DEPLOYING DJANGO IN PRODUCTION WITH NGINX AND GUNICORN

Nginx Gunicorn Django

Django is a powerful web framework that includes a development server. However, this inbuilt development server is not suitable for deploying our Django application since it isn’t scalable enough for production use.
In this post, I will explain how to deploy our Django web application via Nginx and Gunicorn.

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. NGINX is the best-in-class load‑balancing solutions used by high‑traffic websites such as Dropbox, Netflix, and Zynga.

Gunicorn ‘Green Unicorn’ is a Python WSGI HTTP Server for UNIX. It interfaces with both nginx and your actual python web-app code to serve dynamic content.

Let’s get started with the Deployment.

Setting Django Project

Once your code has been moved to the server. Make sure all required installations are present on the server.
The below command will install Python, pip and nginx.

sudo apt install python3-pip python3-dev nginx

Install all the packages required by your application in your virtual environment next. Install gunicorn with pip.

Pip install gunicorn

In settings.py, update the ALLOWED_HOSTS variable with the IP address of your server. Please note that this value must be added as a string. This is the host/domain names that this Django site can serve. A value of ‘*’ will match anything.

ALLOWED_HOSTS = [‘*’]
OR
ALLOWED_HOSTS = [‘100.100.100.100’]

Please run your migrations using the ‘makemigrations’ and ‘migrate’ commands. The next step will be to open the port where we want to run the application.

sudo ufw allow 8100

My project name is “Sample_Test_Project”, so I will be using that below.

Configuring Gunicorn

A socket is a special file used for interprocess communication, which enables two processes to communicate. We now need to create a socket file for gunicorn. Use the below command to create the file:

sudo vim /etc/systemd/system/gunicorn.socket

Paste the below content in the file:

[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target

Next step will be to create service file for Gunicorn:

In this gunicorn.service file, we specify a user name, the working directory for our project, the virtual environment for our project, as well as a sock file. It’s just various metadata for our project that is essential for gunicorn to know. Remember that gunicorn is the django web server. It needs to know the user. It needs to know the working directory for the project. It needs to know the virtual environment folder. We also need to create a sock file for the project. This sock file communicates with nginx, and they are both able to communicate through this socket.

sudo vim /etc/systemd/system/gunicorn.service

Paste the below content in the file.

[Unit]

Next Step will be to enable the Gunicorn socket:

sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket

Configuring Nginx

Now lets create the configuration file for Nginx.

sudo vim /etc/nginx/sites-available/Sample_Test_Project

Paste the below content in the file:

server {
listen 8100;
server_name 100.100.100.100;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /var/test_deployment/Sample_Test_Project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}

Once done now we need to activate our configuration, followed by restart of Nginx and we are done.

sudo ln -s /etc/nginx/sites-available/Sample_Test_Project /etc/nginx/sites-enabled/

With this your application will be up and running :-)

Conclusion:
Here we saw how to deploy your Django application with Nginx and Gunicorn. If you have any queries please feel free to contact.

Full stack Python Web Developer