Deploying your Django web application to production may be an exciting as well as stressful process. Picture this: your Django masterpiece, polished and perfected, ready to break free from the confines of your development environment and venture into the vast expanse of the internet.
In this guide, we will be using the powerful trio – Gunicorn, the agile stallion of WSGI servers; Nginx, the reverse-proxy expert; and Supervisor, the vigilant guardian ensuring your processes stay on their best behavior. Get ready, as we explore the key components and strategies to guarantee a seamless transition from development to production.
Prerequisites
Before we start, make sure you have the following:
- An Ubuntu server with SSH access.
- A Django project with a working Gunicorn configuration.
- Basic knowledge of the Linux command line.
Step 1: Update the System
Ensure your system packages are up-to-date by running the following commands:
sudo apt update
sudo apt upgrade
Step 2: Install Dependencies
Install the required software components:
sudo apt install python3-pip python3-dev nginx
Step 3: Install Gunicorn
Gunicorn (Green Unicorn) is a WSGI HTTP server for Python web applications. It will serve as the gateway between your Django application and the outside world. Install it using pip:
pip3 install gunicorn
Step 4: Configure Gunicorn
Navigate to your Django project directory and create a Gunicorn service file. For example, create a file named gunicorn.service
:
[Unit]
Description=gunicorn daemon for myproject
After=network.target
[Service]
User=username
Group=groupname
WorkingDirectory=/path/to/your/django/project
ExecStart=/usr/local/bin/gunicorn --workers=3 --bind=unix:/path/to/your/django/project/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
Replace username
, groupname
, /path/to/your/django/project
, and myproject
with your actual values.
Enable and start the Gunicorn service:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
Step 5: Install and Configure Nginx
Nginx acts as a reverse proxy, handling client requests and forwarding them to Gunicorn. Install Nginx and configure it to pass requests to Gunicorn.
sudo apt install nginx
Create an Nginx server block configuration for your Django project. For example, create a file named /etc/nginx/sites-available/myproject
:
server {
listen 80;
server_name your_domain_or_ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /path/to/your/django/project;
}
location / {
include proxy_params;
proxy_pass http://unix:/path/to/your/django/project/myproject.sock;
}
}
Replace your_domain_or_ip
and /path/to/your/django/project
with your actual values.
Create a symbolic link to the sites-enabled directory and test the Nginx configuration:
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
Step 6: Implement SSL with Let’s Encrypt (Optional but Recommended)
Enhance the security of your deployment by implementing SSL with Let’s Encrypt. This step is crucial for securing data transmission between clients and your server.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
Step 7: Install Supervisor
Supervisor ensures that Gunicorn is always running, even after server reboots. Install Supervisor and configure it to manage your Gunicorn processes.
sudo apt install supervisor
Create a Supervisor configuration file, for example, /etc/supervisor/conf.d/myproject.conf
:
[program:myproject]
command=/usr/local/bin/gunicorn --workers=3 --bind=unix:/path/to/your/django/project/myproject.sock myproject.wsgi:application
directory=/path/to/your/django/project
user=username
autostart=true
autorestart=true
stderr_logfile=/var/log/myproject.err.log
stdout_logfile=/var/log/myproject.out.log
Replace username
and /path/to/your/django/project
with your actual values.
Restart Supervisor to apply the changes:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart myproject
Conclusion
Congratulations! You’ve successfully deployed your Django web application to production using Gunicorn, Nginx, and Supervisor on an Ubuntu Server. This combination ensures a solid, scalable, and reliable deployment that can handle the demands of the digital world. Remember to secure your server, configure your Django settings for production, and regularly update your system and dependencies for a secure and stable deployment. Happy coding!
Share this post If you find it useful. You may also check our guide to secure your Django app with Nginx, SSL, and CORS post.