Last active
December 21, 2015 04:18
-
-
Save prakhar1989/6247999 to your computer and use it in GitHub Desktop.
Deploying Django apps on Nginx with Gunicorn and Supervisor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There are 4 parts to successfully running django apps on nginx and gunicorn. | |
1. Set up gunicorn | |
2. Set up supervisor | |
3. Set up nginx. | |
4. Making admin staticfiles work | |
Step 1 | |
==== | |
Assuming you have gunicorn installed, just run your app using the command - gunicorn_django <ip_address:port> | |
After this works you need to set up a bash script which will control your gunicorn process. Use the bash script as shown in this gist - https://gist.github.com/postrational/5747293#file-gunicorn_start-bash | |
Keep this script where your other django project reside. | |
Check everything is working fine by running the bash script (remember to make it executable first) | |
Step 2 | |
==== | |
To configure supervisor just add a django.conf file in /etc/supervisor. The update commands for supervisor are shown in this blog - http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/ | |
Try restarting and stopping your process to see if everything works fine. | |
Step 3 | |
=== | |
The most important step is to now tell nginx to handle requests. Its best to have one standard django config for nginx in your sites-available folder. Paste this code in sites-avaiable/django_config -> https://gist.github.com/postrational/5747293#file-hello-nginxconf | |
Pay special attention to the socket line in the upstream app server section. When you run gunicorn, it'll display on screen which socket file gunicorn is using. Don't forget to replace that! | |
Lastly, if you're using sqlite db make sure you provide an absolute path (os.path...) in the settings file otherwise you'll encounter a missing sessions table error. | |
Step 4 | |
===== | |
At this step, you should have your site running (sans the admin static files). To do that, you need to run python manage.py collectstatic. This will dump all the static files in your folder. Be sure to move these files in their correct position in the static folder. After this step, hopefully, it'll all work! | |
Happy djangoing! | |
### Supervisor configuration for django | |
deploy this /etc/supervisor/conf.d | |
[program:django_app] | |
command = /home/captain/Code/django_apps/gunicorn_start | |
user = root | |
stdout_logfile = /home/captain/Code/django_apps/logs/gunicorn_supervisor.log | |
redirect_stderr = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment