Deploy Nginx + uWSGI + Flask in Ubuntu 12.04

Source: Internet
Author: User
Tags virtualenv

Flask itself can directly start the HTTP server, but due to problems such as management, deployment, and performance, we generally do not use the HTTP server of Flask itself in the production environment.

From the existing practices, for Flask, a better deployment method is to use uWSGI as the WSGI container and Nginx as the front-end server. The benefits of doing so are:

1. uWSGI provides good performance, many functions, and convenient O & M.
2. Nginx provides better processing of static files and supports the uWSGI protocol by default, which can be easily implemented in load balancing and pressure control.

Now let's take a look at the specific deployment steps:

Because I prefer Ubuntu Server as a Server operating system, these operations are only applicable to Ubuntu systems. In Ubuntu, we should install Ubuntu for deployment and O & M, instead of compiling and installation. The installation and deployment through the ubuntu apt source not only conforms to the system style in the overall style, it also provides great convenience for configuration and O & M.

First, update the software source:

Sudo apt-get update

Install Nginx:

Sudo apt-get install nginx

Install uWSGI and uWSGI for Python:

Sudo apt-get install uwsgi-plugin-python

Install virtualenv:
Here is a matter of choice, because many python packages can also be installed through the apt source.
My personal habit is that some python tools are easy to install through the source, such as the supervisor, management and upgrade, while some python third-party libraries are still installed through pip in virtualenv, basic tools such as virtualenv are installed through the source.

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

We recommend that you use freeze and install-r requirement_file of pip for Environment deployment.

Assume that our project path is/var/virenvs/myenv/myproject. In this case, we can deploy the program using git or other methods.

Configure permissions for the project directory. nginx and uwsgi are run by www-data users and groups by default:

Chown-R www-data: www-data/var/virenvs/myenv/myproject
Chmod-R 775/var/virenvs/myenv/myproject

For nginx and uwsgi, we can use vhost for deployment. This ensures that each project uses an independent virtualenv and shares a uWSGI pool, this makes it easy 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;
}
Mywebsite is the mywebsite. py file under the project directory, and UWSGI_CALLABLE is the Flask Instance name.

Create 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 = python
Vhost = true
Socket = 127.0.0.1: 9001
Uwsgi has many other configuration items, which must be well configured in the generation environment.

Create 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 completed

Recommended reading:

You should use Nginx + uWSGI

UWSGI + Nginx deploy Flask Web Applications

Deployment of Django + Nginx + uWSGI

Deploy Python applications in Nginx + uWSGI in Linux

Install Nginx + uWSGI + Django on Ubuntu Server 12.04

CentOS 5.5 + Nginx 0.8.50 + uWSGI + Django 1.2.3 deploy the Django Project

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.