Skip to content

Instantly share code, notes, and snippets.

@shivampip
Last active July 19, 2022 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shivampip/e214fcf36db14e267817692291a8bc1b to your computer and use it in GitHub Desktop.
Save shivampip/e214fcf36db14e267817692291a8bc1b to your computer and use it in GitHub Desktop.
Nginx Started

Let's get started

Installation

  • Update ubuntu
sudo apt-get update
sudo apt-get upgrade
  • Install Nginx
sudo apt-get install nginx
  • Stop Apache server (anything running on port 80)
sudo service apache2 stop
  • Confirm nathing runs on port 80
sudo lsof -i:80
  • Now start Nginx
sudo systemctl start nginx
  • Done

Serving Static Site (react etc)

  • Go to /var/www
  • Put your static site there. (in case of react, build folder)(build using npm run build)
/var/www/react-app/build/.......
  • Now go to /etc/nginx/sites-enabled/ and create config file for your react app
/etc/nginx/sites-enabled/react-app.conf
  • This will be the content of your conf file
server{
  listen 80; # port at which nginx will communicate locally (not to user)
  root /var/www/react-app/build;
  index index.html index.htm;
  server_name app.shivam.com www.app.shivam.com;
  location / {
  }
}
  • In case of multiple react app, make multiple server objects with only change in root location and server_name. (in same file or other file, no matter)
  • Restart Nginx
sudo systemctl restart nginx
  • Check status
sudo systemctl status nginx
  • Done

Serving Django with Nginx (including static and media)

  • In your django settings.py file
STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/django-app/static_files/'

MEDIA_URL = "/media/"
MEDIA_ROOT = '/var/www/django-app/media_files/'

Manually create these location ( static_files and media_files )

  • Set DEBUG= True
  • Now comes the Nginx part
  • Open /etc/nginx/sites-enabled/django-app.conf
  • Its content is here
server{
        listen 80;
        server_name www.api.shivam.com api.shivam.com;
        charset utf-8;
        location /media {
                alias /var/www/django-app/media_files;
        }
        location /static {
                alias /var/www/django-app/static_files;
        }
        location / {
            proxy_pass http://0.0.0.0:8001;  #port at which django is running
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
}
  • Restart Nginx
sudo systemctl restart nginx
  • Run the django-app with gunicorn
gunicorn django-app.wsgi -b 0.0.0.0:8001
  • Done
  • Enjoy

Gunicorn Process control

  • Finding gunicorn process
ps ax|grep gunicorn
  • Killing specific process
kill -9 <process-id>
  • Kill all gunicorn process
pkill gunicorn

Nginx root path rewrite to subpath

  # abc.com/h => abc.com/w/web_demo;
	location /h {
		rewrite /h /w/web_demo last;
	}

  # abc.com => abc.com/w/web_demo
	location = / {
		rewrite "^.*$" /w/web_demo last;
	}

Domain DNS Setting

  • Add Record type A
  • For main domain
    • Host= @
    • Points to= 65.76.23.45
    • TTL= 3600 or else
  • For main domain
    • Host= www
    • Points to= 65.76.23.45
    • TTL= 3600 or else
  • For subdomain
    • Host= api
    • Points to= 65.76.23.45
    • TTL= 3600 or else
  • For subdomain
    • Host= www.api
    • Points to= 65.76.23.45
    • TTL= 3600 or else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment