Nginx + uwsgi deployment Django project

Time:2022-1-8

Project environment

Server: alicloud ECS; System version: CentOS 8 0 Nginx version: 0.14.1; Uwsgi version: 2.0.18; Python version: 3.6.0.

Project dependencies and versions are as follows:

Django==2.0
django-mdeditor==0.1.17.1
django-model-utils==4.0.0
django-notifications-hq==1.6.0
Markdown==3.2.1
PyMySQL==0.9.3

Log in to the alicloud console and open the port

  1. Configure ports for http80 and Django services
  2. Configure uwsgi to start Django service
  • Create in the project root directoryuwsgi.iniDocuments,vi uwsgi.iniEnter the following:
[uwsgi]
#Use when using nginx connection
socket=127.0.0.1:8000
#Directly as a web server, use Python manage py runserver ip:port
#http=0.0.0.0:8000
#Project directory
chdir=/root/project/myblog
#WSGI The directory of the PY file, relative to the project directory
wsgi-file=myblog/wsgi.py
#Specifies the number of worker processes to start
processes=2
#Specifies the number of threads in the worker process
threads=2
#Specifies that there is a main process in these processes
master=True
#Save the PID of the main process after startup
pidfile=uwsgi.pid
#Set uwsgi background operation, uwsgi Log save log information
daemonize=uwsgi.log
#Set the maximum number of bytes of the log file
log-maxsize = 100000
#Set the maximum number of requests per process
max-requests = 1000
#Set the path of the virtual environment
virtualenv=/root/anaconda3/envs/myblog

Also refer toDjango official documentsNginx Chinese documentCustom configuration.

  • Uwsgi command
Start service: uwsgi -- ini uwsgi ini
Stop service: uwsgi -- stop uwsgi pid

Configure nginx to start the static file service

  • get into/etc/nginx/conf.dFolder, create your own project profilemyproject.conf, enter the configuration below.
server {
    listen 80;
        server_name ip;
        charset utf-8;
        client_max_body_size 75M;
        location /static {
        alias /root/project/myblog/static;
    }

    location /media {
        alias /root/project/myblog/media;
    }

    location / {
        uwsgi_pass 127.0.0.1:8000;
        include /etc/nginx/uwsgi_params;
    }

}
  • Restart the nginx serviceservice nginx restart

Problems encountered and Solutions

Problem 1: nginx static resource file cannot be accessed, 403 Forbidden error
Solution: innginx.confConfiguration file header plususer root

user root;
worker_processes 8;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
    worker\_connections 1024;
}
... ...

If the background site style cannot be displayed, it is because the static file collection operation is not carried out. You can enter it at the terminalpython manage.py collectstaticsolve. The premise is Xu Zaisettings.pyFile configuration is goodSTATIC_ROOT = os.path.join(BASE_DIR, "static")

Recommended Today

Proper memory alignment in go language

problem type Part1 struct { a bool b int32 c int8 d int64 e byte } Before we start, I want you to calculatePart1What is the total occupancy size? func main() { fmt.Printf(“bool size: %d\n”, unsafe.Sizeof(bool(true))) fmt.Printf(“int32 size: %d\n”, unsafe.Sizeof(int32(0))) fmt.Printf(“int8 size: %d\n”, unsafe.Sizeof(int8(0))) fmt.Printf(“int64 size: %d\n”, unsafe.Sizeof(int64(0))) fmt.Printf(“byte size: %d\n”, unsafe.Sizeof(byte(0))) fmt.Printf(“string size: %d\n”, […]