Let's get started
- 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
- Go to
/var/www
- Put your static site there. (in case of react,
build
folder)(build usingnpm 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 andserver_name
. (in same file or other file, no matter) - Restart Nginx
sudo systemctl restart nginx
- Check status
sudo systemctl status nginx
- Done
- 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
- Finding gunicorn process
ps ax|grep gunicorn
- Killing specific process
kill -9 <process-id>
- Kill all gunicorn process
pkill gunicorn
# 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;
}
- 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