As I was studying, I didn't understand the settings of NGINX and gunicron after deploying Django to a remote host, so I made a note for myself.
-[What you want to do](#What you want to do) -[Required work flow](# Required work flow) -[1. Create configuration file for NGINX to call Django site](# 1-Create configuration file for NGINX to call Django site) -[2. Create a symbolic link to the configuration file that calls Django](# 2-Create a symbolic link to the configuration file that calls Django) -[3. Restart NGINX and gunicorn](# 3-Restart NGINX and gunicorn)
Deploy an application created with Django on the cloud, light processing such as displaying static files is processed by the web server (NGINX), heavy processing such as dynamic processing by Django is processed by the AP server (gunicron) I want to do it.
In summary, the following image
To achieve the above, you need to do the following (assuming the Django application is already deployed on the server):
Add the configuration file of the website (Django app in this case) called by NGINX to "/ etc / nginx / sites-available". This will allow NGINX to call the site.
/etc/nginx/sites-Add files to available
cd /etc/nginx/sites-available
sudo vi djangapp
NGINX calls Django according to the settings described in this "djang app". Next, describe the information in the created configuration file. This time NGINX wants to do the following two things.
The contents of the configuration file looks like this
etc/nginx/sites-available/djangapp
server {
#Port for passing requests to the website you are setting up
listen 80;
#Website IP or domain name
server_name xx.xx.xx.xxx;
#A spell to avoid the error that the favicon cannot be found
location = /favicon.ico {access_log off; log_not_found off;}
###Below, the correspondence between the request URL and the path on NGINX is defined.
#Set the path to a Django static file
location /static/ {
root /home/ubuntu/djangapp;
}
#Set the path to display the CSS of the administrator page
location /static/admin {
root /home/ubuntu/venv/lib/python3.6/site-packages/django/contrib/admin/static/admin;
}
#Set the path to a unix socket to send the request to a web page
location / {
# 「/etc/nginx/proxy_"params" describes the proxy settings
include proxy_params;
# djangapp.The request result is sent to sock
proxy_pass http://unix:/home/ubuntu/djangapp/djangapp.sock;
}
}
The "sites-available / djangapp" created earlier creates a symbolic link in "site-enabled" to indicate that it is "enabled". If you want to disconnect temporarily, just delete this symbolic link
Create symbolic links
sudo ln -s /etc/nginx/sites-available/djangapp /etc/nginx/sites-enaled
You have now created a symbolic link called sites-enabled / djangapp-> sites-available / djangapp. Confirmed for the time being.
Symbolic link confirmation
(venv) ubuntu@ip-172-31-45-165:/etc/nginx/sites-available$ ls -la /etc/nginx/sites-enabled/
total 8
drwxr-xr-x 2 root root 4096 Apr 29 13:26 .
drwxr-xr-x 8 root root 4096 Apr 29 13:27 ..
lrwxrwxrwx 1 root root 34 Apr 29 06:54 default -> /etc/nginx/sites-available/default
#It's a symbolic link
lrwxrwxrwx 1 root root 36 Apr 29 13:26 djangapp -> /etc/nginx/sites-available/djangapp
First, let's test if NGINX works properly
nginx test run
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Confirm that it works without problems. Then, various restarts
Restart NGINX
sudo systemctl restart nginx
Reboot gunicorn
sudo systemctl restart gunicorn
After that, connect to the URL
Recommended Posts