MMS

0 %
Muhammad Mujeeb Sattar
Software Engineer | Full-Stack Developer | Python Developer Django Developer | Web Designer
  • Residence:
    Pakistan
  • City:
    Lahore
English
Urdu
Hindi
Python
Django
Flask
FastAPI
Rest APIs
WordPress
Shopify
Adobe Photoshop
HTML
CSS
JS
Bootstrap
Tailwind CSS
Adobe XD
Django Rest Framework (DRF)
Amazon Web Services (AWS)
Google Cloud Platform (GCP)
RESTful APIs
  • Bootstrap, Materialize
  • Stylus, SAAS, Less
  • Gulp, Webpack, Grunt
  • GIT, Version Control
  • Requirements Analysis
  • Software Architecture,
  • Test-Driven Development (TDD)
  • Agile Methodologies
  • Web Application Security
  • Performance Optimization
  • Responsive Web Design
  • Data Visualization (Apex, Chart.js)
  • Continuous Deployment (CD)
  • Continuous Deployment (CD)
  • Problem Solving
  • Unified Modelling Language

How to deploy a Django project using Nginx, Gunicorn, and Supervisor

January 20, 2024

Introduction

Deploying a Django project on a Virtual Private Server (VPS) can seem like a daunting task, but with the right roadmap and tools, it can be a smooth process. In this blog post, we will provide a step-by-step guide on how to deploy a Django project on a VPS using Nginx, Gunicorn, Supervisor, and a PostgreSQL database. We will also include the necessary commands and configuration files codes to make the process easier.

Step 1: Set Up the VPS

The first step is to set up a VPS with your preferred provider. Once you have your VPS up and running, you will need to connect to it via SSH.

Step 2: Install Dependencies

Before deploying your Django project, you need to install the necessary dependencies. Start by updating the package list:
sudo apt update
Next, install the required packages:
sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx

Step 3: Set Up the PostgreSQL Database

Create a new PostgreSQL database and user for your Django project:
sudo -u postgres psql
CREATE DATABASE your_database_name;
CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;
q

Step 4: Clone Your Django Project

Clone your Django project from your version control system to your VPS:
git clone your_repository_url

Step 5: Install Virtual Environment and Dependencies

Create and activate a virtual environment for your Django project:
python3 -m venv your_virtual_environment_name
source your_virtual_environment_name/bin/activate
Install the required Python packages for your Django project:
pip install -r requirements.txt

Step 6: Configure Nginx

Create a new Nginx server block configuration file:
sudo nano /etc/nginx/sites-available/your_project_name
Add the following configuration to the file:
server {
    listen 80;
    server_name your_domain_name;

    location / {
        proxy_pass http://localhost:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Enable the Nginx server block configuration:
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 7: Configure Gunicorn

Create a new Gunicorn service configuration file:
sudo nano /etc/systemd/system/gunicorn.service
Add the following configuration to the file:
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=your_username
Group=www-data
WorkingDirectory=/path/to/your_project_directory
ExecStart=/path/to/your_virtual_environment_name/bin/gunicorn --access-logfile - --workers 3 --bind unix:/path/to/your_project_directory/your_project_name.sock your_project_name.wsgi:application

[Install]
WantedBy=multi-user.target
Enable and start the Gunicorn service:
sudo systemctl enable gunicorn
sudo systemctl start gunicorn

Step 8: Configure Supervisor

Create a new Supervisor configuration file:
sudo nano /etc/supervisor/conf.d/your_project_name.conf
Add the following configuration to the file:
[program:your_project_name]
command=/path/to/your_virtual_environment_name/bin/gunicorn --access-logfile - --workers 3 --bind unix:/path/to/your_project_directory/your_project_name.sock your_project_name.wsgi:application
directory=/path/to/your_project_directory
user=your_username
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/path/to/your_project_directory/logs/gunicorn.log
Update Supervisor and start the process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start your_project_name

Step 9: Finalize Deployment

Collect the static files for your Django project:
python manage.py collectstatic
Restart Nginx and Supervisor for the changes to take effect:
sudo systemctl restart nginx
sudo supervisorctl restart your_project_name
Your Django project should now be successfully deployed on your VPS using Nginx, Gunicorn, Supervisor, and a PostgreSQL database. You can access your project by visiting your domain name in a web browser.

Conclusion

Deploying a Django project on a VPS may involve several steps, but by following this roadmap and using tools like Nginx, Gunicorn, Supervisor, and PostgreSQL, you can ensure a smooth and reliable deployment process. Remember to regularly update and maintain your deployment to keep your Django project secure and up to date.
Posted in TechnologyTags:
Write a comment