Hello Linux: Gunicorn & Supervisor
By Justin

This is the fourth post of a many part series. This post is the starter post for the whole series.
Guincorn is our production WSGI server for running Django or Python web apps in a production environment.
Supervisor is a process manager to ensure Gunicorn (among other things) starts, restarts, and runs when we need it to.
Requirements
Be sure to complete
- Git Push Local Code to Live Linux Server
- Virtual Environment in Working Directory
- PostgreSQL on Live Linux Server
Our Live Server
- Ubuntu 18.04
1. SSH into your server
Replace [email protected] with your user / ip
2. Install & Start Supervisor
sudo apt-get update -y
sudo apt-get install supervisor -y
sudo service supervisor start
3. Install Gunicorn in our Working Directory's Virtualenv
Find the Virtualenv bin that you created in this post
Mine is /var/www/myproject/bin/
/var/www/myproject/bin/python -m pip install gunicorn
4. Verify Gunicorn works
/var/www/myproject/bin/gunicorn --workers 3
You should see
usage: gunicorn [OPTIONS] [APP_MODULE]
gunicorn: error: No application module specified.
You should not see:
-bash: /var/www/myproject/bin/gunicorn: No such file or directory
So how many workers should we use? A rule of thumb is to do (2 * num_cores) + 1
How do you get the number of cores your Linux system has?
grep -c ^processor /proc/cpuinfo
Let's say that returns 2, you'd have 5 workers (2 * 2) = 4 + 1 = 5
5. Create a Supervisor Process
With supervisor, we can run step 4 automatically, restart it if it fails, create logs for it, and start/stop it easily.
Basically, this ensures that our web server will continue to run if you push new code, server reboots/restarts/goes down and back up, etc.
Of course, if a catastrophic error occurs (or if bad code is in your Django project) then this process might fail as well.
All supervisor processes go in:
/etc/supervisor/conf.d/
So, if you ever need to add a new process, you'll just add it there.
Let's create our project's gunicorn configuration file for supervisor.
touch /etc/supervisor/conf.d/myproject-gunicorn.conf
Now you should see:
$ ls -al /etc/supervisor/conf.d/
myproject-gunicorn.conf
Now, let's add the base settings:
[program:myproject_gunicorn]
user=root
directory=/var/www/myproject/src/
command=/var/www/myproject/bin/gunicorn --workers 3 --bind unix:myproject.sock cfehome.wsgi:application
autostart=true
autorestart=true
stdout_logfile=/var/log/myproject/gunicorn.log
stderr_logfile=/var/log/myproject/gunicorn.err.log
Let's break it down line by line.
[program:myproject_gunicorn]user=rootdirectory=/var/www/myproject/src/command=...autostartautorestartstdout_logfile6. Update Supervisor
supervisorctl reread
supervisorctl update
7. Check Our Supervisor Program/Process Status
As we mentioned when we created the myproject_gunicorn supervisor program, we can now do:
sudo supervisorctl status myproject_gunicorn
A few other useful commands (again):
- sudo supervisorctl start myproject_gunicorn
- sudo supervisorctl stop myproject_gunicorn
- sudo supervisorctl restart myproject_gunicorn
Wrap Up
We now have
- Git Setup for Push/Pulling Code From Local to Production
- Working Directory with a Virtual Environment
- PostgreSQL on Live Linux Server
- Setup Gunicorn & Supervisor (this post)
Now we need to