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
- Configure ports for http80 and Django services
- Configure uwsgi to start Django service
- Create in the project root directory
uwsgi.ini
Documents,vi uwsgi.ini
Enter 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 documents,Nginx 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.d
Folder, 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 service
service nginx restart
Problems encountered and Solutions
Problem 1: nginx static resource file cannot be accessed, 403 Forbidden error
Solution: innginx.conf
Configuration 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 terminal
python manage.py collectstatic
solve. The premise is Xu Zaisettings.py
File configuration is goodSTATIC_ROOT = os.path.join(BASE_DIR, "static")
。