Nginx Setup

  • sudo vim /etc/nginx/sites-available/MY_PROJECT
    server {
        listen 80;
        # if you wanna use subdomains:
        # server_name DOMAIN SUBDOMAIN_01.DOMAIN SUBDOMAIN_02.DOMAIN;
        server_name SERVER_DOMAIN_OR_IP;
     
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/USERNAME/MY_PROJECT_DIR;
        }
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/run/gunicorn.sock;
        }
    }
  • sudo ln -s /etc/nginx/sites-available/MY_PROJECT /etc/nginx/sites-enabled 
  • check for syntax errors by sudo nginx -t
  • restart nginx sudo systemctl restart nginx
  • remove 8000 access sudo ufw delete allow 8000
  • sudo ufw allow 'Nginx Full'
  • run your domain in the browser, all should be running ok

 

  • anytime you change anything in /etc/nginx/* run service nginx restart and systemctl restart gunicorn
  • anytime you change anything on server (after git pull), restart gunicorn to apply the changes sudo systemctl restart gunicorn


Resources:

➡️https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

➡️https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps

➡️https://medium.com/@_christopher/deploying-my-django-app-to-a-real-server-part-i-de78962e95ac

➡️http://rahmonov.me/posts/run-a-django-app-with-nginx-and-gunicorn/

➡️https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-debian-9