In PHP, deployment is the most convenient. You only need to drop the PHP file to a path that supports PHP, and then access the path to use it. No matter how many PHP applications are added to the host, as long as you change the directory, no matter how php-cgi runs, deployment is extremely convenient.
On the other hand, Python is really a headache. Common deployment methods include:
◆ Fcgi: Use the spawn-fcgi or tool provided by the Framework to generate listening processes for each project and then interact with the http service.
◆ Wsgi: Use the mod_wsgi module of the http service to run various projects.
No matter which one is very troublesome, It is very troublesome to configure mod_wsgi for apache, and the memory usage is large. If nginx is to be added as the server for static pages, it will be more troublesome; most of my applications were subsequently divided into different projects, not to mention the management chaos, which is also unfavorable to the load. Idle projects and busy projects also need to occupy the memory.
If there is something in Python that listens to the same port like php-cgi for unified management and load balancing, it can save a lot of deployment effort. When I saw uWSGI, I found that I never knew that it was so convenient to deploy the tool in a unified manner. UWSGI, neither wsgi nor fcgi, is a self-built uwsgi protocol. It is said that the Protocol is about 10 times faster than the fcgi protocol. For more information, see:
UWSGI has the following features:
◆ Ultra-fast performance.
◆ The low memory usage is about half of mod_wsgi of apache2 ).
◆ Manage multiple apps.
◆ Detailed log functions can be used to analyze app performance and bottlenecks ).
◆ Highly customizable memory size limit, which can be restarted after a certain number of times of service ).
Officially started
Although many uwsgi documents are detailed, here is the official uwsgi documentation: http://projects.unbit.it/uwsgi/wiki/doc.
1. Install uwsgi
Ubuntu has uwsgi ppa:
- add-apt-repository ppa:stevecrozz/ppa
- apt-get update
- apt-get install uwsgi
2. Replace mod_wsgi with uwsgi
The overall configuration of Nginx is long. I will not talk about it here. If you already understand the basic configuration of Nginx, then uwsgi is similar to this Configuration:
- location / {
- include uwsgi_params
- uwsgi_pass 127.0.0.1:9090
- }
This is to send all URLs to the uwsgi Protocol Program on port 9090 for interaction. Create myapp. py in the project directory so that the application calls the wsgi interface of the Framework. For example, web. py is:
- ......
- app = web.application(urls, globals())
- appapplication = app.wsgifunc()
For example, django is:
- .......
- from django.core.handlers.wsgi import WSGIHandler
- application = WSGIHandler()
Then run uwsgi listening 9090, where-w is followed by the module name, that is, the configured myapp
- uwsgi -s :9090 -w myapp
The website has been deployed.
3. uwsgi Parameters
The above is the simplest deployment of a single project. uwsgi still has many commendable features, such:
Four concurrent threads:
- uwsgi -s :9090 -w myapp -p 4
Main Control thread + 4 threads:
- uwsgi -s :9090 -w myapp -M -p 4
If the client executes the command for more than 30 seconds, the following command is skipped:
- uwsgi -s :9090 -w myapp -M -p 4 -t 30
Limited Memory Space 128 M:
- uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128
Automatic respawn with over 10000 req services:
- uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000
Background running:
- uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log
4. Configure multiple sites for uwsgi
To allow multiple sites to share a uwsgi service, you must run uwsgi as a virtual site: Remove "-w myapp" and "-vhost ":
- uwsgi -s :9090 -M -p 4 -t 30 --limit-as 128 -R 10000 -d uwsgi.log --vhost
Virtualenv must be configured. virtualenv is a useful virtual environment tool for Python:
- apt-get install Python-setuptools
- easy_install virtualenv
Set up one or more app benchmark environments:
- virtualenv /var/www/myenv
Application environment. The software installed in this environment is only valid in this environment:
- source /var/www/myenv/bin/activate
- pip install django
- pip install mako
- ...
Finally, configure nginx. Note that each site must occupy one server separately. The same server is directed to different applications in different locations and may fail for some reason. This is a bug.
- server {
- listen 80;
- server_name app1.mydomain.com;
- location / {
- include uwsgi_params;
- uwsgi_pass 127.0.0.1:9090;
- uwsgi_param UWSGI_PYHOME /var/www/myenv;
- uwsgi_param UWSGI_SCRIPT myapp1;
- uwsgi_param UWSGI_CHDIR /var/www/myappdir1;
- }
- }
- server {
- listen 80;
- server_name app2.mydomain.com;
- location / {
- include uwsgi_params;
- uwsgi_pass 127.0.0.1:9090;
- uwsgi_param UWSGI_PYHOME /var/www/myenv;
- uwsgi_param UWSGI_SCRIPT myapp2;
- uwsgi_param UWSGI_CHDIR /var/www/myappdir2;
- }
- }
In this way, restart the nginx service and the two sites will be able to share a uwsgi service.