Python Nginx Configurations


  • Installing Nginx

    • sudo apt update
    • sudo apt install nginx
    • Run sudo vi /etc/nginx/nginx.conf, and configure the nginx
    • Run sudo apt-get install libpq-dev

  • Available Sites & Enabled Sites

    • Run cd /etc/nginx
    • /etc/nginx/sites-available are the websites that you can enable
    • /etc/nginx/sites-enabled are the websites that are running and active
    • Run cd /var/www, and clone your application repo git clone <Repo_URL>
    • Run cd /YOUR_REPO_DIR
    • Run python manage.py runserver
    • Create a file that contains your app in /etc/nginx/sites-available, for example run touch eg.eyouthlearning.com
    • Use softlink to link between the file in sites-enabled and the directory in sites-available
    • Run cd /etc/nginx
    • Run vi ../sites-available/eg.eyouthlearning.com, and copy this configuration
server {
    server_name YOUR_DJANGO_SERVER_DOMAIN;


  location /static {
    autoindex on;
    alias /var/www/YOUR_DJANGO_SERVER_DOMAIN/staticfiles/;
  }

  location /media {
    autoindex on;
    alias /var/www/YOUR_DJANGO_SERVER_DOMAIN/media/;
  }

    location / {
        proxy_set_header Host $host;
        proxy_pass http://localhost:8000;
        proxy_redirect off;
    }
}

Run sudo ln -s ./sites-available/eg.eyouthlearning.com ./sites-enabled/eg.eyouthlearning.com To apply this softlink run sudo systemctl restart nginx


  • Certification with Certbot

  • Create a record with preferred subdomain to attach the certificate too!

  • Run sudo pip install certbot certbot-nginx to install Certbot
  • Run sudo certbot --nginx -d <Your_Donmian_Name>
  • This snippet will be generated automatically inside the above created file in /etc/nginx/sites-available/ & /etc/nginx/sites-enabled/
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/eg.eyouthlearning.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/eg.eyouthlearning.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot