Skip to content
Blog How To Deploy a Django project on an Ubuntu server with Nginx

How To Deploy a Django project on an Ubuntu server with Nginx

Here’s a general outline of How to Deploy a Django project on an Ubuntu server with Nginx:
  1. Server Setup:

    • Start by setting up an Ubuntu server.
    • SSH into the server as the root user or a user with sudo privileges.
  2. Install Required Software:

    • Update your system’s package list:
      sudo apt update
    • Install necessary packages:
      sudo apt install python3-pip python3-venv nginx
  3. Create a Virtual Environment:

    • Navigate to your project’s root directory:
      cd /PATH/TO/MY_PROJECT
    • Create a virtual environment:
      python3 -m venv MY_PROJECT_ENV
    • Activate the virtual environment:
      source MY_PROJECT_ENV/bin/activate
  4. Install Dependencies:

    • Install your project’s dependencies using pip:
      pip install -r requirements.txt
  5. Configure Gunicorn:

    • Install Gunicorn (a WSGI HTTP server for running Django):
      pip install gunicorn
    • Create a Gunicorn service file in `/etc/systemd/system/MY_PROJECT_gunicorn.service`:
      [Unit]
      Description=gunicorn daemon for MY_PROJECT
      After=network.target
      
      [Service]
      User=YOUR_USERNAME
      Group=YOUR_GROUP
      WorkingDirectory=/PATH/TO/MY_PROJECT
      ExecStart=/PATH/TO/MY_PROJECT/MY_PROJECT_ENV/bin/gunicorn --workers 3 --bind unix:/PATH/TO/MY_PROJECT/MY_PROJECT.sock MY_PROJECT.wsgi:application
      
      [Install]
      WantedBy=multi-user.target

      Replace `YOUR_USERNAME`, `YOUR_GROUP`, `/PATH/TO/MY_PROJECT`, `MY_PROJECT_ENV`, and `MY_PROJECT` with your actual values.

    • Start and enable the Gunicorn service:
      sudo systemctl start MY_PROJECT_gunicorn
      sudo systemctl enable MY_PROJECT_gunicorn
  6. Configure Nginx:

    • Create an Nginx server block configuration in `/etc/nginx/sites-available/MY_PROJECT`
      server {
          listen 80;
          server_name my_domain.com www.my_domain.com;
      
          location = /favicon.ico { access_log off; log_not_found off; }
          location /static/ {
              root /PATH/TO/MY_PROJECT;
          }
      
          location / {
              include proxy_params;
              proxy_pass http://unix:/PATH/TO/MY_PROJECT/MY_PROJECT.sock;
          }
      }
    • Link the configuration to the `sites-enabled` directory:
      sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled
    • Test the Nginx configuration:
      sudo nginx -t
    • Restart Nginx:
      sudo systemctl restart nginx
  7. Collect Static Files:

    • Collect static files:
      python manage.py collectstatic
  8. SSL Certificate (Optional):

    For HTTPS, consider obtaining an SSL certificate from Let’s Encrypt using Certbot.

  9. Start Your Application:

    • Restart Gunicorn service:
      sudo systemctl restart MY_PROJECT_gunicorn
    • Restart Nginx:
      sudo systemctl restart nginx


Your Django project should now be deployed and accessible through Nginx on your Ubuntu server. Remember that these are general steps; you might need to adapt them to your specific project structure and requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.