Introduction: see reference blog
WSGI is a Web Server Gateway Interface. It is a specification that describes how how a Web server linked together to process a request, the communication with the Web application, and a Web application (reception request, process the request, the response request)
A deployment project .crm (non-isolated front and rear end items)
1. Project deployment technology stack
nginx + uwsgi + django + virtual environment + supervisor + mysql
2. face questions 1
django way to start python3 manage.py runserver why not use in a production environment?
django just a web logical framework, it starts by python3 manage.py runserver command socket server
by means of a built-in python wsgi framework wsgiref framework to achieve
wsgi module is a stand-alone, single-process module, low performance, to start with a wsgi multi-threaded, multi-process project is not appropriate
hence the need for on-line written by c language uwsgi this web server software, start django
way uwsgi support for multi-process multi-threaded, high performance, better start django performance, but uwsgi does not resolve the static files django
3. face questions 2
Why use nginx and uwsgi?
nginx can solve the problem of static files uwsgi, and support high concurrent requests, but also limiting to protect the back-end server security, and so
why should uwsgi
because uwsgi support multi-threaded multi-process mode, performance is very high, better start django performance, but uwsgi not resolve django static files
in future we will collect all the items django static files, threw to deal with nginx
4. Prepare codes crm
The path into the first layer, the file view wsgi.py
vim wsgi.py
from this document can be seen calling django of wsgi, acquiring function get_wsgi_application
then generates an application object by calling this function
during the time uwsgi start django, is the need to find this file wsgi.py module
If you are prompted an error I can not find application, because uwsgi not find wsgi.py
6. Create a virtual environment
Prepare a new virtual environment, to start the crm, virtualenvwrapper use to create a new virtual environment
virtualenvwrapper command tool 1 . Create a new virtual environment mkvirtualenv MyCRM 2 . switching multiple virtual environments workon name of the virtual environment 3 . List all of the virtual environment directory lsvirtualenv 4 . enter the directory currently active virtual environment cdvirtualenv
1 . To solve the problem of virtual environment dependent, that is, to protect the local environment and develop online issue of consistency in local input through windows cmd window PIP3 Freeze > requirements.txt # python3 will be able to export all of the module interpreter to requirements. txt this file 2 . requirements.txt this file transfer to the server, install it in a virtual environment, it can automatically solve all the problems module PIP3 install -r requirements.txt
7. under virtual environment installation uwsgi
Ensure access to the virtual environment, and then install uwsgi
workon nginx_crm
download uwsgi
PIP3 install uwsgi
8. Use the command to start django project uwsgi and thermal load
uwsgi support hot load of startup mode command is as follows uwsgi --http: 8000 --module mysite.wsgi --py-autoreload = 1 uwsgi django command to start the project as follows: uwsgi --http: 8088 - Module Crm_Project.wsgi - http specify the http protocol port to start in 8088 - catalog items found second layer module, inside wsgi.py file if you want to start a http page, provide test access, use - http parameters if you combine nginx reverse agent, you must use --socket parameters uwsgi --socket: 8000 --module Crm_Project.wsgi
Introducing 9.uwsgi.ini profile
Due to excessive parameters, here to introduce the concept called uwsgi.ini profile on this item file path of the first layer starts crm profile as follows uwsgi.ini [to uwsgi] # the Django - Related Settings # The Base Directory ( full path) the absolute path # fill items (first layer path) the chdir = / opt / Crm_Project # the Django ' S WSGI file # crm fill wsgi.py files in the directory path of the second layer Module1 = Crm_Project.wsgi # the the virtualenv (Full path) # fill in the virtual environment of absolute path Home = / root / Envs / nginx_crm # Process - Related Settings # Master Master = to true Number The worker Processes maximum of # # uwsgi based multi-process, according to the cpu to optimize Processes = 4 # at The socket (use at The Full path to BE Safe # If you use nginx reverse proxy, fill out socket parameter # If you use the nginx reverse proxy, fill out socket parameter # If you use nginx reverse proxy, fill out socket parameter # If you use nginx reverse proxy, fill out socket parameter # If you use nginx reverse proxy, fill out socket parameters socket = 0.0 . 0.0 : 8000 #, if you have not nginx, would like to direct a browser to test the back end, using HTTP #http = 0.0 . 0.0 : 8000 # Appropriate ... with the Permissions - May BE needed # chmod -socket = 664 # clear environment on exit vacuum = true
uwsgi support ini, XML, and other configurations, paper ini example, the / etc / new directory uwsgi_nginx.ini, add the following configuration: # File mysite_uwsgi.ini [uwsgi] # the Django - Related Settings # The Base Directory ( path Full) the chdir = / opt / the mysite # the Django ' S WSGI File Module1 = mysite.wsgi # The the virtualenv (Full path) Home = / opt / Venv # Process - Related Settings # Master Master = to true # Number of maximum worker Processes Processes = 1 # the socket (use the full path to be safe socket = 0.0.0.0:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true uwsgi.ini
10. At this time, the boot code by way uwsgi --ini uwsgi.ini
11.django static files and configuration nginx
Virtual environment settings.py edit item written following parameters STATIC_ROOT = ' / opt / crmstatic '
12. Run, collect all of the static files crm
Command is as follows
python3 manage.py collectstatic
# At this point the back-end configuration is completed! ! ! ! Began to engage in nginx
# backend configuration is completed at this time! ! ! ! Began to engage in nginx
# backend configuration is completed at this time! ! ! ! Began to engage in nginx
# backend configuration is completed at this time! ! ! ! Began to engage in nginx
# backend configuration is completed at this time! ! ! ! We began to engage in nginx
13. Configure nginx, first, reverse proxy support, the second is support for static load file
Configuring nginx, first, reverse proxy support, the second is support for static file is loaded modify nginx.conf following code # when users access www.s18dnf.com, you enter the virtual host LOCATION / { uwsgi_pass 0.0 . 0.0 : 8000 ; # 8000 port django running include uwsgi_params; # fixed written } # this location is to solve the problem of static files crm location / static { #alias parameter is the / static into / opt / crmstatic, will find all of our static file Alias / opt / crmstatic; # here crmstatic is STATIC_ROOT = '/ opt / crmstatic' this
}
14. Restart nginx, due to the heat load, the project has been launched
15. The batch management software start-stop supervisor
supervisor is a python-based task management tool used to automatically run a variety of background tasks, of course, you can directly use the nohup command makes the task automatically run in the background, but if you want to restart the task, each task to manually kill off the process, so it is tedious,
but once the program error caused the process to exit, the system can not automatically reload task.
16. Install supervisor
Installation supervisor in a non-virtual environment supervisor python2 can only be used to install, can not be used python3 to install
1 install easy_install. PIP3 is python3 package management tools easy_install is python2 package management tool yum install Python - setuptools 2 . Installation supervisor easy_install supervisor
17.supervisor profile
1. Create a supervisor configuration file, enter the following code to the project path below the first layer echo_supervisord_conf > / etc / supervisor.conf 2. Modify supervisor configuration file, add the command to start uwsgi supervisor is actually help we carried out, the need to manually perform the only command vim / etc / supervisor.conf write the following, written at the bottom [Program: s18crm] the command = / root / Envs / nginx_crm / bin / uwsgi --ini / opt / Crm_Project / uwsgi.ini stopasgroup = to true killasgroup = to true
18. The test starts with a supervisor uwsgi
Testing started with the supervisor uwsgi, has been taken to ensure that this time uwsgi exit . Uwsgi start with a test supervisor, has been taken to ensure that this time uwsgi exit . Uwsgi start with a test supervisor, has been taken to ensure that this time uwsgi exit test starts with a supervisor uwsgi , taken to ensure that this time uwsgi has withdrawn from the virtual environment, enter the following two commands supervisord -c / etc / supervisor.conf # this is the command to start the server supervisor supervisorctl -c / etc / supervisor.conf # this is the supervisor clients management command command learning supervisor's status s18crm # check the status of # s18crm from [Program: s18crm] start s18crm # start task stop s18crm # stop the task start all all tasks start and stop stop all #