Setting up Nginx with Django and Gunicorn allows you to deploy a Django web application with a production-ready web server and a reverse proxy for serving HTTP requests. Gunicorn (Green Unicorn) is a WSGI HTTP server for running Python web applications, while Nginx acts as a reverse proxy to handle incoming requests. Here’s how to set up this configuration:
1. Install Required Software:
Before you start, ensure that you have Python, Django, Gunicorn, and Nginx installed on your server.
- Install Django and Gunicorn using pip:
pip install django gunicorn- Install Nginx using your package manager. On Ubuntu, you can use:
sudo apt update
sudo apt install nginx2. Configure Django and Gunicorn:
a. Navigate to your Django project directory and create a Gunicorn systemd service unit file. For example:
sudo nano /etc/systemd/system/myproject_gunicorn.serviceReplace myproject with your project name. Add the following content to the file:
[Unit]
Description=gunicorn daemon for myproject
After=network.target
[Service]
User=your_username # Replace with your username
Group=group_name # Replace with your group name
WorkingDirectory=/path/to/your/project
ExecStart=/usr/local/bin/gunicorn --workers 3 --bind unix:/path/to/your/project/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.targetSave the file and exit the text editor.
b. Replace your_username, group_name, /path/to/your/project, and myproject with appropriate values for your setup.
c. Start and enable the Gunicorn service:
sudo systemctl start myproject_gunicorn
sudo systemctl enable myproject_gunicorn3. Configure Nginx:
a. Create an Nginx server block configuration file for your Django project. For example:
sudo nano /etc/nginx/sites-available/myprojectAdd the following content to the file:
server {
listen 80;
server_name your_domain.com www.your_domain.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:/path/to/your/project/myproject.sock;
}
}Replace your_domain.com, /path/to/your/project, and myproject with your domain, project path, and Gunicorn socket name.
b. Create a symbolic link to enable the Nginx server block:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabledc. Test the Nginx configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx4. Configure Django Settings:
In your Django project’s settings, make sure you have the ALLOWED_HOSTS setting configured to include your domain name:
ALLOWED_HOSTS = ['your_domain.com', 'www.your_domain.com']5. Configure Your Domain:
Update your DNS settings to point your domain to your server’s IP address.
6. Secure Your Application:
Consider configuring SSL/TLS to encrypt data between the client and server. You can use Let’s Encrypt to obtain free SSL certificates and configure Nginx to use them.
7. Test Your Application:
Restart Gunicorn and Nginx to apply the changes:
sudo systemctl restart myproject_gunicorn
sudo systemctl restart nginxVisit your domain in a web browser to verify that your Django application is running through Gunicorn and Nginx.
By following these steps, you can set up Nginx with Django and Gunicorn to deploy your Django web application for production use.