Python Nginx Configurations
-
Installing Nginx
sudo apt updatesudo 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-availableare the websites that you can enable/etc/nginx/sites-enabledare the websites that are running and active- Run
cd /var/www, and clone your application repogit 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 runtouch eg.eyouthlearning.com - Use softlink to link between the file in
sites-enabledand the directory insites-available - Run
cd /etc/nginx - Run
vi ../sites-available/eg.eyouthlearning.com, and copy this configuration
- Run
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-nginxto 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