NGINX | Gunicorn | Supervisor | Django | Deploy with Confidence
Python has different web frameworks including Django, Flask, Falcon, CubicWeb etc. Django and Flask are more popular than others. Django comes with many built-in modules and you can develop web applications fast. It has its development server that is used for development purposes and when it comes to production, a more robust and secure server is required. Next, you will see how to integrate your Django application with Nginx (a robust and secure server). Before moving to the configuration settings, let’s look at the key components first.
Web Servers
Different web servers are available like Apache, NGINX, Microsoft Internet Information Services (IIS), Google Web Server (GWS), etc. NGINX is open-source, high-performance, and offers a scalable architecture. From official documentation of NGINX
NGINX has a modular, event‑driven, asynchronous, single-threaded architecture that scales extremely well on generic server hardware and across multi-processor systems. NGINX uses all of the underlying power of modern operating systems like Linux to optimize the usage of memory, CPU, and network, and extract the maximum performance out of a physical or virtual server. The end result is that NGINX can often serve at least 10x more (and often 100–1000x more) requests per server compared to Apache — that means more connected users per server, better bandwidth utilization, less CPU and RAM consumed, and a greener environment too!
Web Server Gateway Interface (WSGI)
The web server Gateway interface defines the convention for communication between the web server and the Python web application. PEP 3333 defines the standard for WSGI. WSGI spawns multiple processors to serve the requests. It is fast and secure with a built-in logging system. Gunicorn, uWSGI, mod_wsgi, and CherryPy are some of the popular WSGI for Django deployment.
We will be using Gunicorn for this configuration because of its stability which is commonly used for web app deployment. Gunicorn runs the Django application by invoking a callable object exposed by Django through the wsgi.py file.
Supervisor
The supervisor is a client/server system that allows users to manage processes on a UNIX-like operating system. It has different components like supervisor, supervisorctl, web server, and XML-RPC interface.
Supervisord is a server piece of supervisor and is responsible for starting child programs at their invocation, responding to commands from clients, and restarting crashed or exited subprocesses.
Next, install the required packages of Python, NGINX, Gunicorn, and Supervisor on the system.
Before installing the above-mentioned packages, let's first install the dependencies for Python.
sudo apt-get update
sudo apt-get -y install build-essential libpq-dev python-dev
The required packages will be installed by executing the following command
sudo apt-get -y install nginx supervisor python3 python3-virtualenv
Now, make the virtual environment and activate it.
python3 -m venv venv
source venv/bin/activate
After activating the virtual environment, install the Gunicorn inside it.
pip install gunicorn
Install the Django package in it and create a Django application. To get in-depth knowledge to create the Django application you can visit the official website.
Following the successful creation of the Django application, go to the application directory and run the development server to ensure the application is successfully created.
pip install Django
mkdir run
mkdir bin
mkdir logs
mkdir src
cd src/
django-admin startproject test_django_app
cd test_django_app
python manage.py runserver 0:8000
Gunicorn Configuration
Configure the Gunicorn with the Django application. Go to the virtual environment /bin directory and create the Gunicorn configuration file.
cd bin/
touch gunicorn_start
The following file includes all the commands required for configure. Each command is commented and you will easily understand what their meaning is. You need to make some changes to this file to run for the Django application because your application name and directory paths will be different. So, open the gunicorn_configuration file
nano gunicorn_start
and copy the following commands into it.
Make the gunicorn_configuration file an executable file by using the instruction.
chmod u+x gunicorn_start
Supervisor Configuration
First, enable the supervisor and start it by executing the next instructions.
sudo systemctl enable supervisor
sudo systemctl start supervisor
To configure the supervisor, create a configuration file inside the /supervisor/conf.d/ directory.
sudo touch /etc/supervisor/conf.d/test_django_app.conf
Open the supervisor configuration file and write the following instructions inside it.
sudo nano /etc/supervisor/conf.d/test_django_app.conf
Now reread the Supervisor configuration files and make the new configuration available.
sudo supervisorctl reread #make sure output will be look like
test_django_app: available
Update the Supervisor as well and restart it.
sudo supervisorctl update #output
test_django_app: added process group
sudo supervisorctl restart test_django_app
and check its status, it will be running state.
sudo supervisorctl status
Nginx Configuration
For the Nginx configuration, create a file inside the /sites-available directory.
cd /etc/nginx/sites-available/
sudo touch test_django_app
sudo nano test_django_app
Configure your Nginx according to the following commands,
Be careful while configuring the proxy_pass parameter, it is pointing to the gunicorn socket file and it is created by the gunicorn_configuration when executed by the Supervisor.
Now, create a symbolic link to the sites-enabled from sites-available by using the following instruction
sudo ln -s /etc/nginx/sites-available/test_django_app /etc/nginx/sites-enabled/test_django_app
After creating the symbolic, go to the /sites-enabled directory and make sure the symbolic link file has been created.
cd /etc/nginx/sites-enabled/
ls
After making sure the Nginx configuration is correct, restart it with the following command.
sudo service nginx restart
Great! you can access the application from your domain address in the Nginx configuration.
Now you can control your application using Supervisor. If you want to update the source code of your application with a new version, you need to restart the Supervisor process.