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/ my_project/

Modify the es profile:

cp rs_es/ rs_es/

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 collectstatic #  Collecting static files 
python makemigrations
python migrate
python runserver

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


chmod-socket = 666

processes = 5

# clear environment on exit


# git pull  Automatic restart service 

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/ my_project/

Restart nginx

cp my_project/ my_project/

Start the django service

cp my_project/ my_project/

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

Related articles: