Deploy/Run a Django app with Nginx, Gunicorn and Supervisor in Ubuntu

The reason we need Supervisord

Right now, we have our app running with Nginx and Gunicorn. However, every time our machine boots we have to start gunicorn and overall, controlling (stopping, restarting and etc) gunicorn is very difficult. What we want is an easy way of doing so.

Welcome Supervisord which allows us to monitor and control a number of processes on UNIX-like operating systems.

Let’s remember how we used to start our app:

gunicorn --daemon --workers 3 --bind unix:/home/ubuntu/myproject/myproject.sock myproject.wsgi

With supervisord at our disposal, it will be very easy and convenient to execute those commands:

supervisorctl start myproject
supervisorctl stop myproject
supervisorctl restart myproject

Installation and Setup

To install, type the following:

sudo apt-get install supervisor

Now, restart it:

sudo service supervisor restart

The main configuration file of supervisord is here /etc/supervisor/supervisord.conf. If we take a look, we will see that it contains these lines:

[include]
files = /etc/supervisor/conf.d/*.conf

It means that config files of specific projects can be stored here /etc/supervisor/conf.d/ and they will be included in that main file.

So, let’s create myproject.conf in /etc/supervisor/conf.d/ folder:

sudo vim /etc/supervisor/conf.d/myproject.conf

and configure our project:

[program:myproject]
command=/home/ubuntu/myprojenv/bin/gunicorn --workers 3 --bind 0.0.0.0:8030 myproject.wsgi
directory=/home/ubuntu/myproject
autostart=true
autorestart=true
stderr_logfile=/var/log/myproject.err.log
stdout_logfile=/var/log/myproject.out.log

Let’s save the file and execute the following commands to bring these changes into effect:

sudo supervisorctl reread
sudo supervisorctl update

Well, that’s pretty much it. Simple, right? To verify that everything is working, type this:

ps ax | grep gunicorn

You should see several gunicorn processes running. Or, you can go to localhost:8000 and you will see your django app up and running.

Or, you can now use supervisor to check whether your app is running:

sudo supervisorctl status myproject

Source: http://rahmonov.me/posts/run-a-django-app-with-nginx-gunicorn-and-supervisor/

FollowFollow on FacebookFollow on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *