Django+Uwsgi+Nginx to achieve production environment deployment
- 2021-01-25 07:42:36
- OfStack
uwsgi introduction
uWSGI is an Web server, it implements WSGI protocol, uwsgi, http protocol, etc. The function of HttpUwsgiModule in Nginx is to swap with the uWSGI server.
Note the distinction between WSGI/uwsgi/uWSGI.
WSGI is an Web server gateway interface. It is a specification for an Web server (e.g. nginx, uWSGI, etc.) to communicate with web applications (e.g., programs written with the Flask framework).
uwsgi is a wire protocol rather than a communication protocol, and is often used here for data communication between uWSGI servers and other network servers.
And uWSGI is the implementation of uwsgi and WSGI two protocols Web server.
The uwsgi protocol is a proprietary protocol for the uWSGI server. It is used to define the type of information to be transmitted (type of information). The first 4byte describes the type of information to be transmitted, which is two things compared to WSGI.
Install uwsgi
pip install uwsgi
uwsgi does not support windows
The test start
Create a test file and write:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World! powerde by wsgi'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output.encode('utf8'),]
Execute the command:
uwsgi --http :8080 --file test.py
The browser accesses this port and normally gets output.
Start django with uwsgi
uwsgi --http :8080 --file django_project/wsgi.py
The page can be accessed, but the static file cannot be loaded
uwsgi --http :8080 --file django_project/wsgi.py --static-map=/static=static
Static files are ready to load.
Parameter description:
The http port specifies the IP port as runserver 1 does The file file contains one reflection. If you do not specify Web when calling it, use the default static does one mapping, specifying static files
The uwsgi configuration file starts the django project
ES99en supports a large number of parameters, which can be written in the configuration file. Create the file uwsgi. ini in the project level directory:
# uwsig Start with a configuration file
[uwsgi]
# Project directory
chdir=/opt/webvirtcloud/
# project-specific application
module=webvirtcloud.wsgi:application
# The specified sock The file path of
socket=/tmp/uwsgi.sock
# Number of processes
workers=5
pidfile=/tmp/uwsgi.pid
# The specified IP port
http=0.0.0.0:8080 # If and ngxin Comment out this line
# Specify static files
static-map=/static=/opt/webvirtcloud/static
# Start the uwsgi The user name and user group
uid=root
gid=root
# Enable the main process
master=true
# Automatically remove unix Socket and pid File when the service is stopped
vacuum=true
# Serialize the accepted content, if possible
thunder-lock=true
# Enable the thread
enable-threads=true
# Set the self-interrupt time
harakiri=30
# Set the buffer
post-buffering=4096
# Set the log directory
daemonize=/var/log/uwsgi.log
More parameters: https: / / uwsgi - docs readthedocs. io en/latest/Options html
uwsgi --ini uwsgi.ini, no longer use shell terminal, even if the shell connection is disconnected, the page can still be accessed.
So how do you shut it down or restart it?
uwsgi --stop /tmp/uwsgi.pid
Configuration nginx
Find the Nginx configuration file and write in the virtual domain name:
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080; // Must and uwsgi In the socket The setting of the 1 to
client_max_body_size 35m;
}
}