nginx + gunicorn + supervisor deploy django project

After writing the Django project, to deploy the project to the server, you can use nginx, gunicorn and supervisor to deploy.

Install virtual environment

When multiple projects are deployed on one machine, the packages that each project depends on may conflict and affect each other. Using virtual environment can isolate the execution environment of each project without affecting each other.

Install virtualenv

  pip install virtualenv

Install virtualenvwrapper

In order to simplify the operation of virtualenv, you can install the virtualenv wrapper, which simplifies the operation of virtualenv and places all virtual environments in the same directory.

pip install virtualenvwrapper

Then configure work on home
First find the path to the virtualenvwrapper.sh file

whereis virtualenvwrapper.sh

Add path to / etc/profile

export WORKON_HOME=~/.virtualenvs // The export address is the address that the virtual environment will place in the future. You can choose it yourself
source /usr/local/bin/virtualenvwrapper.sh

Then make the configuration of the / etc/profile file take effect

source /etc/profile

After that, we can use it

Create a virtual environment

mkvirtualenv project

Switch to virtual environment

workon project

Exit virtual environment

deactivate

Delete virtual environment

rmvirtualenv project

Global install nginx

After installation, add the configuration of the project and create a new. conf file

server {
       listen 80; //port
       server_name localhost ;//ip address

       access_log /data/log/nginx-access.log; // Success log address
       error_log  /data/log/nginx-error.log; // Error log address

       keepalive_timeout 3600;
       client_max_body_size 5120M;

       location /static/ {
          alias /data/static; // Address of static file
       }

       location / {
           include uwsgi_params;
           add_header Access-Control-Allow-Origin *;
           proxy_set_header Host $http_host;
           proxy_set_header X-Forward-HOST $server_name;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_redirect off;
           proxy_connect_timeout 3800s;
           proxy_read_timeout 3600s;
           proxy_http_version 1.1;
           proxy_set_header Connection "";
           proxy_pass http://127.0.0.1:80;
        }
  }

Test correctness after configuration

nginx -t

Start nginx after configuration:

/usr/local/nginx/sbin/nginx  -s reload // The front is the nginx installation address

Install gunicorn

You can install gunicorn in a virtual environment

pip install gunicorn

Install supervisor

Configure supervisor and create a new. ini file

command=/data/.virtualenvs/project/bin/gunicorn -w 3 -b 127.0.0.1:80 project.wsgi:application //Switch to virtual environment startup project
directory=/data/project   // Switch to the execution directory
user=root
autostart=true
autorestart=true
startsecs=10
redirect_stderr=true
stderr_logfile=/data/log/stderr.log // Error log address
stdout_logfile=/data/log/stdout.log // Correct log address
stopasgroup=true
stopsignal=QUIT

Restart the supervisor and load the new configuration

supervisorctl update
supervisorctl reload

Finally, the entire deployment is complete

Tags: Web Server Nginx supervisor pip Django

Posted on Sun, 17 Nov 2019 14:42:41 -0800 by SUNNY ARSLAN