Detailed steps to start the Django project with nginx+uwsgi

Detailed steps to start the Django project with nginx+uwsgi

nginx+uwsgi+django is our commonly used Django deployment method. As the front-end server, nginx is responsible for receiving all client requests. The requested static files are processed by the nginx server itself because it has a good ability to handle static files, has optimized performance, and supports high concurrency. The uWSGI server is a support server that is used to serve nginx. nginx hands the requested dynamic files to uWSGI for processing. uWSGI implements the uwsgi, wsgi and http protocols. The uwsgi protocol is a custom protocol of uWSGI, which 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: virtualenv15.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 configuration file:

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

Modify the es configuration file:

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 running test:

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

3. NGINX and UWSGI related configuration

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 service port number service is started through nginx and uwsgi communication server_name 192.168.xx.xx; //The ip of nginx proxy 
 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 automatically restarts the service touch-reload=.git/index

4. Configure Emperor mode monitoring and system automatic startup of uwsgi

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 automatically starts uwsgi

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

5. Start Django service through uwsgi

Start uwsgi

uwsgi --ini uwsgi.ini

Restart nginx

sudo service nginx restart

Start the Django service

sudo uwsgi --reload /var/run/my_project.pid

At this point, you can access the service through the ip and port proxy from ngnix in the browser

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed tutorial on building django+nginx+uwsgi on Ubuntu
  • Detailed explanation of the best practice of Django+uwsgi+Nginx online
  • Detailed deployment of Django uwsgi Nginx in production environment
  • How to deploy Django service nginx+uwsgi on Centos
  • Detailed explanation of Django+Uwsgi+Nginx to achieve production environment deployment
  • Detailed explanation of the production environment deployment of Django+Uwsgi+Nginx
  • Solve all the problems of deploying Django with nginx+uwsgi (summary)
  • How to implement production environment deployment with Django+Uwsgi+Nginx

<<:  Vue implements a visual drag page editor

>>:  MySQL 5.7.11 zip installation and configuration method graphic tutorial

Recommend

HTML form submission method case study

To summarize the form submission method: 1. Use t...

Detailed example of using useState in react

useState useState adds some internal state to a c...

Examples of correct judgment methods for data types in JS

Table of contents Preface Can typeof correctly de...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

Docker container log analysis

View container logs First, use docker run -it --r...

Learn MySQL database in one hour (Zhang Guo)

Table of contents 1. Database Overview 1.1 Develo...

Key features of InnoDB - insert cache, write twice, adaptive hash index details

The key features of the InnoDB storage engine inc...

Native js to implement a simple calculator

This article example shares the specific code of ...

JavaScript imitates Jingdong magnifying glass effect

This article shares the specific code for JavaScr...

WeChat applet selects the image control

This article example shares the specific code for...

HTML&CSS&JS compatibility tree (IE, Firefox, Chrome)

What is a tree in web design? Simply put, clicking...

How to use echarts to visualize components in Vue

echarts component official website address: https...

Pure CSS3 to achieve mouse over button animation Part 2

After the previous two chapters, do you have a ne...

Pure CSS to achieve three-dimensional picture placement effect example code

1. Percentage basis for element width/height/padd...