Rapid Deployment of Python applications: Nginx + uWSGI configuration details

Source: Internet
Author: User
Compared with PHP, deploying Python applications is troublesome. fcgi and wsgi are commonly used. However, these two methods are a headache. This article introduces a simple Nginx + uwsgi method to quickly deploy Python applications. In PHP, deployment is the most convenient. you only need to drop the PHP file to a path that supports PHP,

Compared with PHP, deploying Python applications is troublesome. fcgi and wsgi are commonly used. However, these two methods are a headache. This article introduces a simple Nginx + uwsgi method to quickly deploy Python applications.
In PHP, deployment is the most convenient. you only need to drop the php file to the path supporting 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; basically, my applications were used by different projects, not to mention the management chaos, which was also detrimental to the load. idle projects and busy projects also needed 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.

◆ Low memory usage (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, restart after a certain number of services, etc ).

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}: 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 frame. 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 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: the client that executes the command for more than 30 seconds directly gives up:

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 service more than 10000 req automatic respawn:

Uwsgi-s: 9090-w myapp-M-p 4-t 30 -- limit-as 128-R 10000 background run and so on:

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 must then be configured with virtualenv. virtualenv is a useful virtual environment tool for Python. install it like this:

Apt-get install Python-setuptools easy_install virtualenv and then set 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 partition; 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 packages/var/www/myappdir1;} server {listen 80; server_name servers; location/{include uwsgi_params; uwsgi_pass 127.0.0.1: 9090; define UWSGI_PYHOME/var/www/myenv; then UWSGI_SCRIPT myapp2; uwsgi_param UWSGI_CHDIR/var/www/myappdir2 ;}} restart the nginx service and the two sites can share a uwsgi service.

5. Practical applications

After the initial settings are complete, you only need to make a few modifications to the added application in Nginx. you can deploy the application immediately without restarting uwsgi. Uwsgi comes with a django-based tool to monitor the running status of uwsgi. you can use it to deploy it:

Server {listen 80; root/var/www/django1.23; index index.html index.htm; server_name uwsgiadmin.django.obmem.info; access_log/var/log/nginx/django. access. log; location/media/{root/var/www/django1.23/adminmedia; rewrite ^/media /(. *) $/$1 break ;}location/{include uwsgi_params; uwsgi_pass 127.0.0.1: 9090; uwsgi_param UWSGI_PYHOME/var/www/django1.23/vtenv; uwsgi_param UWSGI_CHDIR/var/www/django1.23/uwsgiadmin; uwsgi_param UWSGI_SCRIPT uwsgiadmin_wsgi ;}} The uwsgi monitoring information can be found in http://uwsgiadmin.django.obmem.info As you can see (the username and password are all admin ). Another example is the deployment of the LBForum Forum program: after installation is complete, modify the configuration file according to the deployment instructions, and then modify the nginx configuration file:

Server {listen 80; root/var/www/django1.23; index index.html index.htm; server_name lbforum.django.obmem.info; access_log/var/log/nginx/django. access. log; location/{& nb

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.