How we are deploying Django projects to AWS

Internally, we are constantly looking for the best way to deploy some Django projects to AWS.  Some people prefer to use automated process (yeah! automated deploys).

This is just a step. There are a lot way to do it. Here is the stack. Let’s go!

AWS:
I personally prefer Google Cloud Platform. Probably I will write about that on this blog soon.

I’m using a EC2 instance, with Ubuntu.
I’m clonning the  Git repo under  /home/ubuntu by using the ubuntu user.

Virtualenvwrapper:

Virtual environment is an important requirement. I have used both virtualenv  and virtualenvwrapper; in this case I used virtualenvwrapper, and all Python and pip packages are installed in a separated folder.

Gunicorn:

Gunicorn is just an HTTP server for projects.I have started my Django project on server trough Gunicorn on a local hostname and port (http://localhost:8000).

Supervisor:

I use Supervisor to convert my project in a daemon. That’s it. So, if I restart the server, this daemon will be started automatically.This is my  /etc/supervisor/conf.d/myproject.conf

[program:myproject]
command=/home/ubuntu/myproject/start_gunicorn.sh
autostart=true
stdout_logfile=/home/ubuntu/logs/gunicorn.log
stdout_logfile_backups=20
stdout_logfile_maxbyes=20MB

Start_gunicorn.sh  is just a bash file to launch Django trough Gunicorn. Something like:

gunicorn myproject.wsgi:application -w 3 --bind=127.0.0.1:8000 

Nginx:

Nginx is very cool HTTP/Proxy server. I’m using Nginx as a proxy, to serve the local project to the web.I’ts like make enabled  localhost:8000  from  mydomain.com. Nginx is also serving all the Django static files, and make it visible from  /static.

server { listen 80; server_name mydomain.com; client_max_body_size 5M; charset utf8; keepalive_timeout 6; access_log /home/ubuntu/logs/nginx.access.log; error_log /home/ubuntu/logs/nginx.error.log; location / {  proxy_read_timeout 800;  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  proxy_set_header Host $host;  proxy_pass http://127.0.0.1:8000/; } location /static {  alias /home/ubuntu/myproject/static;  autoindex on; }}

If you need help with your deployment or managing your stack, please contact us.