Setting up a Django application with Nginx and Gunicorn on a Linux server .

Vikram Singh Gurjar
1 min readJul 20, 2024

--

sudo apt update
sudo apt install python3-pip python3-dev libpq-dev nginx

pip3 install gunicorn

Navigate to Your Django Project Directory:

cd /path/to/your/django/project

Run Gunicorn to Test:

gunicorn — workers 3 projectname.wsgi:application

Create a Systemd Service File for Gunicorn :

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

[Unit]
Description=gunicorn daemon for Django project
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/oemtesting/djangoprojectdir
ExecStart=/home/oemtesting/djangoprojectdir/djprojectenv/bin/gunicorn — access-logfile — — workers 3 — bind unix:/home/oemtesting/djangoprojectdir/gunicorn.sock mysite.wsgi:application

[Install]
WantedBy=multi-user.target

Start and Enable the Gunicorn Service:

sudo systemctl start gunicorn
sudo systemctl enable gunicorn

Configure Nginx :

Install Nginx :

sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo nano /etc/nginx/sites-available/yourproject

server {
listen 80;
server_name 10.133.135.71;
client_max_body_size 8000M;
client_body_buffer_size 8000M;
client_body_timeout 30000;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/oemtesting/djangoprojectdir;
}
location /data/ {
root /home/oemtesting/djangoprojectdir;
}
location / {
include proxy_params;

limit_rate_after 5000k;
limit_rate 500k;

proxy_read_timeout 30000;
proxy_connect_timeout 30000;
proxy_send_timeout 30000;

client_max_body_size 50m;

proxy_pass http://unix:/home/oemtesting/djangoprojectdir/gunicorn.sock;
}

}

sudo ln -s /etc/nginx/sites-available/yourproject /etc/nginx/sites-enabled

sudo nginx -t

sudo systemctl restart nginx

--

--

Vikram Singh Gurjar
Vikram Singh Gurjar

Written by Vikram Singh Gurjar

||Python||Data Science||Linux||Web Developer*

No responses yet