Flask+uwsgi+nginx

Source: Internet
Author: User
Tags nginx server

Environment

Centos

noun explanation

Flask:flask is a python implementation of the Web Development Micro-service framework, related information, http://docs.jinkan.org/docs/flask/

UWSGI:UWSGI is a Web server that implements the WSGI protocol, UWSGI, HTTP protocol, etc.

Deployment explanation

Individual flask can also start and provide Web services, but flask is only a web framework, its Web services can only be used for development environments, not for production environments, and therefore also needs to be integrated with UWSGI and Nginx

Uwsgi itself is a Web service, and the reason for introducing Nginx again is because Nginx is a reverse proxy. In this way, some pictures, JS and other static resources can be used nginx to provide services, while the other forwarding to Uwsgi, which is the ultimate goal of our deployment framework

Consolidation steps

1. Global Installation Flask

>sudo pip Install flask

2. Realize a minimum application hello.py

From flask import flask
app = Flask (__name__)

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

if __name__ = = ' __main__ ':
    app.run (host= ' 0.0.0.0 ') # host= ' 0.0.0.0 ' marks externally accessible


3. Installation Nginx

>sudo Yum Install Nginx

4, modify the Nginx configuration

If you are downloading the installed Nginx yourself, the default installation directory may be/usr/local/nginx

However, if the installation is Yum, the default installation directory is/etc/nginx/, and the default installed Nginx configuration file has the following configuration

include/etc/nginx/conf.d/*.conf;

In this way, we only need to add a hello.conf to the/etc/nginx/conf.d/directory

Upstream Hello-test {
    server unix:///root/works/flask/hello.sock;
}

server {
    listen      8088;
    server_name  hello.yunzong  localhost;
    CharSet    Utf-8;

    Client_max_body_size 100M;

    Location/{
        uwsgi_pass  hello-test;
        Add_header ' Access-control-allow-origin ';
        Add_header ' access-control-allow-credentials ' true;
        Add_header ' Access-control-allow-methods ' Get, POST, OPTIONS ';
        Add_header ' Access-control-allow-headers ' Dnt,x-customheader,keep-alive,user-agent,x-requested-with, If-modified-since,cache-control,content-type ';

    }
}


>systemctl Restart Nginx

Note: If start failure prompt: Nginx: [Emerg] bind () to 0.0.0.0:8088 failed (13:permission denied)

Then maybe the system is selinux, we need to turn off the SELinux, the following is a temporary shutdown command, if you want to permanently shut down please separate Google

>setenforce 0

Then we have started the nginx, and then we visit the http://10.211.55.3:8088/, where IP is nginx server IP, will be prompted: 502 Bad Gateway, here is the reason is in the above configuration, we configured the "server Unix:///root/works/flask/hello.sock; ", but we have not deployed UWSGI, and have not provided the socket service

5. Installation Uwsgi

>pip Install Uwsgi

6, create a Hello.ini type of file, of course, the other can also, specifically please Google

[Uwsgi]
Pythonpath =/root/tensorflow   # can be deleted
chdir           =/root/works/flask/
wsgi-file  =/root/works/flask/ hello.py
#socket file ' s location
socket =/root/works/flask/hello.sock

#permissions for the socket file
chmod-socket    = 666

#the variable that holds a flask application inside the module imported at line #6
callable = App

#location log files
logto =/root/works/flask/hello.log


Note: The/root/flask directory has our previous hello.py file

7, Start Uwsgi

>uwsgi--ini Hello.ini &

Note: Note the various files and paths involved in the command when starting Uwsgi


Conclusion:

There are a lot of problems with the whole process.

1, nginx configuration of the/root/works/flask/hello.sock access hint (13:permission denied), find information that is closed SELinux, their own testing can not, so the configuration changed to the 0.0.0.0 : 1500 IP and port mode, while modifying the Uwsgi Hello.ini configuration

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.