Serving Django with Gunicorn and NGINX
In this article, we will explore the concept of serving Django applications using Gunicorn and NGINX. We will discuss the importance of using these tools, their use cases, and provide a step-by-step g …
Updated September 20, 2024
In this article, we will explore the concept of serving Django applications using Gunicorn and NGINX. We will discuss the importance of using these tools, their use cases, and provide a step-by-step guide on how to set up your Django application with Gunicorn and NGINX. Serving Django with Gunicorn and NGINX
What is Gunicorn?
Gunicorn is a Python WSGI (Web Server Gateway Interface) server that allows you to run your Django application concurrently. It provides a robust and scalable solution for deploying your Django application. Gunicorn is designed to be used behind a reverse proxy server, such as NGINX.
What is NGINX?
NGINX is a popular open-source web server software that can also act as a reverse proxy server. In the context of serving Django applications, NGINX acts as a reverse proxy server that sits between the client and Gunicorn. NGINX receives incoming requests from clients and forwards them to Gunicorn, which then runs the Django application.
Why Use Gunicorn and NGINX?
Using Gunicorn and NGINX provides several benefits, including:
- Scalability: Gunicorn allows you to run multiple worker processes concurrently, making it easier to scale your application.
- Robustness: Gunicorn provides a robust solution for deploying your Django application. It can handle high traffic and unexpected crashes.
- Security: NGINX acts as a reverse proxy server that sits between the client and Gunicorn. This adds an extra layer of security to your application.
Step-by-Step Guide
Step 1: Install Gunicorn
To install Gunicorn, you can use pip:
pip install gunicorn
Step 2: Create a Systemd Service File for Gunicorn
Create a new file called gunicorn.service
in the /etc/systemd/system
directory:
sudo nano /etc/systemd/system/gunicorn.service
Add the following code to the file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=<your_username>
RuntimeDirectory=gunicorn
ExecStart=/usr/bin/gunicorn --workers 3 --bind unix:gunicorn.sock -m 007 your_django_app.wsgi:application
Restart=always
[Install]
WantedBy=multi-user.target
Replace <your_username>
with your actual username and your_django_app
with the name of your Django application.
Step 3: Create a Systemd Socket File for Gunicorn
Create a new file called gunicorn.socket
in the /etc/systemd/system
directory:
sudo nano /etc/systemd/system/gunicorn.socket
Add the following code to the file:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
Step 4: Reload Systemd and Start Gunicorn
Reload systemd to pick up the new service files:
sudo systemctl daemon-reload
Start the Gunicorn service:
sudo systemctl start gunicorn
Enable the Gunicorn service to start automatically on boot:
sudo systemctl enable gunicorn
Step 5: Configure NGINX
Create a new file called django.conf
in the /etc/nginx/sites-available
directory:
sudo nano /etc/nginx/sites-available/django.conf
Add the following code to the file:
server {
listen 80;
server_name example.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /path/to/your/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Replace example.com
with your domain name and /path/to/your/project
with the actual path to your project.
Create a symbolic link to the new configuration file:
sudo ln -s /etc/nginx/sites-available/django.conf /etc/nginx/sites-enabled/
Restart NGINX:
sudo service nginx restart
Conclusion
In this article, we explored the concept of serving Django applications using Gunicorn and NGINX. We discussed the importance of using these tools, their use cases, and provided a step-by-step guide on how to set up your Django application with Gunicorn and NGINX.
By following the steps outlined in this article, you can create a robust and scalable solution for deploying your Django application using Gunicorn and NGINX.