Rapid deployment of Python applications: Nginx + uWSGI configuration details (1)

Source: Internet
Author: User
Tags virtual environment virtualenv

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:

 
 
  1. add-apt-repository ppa:stevecrozz/ppa  
  2. apt-get update  
  3. 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:

 
 
  1. location / {  
  2.   include uwsgi_params  
  3.   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:

 
 
  1. ......  
  2. app = web.application(urls, globals())  
  3. appapplication = app.wsgifunc() 

For example, django is:

 
 
  1. .......  
  2. from django.core.handlers.wsgi import WSGIHandler  
  3. application = WSGIHandler() 

Then run uwsgi listening 9090, where-w is followed by the module name, that is, the configured myapp

 
 
  1. 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:

 
 
  1. uwsgi -s :9090 -w myapp -p 4 

Main Control thread + 4 threads:

 
 
  1. uwsgi -s :9090 -w myapp -M -p 4 

If the client executes the command for more than 30 seconds, the following command is skipped:

 
 
  1. uwsgi -s :9090 -w myapp -M -p 4 -t 30 

Limited Memory Space 128 M:

 
 
  1. uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 

Automatic respawn with over 10000 req services:

 
 
  1. uwsgi -s :9090 -w myapp -M -p 4 -t 30 --limit-as 128 -R 10000 

Background running:

 
 
  1. 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 ":

 
 
  1. 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:

 
 
  1. apt-get install Python-setuptools  
  2. easy_install virtualenv 

Set up one or more app benchmark environments:

 
 
  1. virtualenv /var/www/myenv 

Application environment. The software installed in this environment is only valid in this environment:

 
 
  1. source /var/www/myenv/bin/activate  
  2. pip install django  
  3. pip install mako  
  4. ... 

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.

 
 
  1. server {  
  2.         listen       80;  
  3.         server_name  app1.mydomain.com;  
  4.         location / {  
  5.                 include uwsgi_params;  
  6.                 uwsgi_pass 127.0.0.1:9090;  
  7.                 uwsgi_param UWSGI_PYHOME /var/www/myenv;  
  8.                 uwsgi_param UWSGI_SCRIPT myapp1;  
  9.                 uwsgi_param UWSGI_CHDIR /var/www/myappdir1;  
  10.         }  
  11.     }  
  12.     server {  
  13.         listen       80;  
  14.         server_name  app2.mydomain.com;  
  15.         location / {  
  16.                 include uwsgi_params;  
  17.                 uwsgi_pass 127.0.0.1:9090;  
  18.                 uwsgi_param UWSGI_PYHOME /var/www/myenv;  
  19.                 uwsgi_param UWSGI_SCRIPT myapp2;  
  20.                 uwsgi_param UWSGI_CHDIR /var/www/myappdir2;  
  21.         }  
  22.     } 

In this way, restart the nginx service and the two sites will be able to share a uwsgi service.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.