For Python-based Web projects, common deployment methods are:
- FCGI: Generate a listener process for each project, and then interact with the HTTP service, using a spawn-fcgi or framework-led tool.
- WSGI: Use the HTTP Service's MOD_WSGI module to run each project.
But there is a uwsgi, which neither WSGI nor fcgi agreements, but has created a UWSGI agreement, which the authors say is about 10 times times as fast as the FCGI protocol. The main features of Uwsgi are as follows:
- Super fast performance.
- Low memory footprint (measured at about half of the mod_wsgi of Apache2).
- Multiple app management.
- Detailed logging capabilities (which can be used to analyze app performance and bottlenecks).
- Highly customizable (memory size limit, service Restart after a certain number of times).
Environment Ubuntu 12.04 ip:10.1.6.79
Install Nginx
Apt-get Install Nginx-full Nginx-common
Nginx Configuration/etc/nginx/sites-enabled/example
server {
listen ;
server_name 10.1.6.79;
Access_log /var/log/nginx/example_access.log;
Error_log /var/log/nginx/example_error.log;
Root /var/www/example;
Location/{
uwsgi_pass 127.0.0.1:9001;
Include Uwsgi_params;
Uwsgi_param uwsgi_scheme $scheme;
Uwsgi_param server_software nginx/$nginx _version;
}
Install Uwsgi
Apt-get Install Uwsgi Uwsgi-plugin-python
If you want to install all the Uwsgi plug-ins, you can install the Uwsgi-plugin-all package
UWSGI Configuration/etc/uwsgi/apps-enabled/default.xml
<uwsgi>
<plugin>python</plugin>
<socket>127.0.0.1:9001</socket>
< pythonpath>/var/www/example/app/</pythonpath>
<app mountpoint= "/" >
<script>wsgi_ configuration_module</script>
</app>
<master/>
<processes>4</processes >
<reload-mercy>8</reload-mercy>
<cpu-affinity>1</cpu-affinity>
< max-requests>2000</max-requests>
<limit-as>512</limit-as>
<reload-on-as> 256</reload-on-as>
<reload-on-rss>192</reload-on-rss>
<no-orphans/>
<vacuum/>
</uwsgi>
Parameters in the Uwsgi configuration file can also be specified on the command line via UWSGI, which can be written in INI format in addition to the XML format, after the package is installed in/usr/share/doc/uwsgi/examples/ There are examples of XML and INI format configuration files in the Conffile directory.
wsgi_configuration_module.py Script Content
#!/usr/bin/python
import OS
import sys
sys.path.append ('/var/www/example/app ')
os.environ[' Python_egg_cache '] = '/var/www/example/.python-egg '
def application (environ, start_response):
status = ' 200 OK '
output = ' Hello world! '
Response_headers = [(' Content-type ', ' Text/plain '),
(' Content-length ', str (output))]
Start_response ( Status, Response_headers) return
[Output]
Start Uwsgi
Uwsgi-x/etc/uwsgi/apps-enabled/default.xml--daemonize/var/log/uwsgi/app/default.log
Parameters for Uwsgi:
-M opens master process
-P 4 Opens 4 processes
The port or socket address used by-s
-D Use Daemon, note that after use-D, you need to add log file address, such as-d/var/log/uwsgi.log
-R 10000 opens 10,000 processes, automatically respawn under
-T 30 set timeout for 30s, after timeout, automatically discard the link
–limit-as 32 Controls The total memory of the process at 32M
-X using configuration file mode
Concurrent 4 Threads
Uwsgi-s: 9090-w myapp-p 4
Main control thread + 4 threads
Uwsgi-s: 9090-w myapp-m-P 4
Executes more than 30 seconds of client direct waiver
Uwsgi-s: 9090-w myapp-m-P 4-t 30
Limit Memory Space 128M
Uwsgi-s: 9090-w myapp-m-P 4-t--limit-as 128
Service over 10,000 req automatic respawn
Uwsgi-s: 9090-w myapp-m-P 4-t--limit-as 128-r 10000
Background running and so on
Uwsgi-s: 9090-w myapp-m-P 4-t--limit-as 128-r 10000-d uwsgi.log
In addition to booting directly with the UWSGI command, you can start with a script under INIT.D, but first modify the path to the default profile in/etc/default/u Wsgi, and then start by/etc/init.d/uwsgi start
#INHERITED_CONFIG =/usr/share/uwsgi/conf/default.ini
Inherited_config=/etc/uwsgi/apps-enabled/default.xml
Start Nginx
The effect is as follows:
Test Uwsgi is available
test Script test.py
#!/usr/bin/python
def application (env,start_response):
start_response (' OK ', [(' Content_Type ', ' text/ HTML]) return
"Congraduation!!! UWSGI testing OK!!!
#启动web server
uwsgi--http:9090--wsgi-file test.py
Browser input IP: port: 192.168.1.99:9090
You can see "Congraduation!!! UWSGI Testing OK!!! "
Summary
Uwsgi is actually an HTTP server, but it only targets Python Web applications. Although Uwsgi is also an HTTP server, it cannot be used directly to deploy a Python Web application, or there will be an error.
In this article, Uwsgi plays the role of a back-end HTTP server, Nginx plays the role of front-end HTTP server, hello.py is the client application. The user sends a request from the Web browser, the Nginx server receives the request, passes the user's request to the UWSGI server through its UWSGI module, UWSGI the server processing completes returns the result to the Nginx, the browser will display the final result to the user.