Nginx+uwsgi+flask Building Python-web Applications

Source: Internet
Author: User

Flask itself can start the HTTP server directly, but is limited by the management, deployment, performance and other issues, in the production environment, we generally do not use the flask itself with the HTTP server.

From the existing practice, for flask, a better way to deploy is to use UWSGI do Wsgi container, nginx do front-end server. The benefits of doing this are:

1. Uwsgi good performance, provide a lot of functions, operation and maintenance convenience.
2. Nginx for the static file processing is good, and the default support UWSGI protocol, load balancing and pressure control can be easily implemented.

Now let's talk about the specific deployment steps:

Because I prefer Ubuntu server to do server operating system, so these operations are only suitable for Ubuntu system. in Ubuntu we should install Ubuntu in the way of deployment operations , rather than compile and install, through the Ubuntu apt source installation deployment, not only in the overall style in line with the system style, but also to provide configuration and operation of great convenience.

First update the software source:

sudo apt-get update

Install Nginx:

sudo apt-get install nginx

Install UWSGI and UWSGI support for Python:

sudo apt-get install uwsgi uwsgi-plugin-python

Install Virtualenv:
This is a matter of personal choice, as many Python packages can also be installed via apt source.
My personal custom is that some Python tools are easy to install via source, such as supervisor, management and upgrades, while some third-party libraries in Python are installed through PIP in virtualenv, such as virtualenv, an infrastructure tool, or a source installation.

sudo apt-get install python-virtualenv

Create a Python environment:

mkdir -p /var/virenvs
cd /var/virenvs
virtualenv myenv

Install Flask:

cd myenv
. bin/activate
pip install flask

It is recommended that you use Pip's Freeze and Install-r requirement_file for environment deployment.

Assuming our project path is/var/virenvs/myenv/myproject, we can deploy the program through git or some other way.

To configure permissions for the project directory, Nginx and UWSGI are run by default with Www-data users and groups:

chown -R www-data:www-data /var/virenvs/myenv/myproject
chmod -R 775 /var/virenvs/myenv/myproject

For Nginx and Uwsgi we can deploy using vhost, which ensures that each project uses a separate virtualenv and can share a single uwsgi pool, which is easier to use, deploy, and manage.

vim /etc/nginx/site-available/myproject

server {Listen 80;server_name hostname;    location/static {alias/var/virenvs/myenv/myproject/static;}    Location/{include Uwsgi_params;uwsgi_pass 127.0.0.1:9001;uwsgi_param Uwsgi_pyhome/var/virenvs/myenv;uwsgi_param Uwsgi_chdir/var/virenvs/myenv/myproject;uwsgi_param uwsgi_module mywebsite;uwsgi_param UWSGI_CALLABLE app;}    Error_page 404/404.html;}

Where Mywebsite is the mywebsite.py file under the project directory, uwsgi_callable is the flask instance name.

To establish a soft connection to the enabled directory

ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/myproject

UWSGI Configuration

vim /etc/uwsgi/apps-available/myuwsgi.ini

[uwsgi]plugins=pythonvhost=truesocket=127.0.0.1:9001

Uwsgi also has many other configuration items that need to be well configured in the build environment.

To establish a soft connection to the enabled directory

ln -s /etc/uwsgi/apps-available/myuwsgi.ini /etc/uwsgi/apps-enabled/myuwsgi.ini

Restart Service

service nginx restart
service uwsgi restart

Deployment complete

Nginx has many configuration methods, and his loading process is as follows:

Start the Nginx service, he will load nginx.conf, this file will load the configuration in the/etc/nginx/site-enabled, this configuration tells the relevant parameters to Uwsgi,uwsgi to manage the app

The result is returned to the Nginx server, which is displayed in HTML form.

If there is a upstream error in Nginx, it is nginx and uwsgi communication problems, to see if there is a problem with the configuration

If there is a python. Egg permission problem, you need to set the Python-egg-cache path: http://blog.csdn.net/watsy/article/details/9665339, it is important to note that Cache storage Location Permissions issues for third-party packages

Debugging

App debugging is mainly to look at the log file, here are a few log files need to note:

Nginx:access.log,error.log, the main record is the log of Web requests and related errors

Uwsgi:myuwsgi.log, the main record is the application internal error

Nginx+uwsgi+flask Building Python-web Applications

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.