Detailed steps for starting the nginx+uwsgi project

  • 2020-05-15 03:37:46
  • OfStack

When we developed web project with django, we used django's own test server in the development and testing process. Due to the limitation of its security, stability and other performance, django officially does not recommend using the test server in actual production.

nginx+uwsgi+django is a common django deployment. As the most front-end server, nginx is responsible for receiving all client requests. For the requested static files, nginx server does it by itself, because it has a good ability to handle static files, and its performance has been optimized to support high concurrency. The uWSGI server serves nginx as a support server, and nginx hands the requested dynamic file to uWSGI for processing. uWSGI implements the uwsgi, wsgi, and http protocols. uwsgi is a custom uWSGI protocol that defines the interface between the framework (django) and the server.

1. Install the project environment

System environment: ubuntu 16.04

python environment: python3.5.2

Django version: django1.11.7

nginx environment: nginx_1.10.3

Virtual environment: virtualenv 15.1.0

uwsgi version: uwsgi2.0.17.1

Install and enter the project virtual environment:


sudo apt-get install virtualenv
virtualenv -p python3 env_my_project 
source env_my_project/bin/activate
pip install -r requirements.txt 

2. Project configuration and operation test

Modify the project profile:


cp my_project/settings_local.py.example my_project/settings_local.py

Modify the es profile:


cp rs_es/es_settings.py.example rs_es/es_settings.py

wsgi.py:


import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings_local")
application = get_wsgi_application()

Project operation test:


python manage.py collectstatic #  Collecting static files 
python manage.py makemigrations
python manage.py migrate
python manage.py runserver 0.0.0.0:8001

3. Configuration of NGINX and UWSGI


sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/my_project
sudo ln -s /etc/nginx/sites-available/my_project /etc/nginx/sites-enabled/
sudo vim /etc/nginx/sites-enabled/my_project

nginx configuration:


upstream my_project{
 server unix:///var/run/my_project.sock;
}

server {
 listen  8001; // The port number of the service   Services through nginx with uwsgi Communication to start 

 server_name 192.168.xx.xx; //nginx The agent's ip 
 charset  utf-8;

 # max upload size
 client_max_body_size 10M;

 # send all non-media requests to the Django server.
 location / {
  uwsgi_pass my_project;
  include  /etc/nginx/uwsgi_params;
 }

 location /static/ {
  root /home/ubuntu/my_project;
 }
}

Uwsgi configuration:


sudo mkdir /var/log/uwsgi
sudo chmod -R 777 /var/log/uwsgi

uwsgi.ini:
[uwsgi]
chdir=/home/ubuntu/my_project
home=/home/ubuntu/my_project/env_my_project
module=my_project.wsgi:application

socket=/var/run/my_project.sock
chmod-socket = 666

master=True
processes = 5
max-requests=5000

# clear environment on exit
vacuum=True

pidfile=/var/run/my_project.pid
daemonize=/var/log/uwsgi/my_project.log

# git pull  Automatic restart service 
touch-reload=.git/index

4. Configure Emperor mode monitor and the system to start uwsgi automatically

Configure Emperor mode listening


sudo mkdir /etc/uwsgi
sudo mkdir /etc/uwsgi/vassals
sudo ln -s /home/ubuntu/my_project/uwsgi.ini /etc/uwsgi/vassals/

The system starts uwsgi automatically


sudo vim /etc/rc.local
/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals

5. Start the django service via uwsgi

Start the uwsgi


cp my_project/settings_local.py.example my_project/settings_local.py
0

Restart nginx


cp my_project/settings_local.py.example my_project/settings_local.py
1

Start the django service


cp my_project/settings_local.py.example my_project/settings_local.py
2

At this point, the browser can access the service through the ip and port of the ngnix proxy


Related articles: