HOME BLOGS ABOUT CONTACT

How to Host a Django App on a Linux Server with Nginx and Gunicorn (Step-by-Step Guide)

tania andrew Suresh Thapa
| 06 Oct, 2025 | 505 views
0
0
How to Host a Django App on a Linux Server with Nginx and Gunicorn (Step-by-Step Guide)

How to Host a Django App on a Linux Server with Nginx and Gunicorn (Step-by-Step Guide)

 

Deploying Django in production can seem tricky, but with the right setup — Gunicorn + Nginx — it’s fast, secure, and scalable.
In this guide, we’ll walk step-by-step through deploying a Django app on a Linux server.

 

Prerequisites

Before you start, make sure you have:

  • A Linux server (Ubuntu recommended)
  • Python 3.x installed
  • Nginx installed
  • Your Django app ready (e.g., /home/ubuntu/projects/myapp)
  • A domain name (e.g., myapp.com)

 

Step 1: Create a Virtual Environment

First, SSH into your Linux server and navigate to your project directory:

 

cd /home/ubuntu/projects/myapp
python3 -m venv venv
source venv/bin/activate

 

Then install your dependencies:

pip install -r requirements.txt
pip install gunicorn

 

Step 2: Test Gunicorn with Your App

Before creating services, make sure Gunicorn can serve your Django app:

cd /home/ubuntu/projects/myapp
gunicorn myapp.wsgi:application

Now visit your server’s IP in a browser (http://your-server-ip:8000) to verify that it works.

Stop Gunicorn with Ctrl + C once confirmed.

 

Step 3: Create Gunicorn Systemd Files

To run Gunicorn as a background service, create two systemd files:

/etc/systemd/system/myapp.socket

sudo nano /etc/systemd/system/myapp.socket

 

Paste the following:

[Unit]
Description=gunicorn socket for myapp

[Socket]
ListenStream=/run/myapp.sock

[Install]
WantedBy=sockets.target

 

/etc/systemd/system/myapp.service

sudo nano /etc/systemd/system/myapp.service

 

Paste:

[Unit]
Description=gunicorn daemon for myapp
Requires=myapp.socket
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/projects/myapp
ExecStart=/home/ubuntu/projects/myapp/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/myapp.sock \
          myapp.wsgi:application

[Install]
WantedBy=multi-user.target

 

Enable and Start Gunicorn

sudo systemctl start myapp.socket
sudo systemctl enable myapp.socket

 

Check that it’s running:

sudo systemctl status myapp.socket
file /run/myapp.sock

 

If everything looks good, continue to Nginx setup.

 

Step 4: Configure Nginx

Now we’ll configure Nginx to serve your Django app and proxy traffic to Gunicorn.

Create a new server block:

sudo nano /etc/nginx/sites-available/myapp

 

Add this configuration:

server {
    listen 80;
    server_name myapp.com www.myapp.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/ubuntu/projects/myapp;
    }

    location /media/ {
        root /home/ubuntu/projects/myapp;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/myapp.sock;
    }
}

 

Enable the Nginx Config

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx

 

Step 5: Add SSL with Let’s Encrypt (Optional but Recommended)

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d myapp.com -d www.myapp.com

This will automatically install a free SSL certificate and redirect traffic to HTTPS.

 

Step 6: Verify Everything Works

Check your Gunicorn and Nginx services:

sudo systemctl status myapp.service
sudo systemctl status nginx

Then visit https://myapp.com — you should see your Django app running securely on the web

 

Wrap-Up

You’ve successfully deployed a Django application using Gunicorn and Nginx on a Linux server.

Quick Recap

 

Steps    Description
1    Create and activate virtualenv
2    Install Gunicorn
3    Create Gunicorn socket & service
4    Configure Nginx reverse proxy
5    (Optional) Enable SSL with Certbot
6    Restart services and test deployment

 

Pro Tips

  • Use sudo journalctl -u myapp to view Gunicorn logs.
  • Always restart Nginx after modifying site configs.

 

 

Tags:

django linux python ubuntu

Comments

Please login to leave a comment.

No comments yet. Be the first to comment!