Flask Web App
First, Introduction
Recently developed to use a test environment, is such a structure of nginx+uwsgi+flask. Here are some records for deploying a simple Python application on a centos system using the flask architecture. Then use Nginx as the front-end reverse proxy to set up the UWSGI application gateway to process the Web application.
Second, conditions
1) Environmental Requirements
- Server OS: Minimal installation of Centos 6.8
- Nginx: Reverse Proxy
- python2.6~2.7: Developing language
- Flask/uwsgi: Frame
- Pip:python Package Management Tools
- Iptables&selinux: Open the port to use, turn off SELinux.
2) Install the necessary development tools and environment
Update system:
[email protected] ~]# yum update-y[[email protected] ~]# yum Groupinstall "Development tools"-Y
After the update is complete, remember to restart the system, and then install the tool:
[[email protected] ~]# yum install wget vim telnet python-devel zlib-devel openssl-devel bzip-devel-y[[email protected] ~ ]# Yum Install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel python- Devel libxml2 libxml2-devel python-setuptools zlib-devel wget openssl-devel pcre pcre-devel sudo gcc make Autoconf automake-y
3) Install Python package management tool-PIP
[Email protected] ~]# wget https://bootstrap.pypa.io/get-pip.py && python get-pip.py
or refer to this website
4) Install Uwsgi and flask
Download software
[[email protected] ~]# mkdir soft[[email protected] ~]# CD Soft[[email protected] soft]# wget https://pypi.python.org/pack Ages/0c/1c/44849e293e367a157f1ad863cee02b4b865840543254d8fae3ecdebdbdb9/uwsgi-2.0.12.tar.gz[[email protected] soft]# wget https://pypi.python.org/packages/db/9c/149ba60c47d107f85fe52564133348458f093dd5e6b57a5b60ab9ac517bb/ Flask-0.10.1.tar.gz
If the software is slow to download, you can add a 8.8.8.8 in/etc/reslov.conf DNS, soon.
Install the software (development requires a fixed version, if you do not mind the version, you can directly use the PIP install Packge to install, equivalent to the Yum in Linux)
[Email protected] soft]# tar-xf flask-0.10.1.tar.gz[[email protected] ~]# CD Flask-0.10.1[[email protected] Flask-0.10. 1]# python setup.py install[[email protected] soft]# tar-xf uwsgi-2.0.12.tar.gz[[email protected] soft]# CD uwsgi-2.0.12[ [email protected] uwsgi-2.0.12]# python setup.py install
5) Create a test app
Create a folder where the Web application resides
[Email protected] ~]# mkdir-pv/usr/local/webtest[[email protected] ~]# cd/usr/local/webtest
Create a Python script that reads as follows:
[email protected] webtest]# cat myproject.pyfrom flask Import flaskapplication = Flask (__name__) @application. Route ("/" ) def hello (): return "
Create the WSGI entry point file, named: wsgi.py, with the following
[email protected] webtest]# cat wsgi.pyfrom myproject import applicationif __name__ = = "__main__": Application.Run ( )
6) Configure the UWSGI Server Gateway service to run, run ports, and so on.
When the two Python script files are created, the Uwsgi command starts, runs on port 8000, specifies protocol HTTP, and the runtime has some warnings that can be ignored temporarily because the root account is currently running UWSGI, Normal user nginx can be configured after the boot from the configuration file.
[[email protected] webtest]# uwsgi--socket 0.0.0.0:8000--protocol=http-w wsgi Verification, look at the listening port [[email protected] webtest]# NE TSTAT-AULTNP | grep 8000tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 7181/uwsgi tcp 0 0 192.168.30.58:8000 192.168.30.65:48164 time_wait -
You can open the browser access test: http://192.168.30.58:8000
7) Create UWSGI configuration file
The. ini format for the UWSGI boot configuration file (and Xml,json, etc.), so that you do not have to enter a large number of commands at the command line, start with the Uwsgi command, you can create a Uwsgi startup script, add to the system services
In the project's directory, create the Uwsgi.ini:
[email protected] webtest]# cat Uwsgi.ini [uwsgi]uid = Nginxgid = Nginxsocket = 127.0.0.1:8000 master = truevhost = t Rueworkers = 8reload-mercy = 10vacuum = truemax-requests = 10000limit-as = 1024buffer-sizi = 3000pidfile =/var/run/uwsgi. Piddaemonize =/var/log/uwsgi/uwsgi.log chdir =/usr/local/webtestmodule = Wsgichmod-socket = 660enable-threads = True
Save and exit. Configuration file Reference: http://heipark.iteye.com/blog/1847421
Under the general explanation:
UID, GID: which user, group to start the service
Socket: IP and port for listening
Master: Start the main process
Workes: Number of processes initiated by the server
Reload-mercy: Smooth Restart
Pidfile: The PID file at startup.
Daemonize: Start Log
Module:web the entry module name of the application
Enable-threads: Enable threading Mode
8) Create the boot system startup script file with the name Uwsgid.
UwsgidAfter the creation is complete, give execute permission, join the boot start
[Email protected] ~]# chmod +x/etc/init.d/uwsgid [[email protected] ~]# chkconfig--add uwsgid
[Email protected] ~]# chkconfig--level 2345 Uwsgid on
Check the
[Email protected] ~]# chkconfig--list uwsgiduwsgid 0:off 1:off 2:on 3:on 4:on 5:on 6 : Off
Start Uwsgid
[[Email protected] ~]# service Uwsgid startstarting uwsgi: [Uwsgi] getting INI configuration From/usr/local/webtest/uwsgi . ini
Verify the Listening port (note: Here we use 127.0.0.1 for monitoring, is for later use, only Nginx can access the Web application):
[Email protected] ~]# NETSTAT-AULTNP | Grep:8000tcp 0 0 127.0.0.1:8000 0.0.0.0:* LISTEN 8945/uwsgi
9) Install Nginx configuration agent
Download the Epel source for installation 6.6 first
[Email protected] ~]# wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm[[email Protected] ~]# RPM-IVH epel-release-6-8.noarch.rpm
Installing Nginx
[email protected] ~]# Yum install nginx-y
After the installation is complete, modify the Nginx default profile default.conf, you can back up the next source file before modifying it oh. The complete contents are as follows:
[Email protected] ~]# Cat/etc/nginx/conf.d/default.confserver { listen ; server_name 192.168.30.58; Location/{ include uwsgi_params; Uwsgi_pass 127.0.0.1:8000; Uwsgi_param Uwsgi_script Wsgi; Uwsgi_param uwsgi_chdir/usr/local/webtest; Index index.html index.htm; Client_max_body_size 35m;
The above content proxies the UWSGI port, the client accesses the Nginx 80 port, then the Nginx forwards the request to the 8000 port of the background flask application.
Start Nginx
[[Email protected] ~]# service Nginx startstarting nginx: [ OK ]
Test access in the browser nginx:http://192.168.30.58
Flask Web App