The relationship between uwsgi and nginx is not discussed here, but learning is recommended
Generally speaking, Nginx can ensure load balance when your web page supports more requests,
Simple web pages can also meet the requirements with uwsgi deployment and django runserver, so the load capacity is
Nginx > uwsgi > local django runserver
(your server needs to install Python, pip and nginx and start the environment before installing uwsgi) - this step precedes the first one
The project enters the deployment state ALLOWED_HOST = [’*’]
DEBUG=False or something
1. First, you need to configure the virtual environment virtualenv on the server
Note (only Python 2.7 and later support virtualenv)
If Python 2
After navigating to your project folder
virtualenv -p /path/to/new/virtual/environment venv
If Python 3
python3 -m venv /path/to/new/virtual/environment
Running this command will create a new folder environment
And the virtual environment is named venv
There will be three folders in the created new folder, of which bin is the starting directory for starting the virtual environment in the future
If you need to delete the virtual environment, just delete the root directory of the virtual environment directly
As follows: use the source command to turn on and turn off using deactivate [venv name]. If you want to turn off the initial [base] environment, use conda deactivate
source /path/to/new/virtual/environment/bin/activate
Note that you must remember to install uwsgi for the virtual environment
pip install uwsgi
Next, it is also important to install the configuration file for the virtual environment (that is, the virtual environment installed in pycharm for your project)
cd to the local project folder or pycharm terminal
pip freeze > requirements.txt
If there is a problem (I have a problem with the package of - ip 24, just search and delete it on Baidu) you need to query each package with a problem
Then check the requirements Txt format
It's probably like this. The package = = version number must be exactly the same. PIP freeelist can't
asgi-redis==1.4.3 asgiref==3.3.4 asttokens==2.0.4 async-timeout==2.0.1
Then scp -r uploads the local project to the server
Then start the virtual environment on the server and check the pip version and python version
source /path/to/new/virtual/environment/bin/activate
Then install the dependency (there may be some problems in this step. It is mainly domain problems, so pychart can't be lazy when using it. It must be a project and an environment
Otherwise, too many packages always have package versions that are not supported by the domain. Try to use the image separately for the problematic package, and set http to safe source
What others wrote on csdn is too detailed (I'm sorry to copy)
pip install -r requirements.txt
If present
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: UNKNOWN_PROTOCOL] unknown protocol
Because PIP version = = 20.3.3 (latest version) has fully used SSL authentication. If the server is http, consider demoting pip to 20.2.2
2. Configure uwsgi ini
Under the root directory of django project (the same level as manage.py directory)
Test uwsgi connection
First create a py test file
# /path/to/django/project/test.py def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
cd to the / path/to/django/project / command line, enter the following
uwsgi --http :8001 --wsgi-file test.py
The browser inputs your server URL plus port 8001. If you see Hello World on the page, it indicates that uwsgi configuration is successful
If you are prompted with Internal Server Error
- Check the version of uwsgi to see if it is installed in the virtual environment
- Check test Is py running the correct one
Next, formally configure the uwsgi of the Django project INI file. To start this file, you need to modify some previous commands, because the entry used is module
uwsgi --ini uwsgi.ini
New uwsgi INI file
[uwsgi] socket = 127.0.0.1:6667 chdir = /path/to/django/project/[Yours django Project name] module = [Yours django Project name].wsgi master = true processes = 10 threads = 2 max-requests = 2000 chmod-socket = 664 pidfile= /path/to/django/project/[Yours django Project name]/[Yours django Project name].pid vacuum = true py-autoreload = 1 daemonize = /path/to/django/project/[Yours django Project name]/log/uwsgi.log
Common options:
http: protocol type and port number
Processes: number of open processes
Workers: the number of open processes, which is equivalent to processes (the official website says spawn the specified number of workers / processes)
Chdir: chdir to specified directory before apps loading
WSGI file: load WSGI file (load. WSGI file)
Stats: enable the stats server on the specified address
Threads: run threads. Due to the existence of GIL, I think this is really useless. (run each worker in prethreaded mode with the specified number of threads)
Master: enable master process
Daemon: make the process run in the background and print the log to the specified log file or udp server (daemon uwsgi). In fact, the most common is to output the operation records to a local file. (you need to create a log folder manually). Otherwise, an error is reported
pidfile: specify the location of the pid file and record the pid number of the main process.
vacuum: automatically clean up the environment when the server exits, and delete unix socket files and pid files (try to remove all of the generated files / sockets)
uwsgi --ini uwsgi.ini
3. Create nginx Conf configuration file
It is also in the root directory of django project (the same level as manage.py)
This is my configuration
events { worker_connections 1024; } http { include /usr/local/nginx/conf/mime.types; default_type application/octet-stream; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 10110; server_name localhost; charset utf-8; access_log /path/to/django/project/[Yours django Project name]/log/nginx_access.log; error_log /path/to/django/project/[Yours django Project name]/log/nginx_error.log; large_client_header_buffers 4 16k; # Maximum number and size of buffers to read large client request headers client_max_body_size 10000m; #Set the maximum request body size that nginx can handle. client_body_buffer_size 128k; #Buffer size of the request body. proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 64k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; location /static { alias /path/to/django/project/[Yours django Project name]/static; # New static file collection address } location / { uwsgi_send_timeout 600; # Specify the timeout for transmitting the request to uWSGI, and the timeout for transmitting the request to uWSGI after the handshake is completed. uwsgi_connect_timeout 600; uwsgi_read_timeout 600; include /usr/local/nginx/conf/uwsgi_params; uwsgi_pass 127.0.0.1:8208; } } }
Keep it simple
listen 88; # Port different from uwsgi setting server_name www.wcwnina.com; # Remember to add the mapping between IP and domain name in the / etc/hosts file of the system! location / { include uwsgi_params; # With nginx Conf same directory uwsgi_pass 192.168.1.2:8000; # Consistent with socket in uwsgi configuration } location /static { alias /path/to/django/project/[Yours django Project name]/static; }
Restart nginx after creation (modification)
nginx -s reload
4. Collect static files
It is also in the root directory of django project (the same level as manage.py)
python manage.py collectstatic
5. Run uwsgi again
It is also in the root directory of django project (the same level as manage.py)
uwsgi --ini uwsgi.ini
If you encounter problems, you need to check nginx in the log folder of the project directory_ error. log
You can use the following to see the last 10 lines and new information, and then specify Baidu according to specific problems
tail -f nginx_error.log
This article omits the installation requirements of pip and the problems encountered in installing other packages
You can start with the domain proxy pip proxy of python nginx pip Version (I won't be more difficult...)
I hope your process can be smooth sailing, otherwise the specific problems will be specific
If you encounter a problem, you need to restart, find the pid of the process, close it with kill-9, and then try again
ps -ef | grep [project name] or [: port number] ps: see the: before the port number
lsof -i: port number
kill -9 [pid]