This is Part 3 of my tutorial for Flask Webserver Deployment. In the last section we prepared our server for serving a Flask application. We created a small Flask test application and finally started up the Flask development server. In this part we will use a more production ready server called Gunicorn to run the application.
Gunincorn 'Green Unicorn' is a Python WSGI HTTP Server for Unix. It is ported from Ruby's Unicorn project and it is supposed to be "simply implemented", "light on server resources" and "fairly speedy".
We can easily install Gunicorn via Pythons package installer pip. Make sure to change to the virtual environment we created in the last part of the tutorial and install it:
cd /home/apps
source /venv/bin/activate
pip install gunicorn
Gunicorn expects a Python module with a WSGI object in it. So we create an new Python module named wsgi.py containing the following line of code:
from app import app as application
This module only opens up our app module and imports Flasks WSGI app object.
Now we can start our server by calling:
gunicorn wsgi -b127.0.0.1:80
We got our Gunicorn server working but it is a bit inconvenient. We need to manually activate our virtual environment and start up the Gunicorn server. Actually we want our server to startup automatically. To achieve this we will use a process control system called supervisor. It is a Python2 application so in order to use it we will leave our virtual environment. Make sure you have installed the Python2 packages python and python-pip.
deactivate
apt-get install python python-pip
pip install supervisor
We need a startup script that does the following things for us:
We create a new file called run_flaskapp.py in /home/apps with the following lines of code:
#!/bin/bash
# activate virtualenv
cd /home/apps
source venv/bin/activate
# run gunicorn
exec gunicorn wsgi -b0.0.0.0:80
Supervisor comes with a config template which we will copy in our actual supervisord.conf file.
echo_supervisord_conf > /etc/supervisord.conf
vim /etc/supervisord.conf
We add our flasktest app here by adding the following text at the bottom of /etc/supervisord.conf:
[program: flasktest]
command=/home/flasktest/run_flasktest.sh
directory=/home/flasktest
user=apps
autostart=true
autorestart=true
stdout_logfile=/tmp/flasktest.log
stderr_logfile=/tmp/flasktest_error.log
We start the supervisor daemon by calling:
supervisord
This should automatically startup our flasktest app. Now we can use supervisorctl to start and stop our application:
$ supervisorctl start flasktest
flasktest: started
$ supervisorctl status flasktest
flasktest RUNNING pid 1504, uptime 0:00:49
$ supervisorctl stop flasktest
flasktest: stopped
To automatically startup the supervisord daemon and with it our application on system start we need an init script. Luckily somebody had the idea of writing it for us and putting it on GIT. We can find init scripts for different Linux distributions on https://github.com/Supervisor/initscripts.git.
In my next post we will put our Gunicorn application server behind a Nginx proxy server for increased savety reasons.