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

Detailed explanation of encoding issues during MySQL command line operations

1. Check the MySQL database encoding mysql -u use...

HTML pop-up div is very useful to realize mobile centering

Copy code The code is as follows: <!DOCTYPE ht...

Detailed tutorial on VMware installation of Linux CentOS 7.7 system

How to install Linux CentOS 7.7 system in Vmware,...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

JavaScript canvas to achieve meteor effects

This article shares the specific code for JavaScr...

jQuery implements the function of adding and deleting employee information

This article shares the specific code of jQuery t...

How to install Nginx in Docker

Install Nginx on Docker Nginx is a high-performan...

How to use Axios asynchronous request API in Vue

Table of contents Setting up a basic HTTP request...

Why should MySQL fields use NOT NULL?

I recently joined a new company and found some mi...

Using JavaScript to implement carousel effects

This article shares the specific code for JavaScr...

TypeScript namespace explanation

Table of contents 1. Definition and Use 1.1 Defin...

Two ways to clear table data in MySQL and their differences

There are two ways to delete data in MySQL: Trunc...