1

What is the correct way to deploy a Django project online? Currently, the method I use involves ssh-ing onto the server, git pull, performing Django chores (migrations, collectstatic etc) and then restarting the web server. There must be an automated way of doing this. E.g. If I wanted to deploy onto 10 servers, I don't want to manually update them all!

I can't seem to find anything concrete about best practices. I have heard of Fabric but I don't think it is compatible with Python3 (which I am using). There seem to be many tools but I'm not sure which are reputable.

Any advice would be helpful, thanks

zubhav
  • 1,519
  • 1
  • 13
  • 19

2 Answers2

3

Just to address your question about using Fabric to deploy a Python 3 application: Fabric is not usually installed as part of a project (like in a virtualenv), and so if your system has Python 2, then you should be able to install and run Fabric. On the other hand, if your system only has Python 3, and it is not an option to install Python 2, then you will not be able to use Fabric.

The important thing to note is that Fabric is not tied to your project - it stands on its own as a system tool. You definitely can install it in a virtualenv and tie it to your application's code (i.e. if your were running a Python 2 app), but there's no requirement for you to do so.

As to the correct way to deploy Django apps, that's situational and requires some opinion, so we can't really answer that for you. But I would definitely recommend Fabric to someone in your situation, as a starting point for automating the existing set of shell tasks that you normally perform for your deployments.

YellowShark
  • 2,169
  • 17
  • 17
  • Thanks for this. I just spent a few hours using Fabric and I now have a fully functioning automated deployment procedure! – zubhav Nov 28 '16 at 22:30
  • Great to hear! I've been using it for years, it's easy to write some fairly-complex procedures, especially in modular ways. Check out the following docs for more info: - http://docs.fabfile.org/en/1.12.0/usage/tasks.html#new-style-tasks - http://docs.fabfile.org/en/1.12.0/api/core/tasks.html - http://docs.fabfile.org/en/1.12.0/usage/execution.html#execute – YellowShark Nov 29 '16 at 00:24
1

Fabric may not be compatible with Python3, but it wouldn't hurt you much to write your deploy scripts in Fabric even if your main project is Python3.

There are really 3 separate issues you face here... 1. packaging your code, 2. orchestrating your deployments, and 3. managing your servers.

On issue 1, git is a common choice for Python. Your other choices are building OS level packages (like apt) or building Docker images.

For issue 2, Fabric is actually a pretty good choice for actual deployments. The procedural parts (run migrations, build a new virtual env, etc) are very easy to encode in Python. Other configuration management tools like Ansible or Chef can be used here.

Issue 3 is really where tools like Ansible or Chef come into play (if you are using one of them, it often makes sense to do step 2 using the same tool).

My current favorite method is building Docker containers. For a small (single server) deployment, I just write a Makefile combined with Docker-machine. For multi-servers, ECS is very easy to get going (if you are on AWS) to manage your configuration and deployments. If you're not on AWS, kubernetes gives you lots of features but has a ton of operational overhead.

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42
  • Thanks. I have gone with Fabric as my project is small and has a deadline. Just wrote a fabfile.py and it works perfectly for deployment. I would like to try to use Docker when I have more time, I keep hearing about that one! – zubhav Nov 28 '16 at 22:31