[PYTHON] Organize the flow when running Django using NGINX and gunicorn (personal memo)

Introduction

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)

Thing you want to do

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 image.png

Required work flow

To achieve the above, you need to do the following (assuming the Django application is already deployed on the server):

  1. Prepare a configuration file for NGINX to call the Django site --Create a configuration file with a proper name in the "/ etc / nginx / sites-available" folder --The content to be written in the configuration file is "Use this port"
  2. Create a symbolic link to the file created in 1 above in the "/ etc / nginx / sites-enabled" folder. ――By doing this, if the above No. 1 file becomes invalid, you only have to break the symbolic link. ――Symbolic link In the first place, if you think that it's like a shortcut link, k
  3. Restart NGINX and gunicron to access

1. Create a configuration file for NGINX to call your Django site

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.

  1. View static files
  2. Send the request information to djangapp.sock (file for socket with gunicorn)

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;
        }
}

2. Create a symbolic link to the config file that calls Django

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

3. Restart NGINX and gunicorn

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

Organize the flow when running Django using NGINX and gunicorn (personal memo)
Flow of getting the result of asynchronous processing using Django and Celery
(Personal) points when using ctypes
DEBUG settings when using Django
When using if and when using while
Organize [Django] commands and roles
[Python] Error and solution memo when using venv with pyenv + anaconda
DJango memo: From the beginning (using the management screen) my addictive point