Linux use Nginx + UWSGI to run flask program

Source: Internet
Author: User

If you google yourself, you may find a keyword Uwsgi, he is the protagonist of this article.

noun explanation

Web Server Gateway Interface (Interface, abbreviated as WSGI.
Uwsgi, like Wsgi, is a communication protocol.
Uwsgi, a server that implements Wsgi,uwsgi,http protocols, and nginx at one level.

Given that some of the domestic tutorials are written in Xiang, the old job hurriedly record share.

PS. This tutorial is not limited to flask, web.py, Django is also applicable.
I. Installation Environment

Centos x64
Python 2.6

II. Installation of Uwsgi

Pip Install Uwsgi
Iii. Installation Configuration Virtualenv

Generally we will use VIRTUALENV to configure the project's operating environment

Pip Install Virtualenv

# Suppose ~/app_dir for your project directory
mkdir ~/app_dir && CD ~/app_dir

# The simplest example of flask
VI hello_world.py

# add

From flask import Flask
App = Flask (__name__)

@app. Route ("/")
def hello ():
Return "Hello world!"

if __name__ = = "__main__":
App.run (' 0.0.0.0 ', 8080)

--------: Wq

Virtualenv venv

# Activate Virtualenv
. Venv/bin/activate

#pip Install flask QRCode image
Pip Install flask

# Exit Virtualenv
Deactivate
Iv. Test Run

The project and the operating environment are ready, let's start with Uwsgi now try to run the project.

# Activate Virtualenv
. Venv/bin/activate
Python hello_world.py

# now to visit your ip:8080 try
V. Nginx configuration

Here we use the HTTP reverse proxy

VI hello_world.conf

server{
Listen 80;
Location/{
Proxy_pass http://127.0.0.1:9090;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_set_header Referer http://$host;
}
}
Vi. running Uwsgi Server

Uwsgi--http-socket 127.0.0.1:9090--wsgi-file/root/app_dir/hello_world.py--callable app-h
Or
Uwsgi--http:9090--wsgi-file/root/app_dir/hello_world.py--callable app-h/root/app_dir/venv
The above command we run the UWSGI server using HTTP, bind port 9090, specify the entry file hello_world.py with an absolute path, and specify the VIRTUALENV address using the-H parameter.

Of course, there are many ways to run the UWSGI server, such as we can start in the form of configuration files, and then we can use the form of sock, of course, the corresponding Nginx response agent also need to be modified.

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.