Detailed steps for nginx+uwsgi to start the Django project
When we are developing the web project with django, we use the test server that comes with django in the development and testing process. Due to its security and stability and other performance limitations, django officials do not recommend using the test server in actual production.
Nginx+uwsgi+django is our common way of deploying django. Nginx, as the front-end server, is responsible for receiving all client requests, and the requested static files are completed by the nginx server itself, because it has a good ability to deal with static files, performance has been optimized, and supports high concurrency; uWSGI server, as a support server, is used to serve nginx, and nginx sends the requested dynamic files to uWSGI for processing. UWSGI implements uwsgi, wsgi and http protocols. Uwsgi protocol is a custom protocol of uWSGI and defines the interface between django and server.
1. Install the project environment
System environment: ubuntu16.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 virtualenvvirtualenv-p python3 env_my_project source env_my_project/bin/activatepip install-r requirements.txt
two。 Project configuration and running tests
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 osfrom django.core.wsgi import get_wsgi_applicationos.environ.setdefault ("DJANGO_SETTINGS_MODULE", "my_project.settings_local") application = get_wsgi_application ()
The project runs the test:
Python manage.py collectstatic # collect static files python manage.py makemigrationspython manage.py migratepython 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_projectsudo 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 port number service of the service communicates with uwsgi through nginx to start server_name 192.168.xx.xx; / nginx agent ip 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/uwsgisudo chmod- R777 / var/log/uwsgiuwsgi.ini: [uwsgi] chdir=/home/ubuntu/my_projecthome=/home/ubuntu/my_project/env_my_projectmodule=my_project.wsgi:applicationsocket=/var/run/my_project.sockchmod-socket = 666master=Trueprocesses = 5max-requests=5000# clear environment on exitvacuum=Truepidfile=/var/run/my_project.piddaemonize=/var/log/uwsgi/my_project.log# git pull automatically restart the service touch-reload=.git/index
4. Configure Emperor mode snooping and the system automatically starts uwsgi
Configure Emperor mode snooping
Sudo mkdir / etc/uwsgisudo mkdir / etc/uwsgi/vassalssudo 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 the 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, the service can be accessed in the browser through the ip and port proxied by ngnix
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.