Deploy django project using NGINX and Gunicorn

Marwan FazoraMarwan Fazora
2 min read

Deploy django project using NGINX and Gunicorn

Requirements:

  1. NGINX
  2. Gunicorn

Prepare environment:

  1. Install pip:
     sudo apt-get install python3-pip
    
  2. Install virtualenv:
     sudo pip3 install virtualenv
    
  3. Create directory for the project:
     mkdir project_name
     cd project_name
    
  4. Now create a virtual environment
     virtualenv project_venv -p 3
    
  5. Now Activate it:
     source project_venv/bin/activate
    

Prepare project files and dependencies:

  1. Upload project to the folder that you just created "project_name"
  2. Upload static folder to project_name
     .
     └── project_name/
         ├── project/
         │   ├── requirements.txt  
         │   ├── ...
         │   └── project/
         │       ├── ...
         │       ├── settings.py
         │       ├── urls.py
         │       ├── wsgi.py
         │       └── ...
         ├── project_venv/
         │   └── bin/
         │       ├── activate
         │       └── ...
         └── static/
             └── ...
    
  3. Install requirements:
     pip3 install -r project/requirements.txt
    

Prepare Gunicorn server:

  1. Install Gunicorn:
     pip3 install gunicorn
    
  2. Create Configuration file
     mkdir conf
     touch conf/gunicorn.py
    
  3. Paste this configuration into conf/gunicorn.conf

     command = '/path_to_project/project_venv/bin/gunicorn'
     #example: '/home/user/project_name/venv/bin/gunicorn'
    
     pythonpath = '/path_to_project'
     #example: '/home/user/project_name'
    
     bind = '127.0.0.1:8000' 
     workers = 3
    
  4. Now run Gunicorn server:
    gunicorn -c conf/gunicorn.py project.wsgi
    
  5. Output should be like:
     [2022-12-05 10:44:14 +0000] [3053259] [INFO] Starting gunicorn 20.1.0
     [2022-12-05 10:44:14 +0000] [3053259] [INFO] Listening at: http://127.0.0.1:8000 (3053259)
     [2022-12-05 10:44:14 +0000] [3053259] [INFO] Using worker: sync
     [2022-12-05 10:44:14 +0000] [3053261] [INFO] Booting worker with pid: 3053261
     [2022-12-05 10:44:14 +0000] [3053262] [INFO] Booting worker with pid: 3053262
     [2022-12-05 10:44:15 +0000] [3053263] [INFO] Booting worker with pid: 3053263
    
  6. Now your project structure should be like this:
     .
     └── project_name/
         ├── conf/
         │   └── gunicorn.py
         ├── project/
         │   ├── requirements.txt  
         │   ├── ...
         │   └── project/
         │       ├── ...
         │       ├── settings.py
         │       ├── urls.py
         │       ├── wsgi.py
         │       └── ...
         ├── project_venv/
         │   └── bin/
         │       ├── activate
         │       └── ...
         └── static/
             └── ...
    

Prepare NGINX:

  1. Install NGINX:

     # update system
     sudo apt update
    
     # install nginx
     sudo apt install nginx
    
     # check if nginx installed successfully 
     sudo systemctl status nginx.service # you should see active (running)
    
  2. Create NGINX config file:

     sudo -i
     cd /etc/nginx/sites-available
     touch "project_name"
    
    • Paste this configuration into "project_name"

        server {
            server_name server_ip_or_domain;
      
            location /static/ {
                alias path_to_static_dir;
                # example: /home/user/project_name/static/
            }
      
            location / {
                proxy_pass http://127.0.0.1:8000; # bind from gunicorn conf
            }
        }
      
  3. Activate this project:

     cd /etc/nginx/sites-enabled
     ln -s /etc/nginx/sites-available/project_name /etc/nginx/sites-enabled/project_name
    
  4. Restart NGINX:

     systemctl restart nginx.service
    

    Finally you now have an active django website deployed with NGINX and gunicorn :)

0
Subscribe to my newsletter

Read articles from Marwan Fazora directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Marwan Fazora
Marwan Fazora

I am Marwan Fazora, a proficient full-stack web developer with a strong command of Python and Django. Possessing expertise in both front-end and back-end development, I specialize in crafting secure and high-performance web applications. I am eager to collaborate and contribute to transforming conceptual ideas into tangible solutions.