install
pip install gunicorn gevent flask
flask application
# -*- coding: utf-8 -*-
# run.py
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
@app.route('/')
def hello_world():
return 'Hello World!'
Command line start
gunicorn -w 2 -b 127.0.0.1:8000 run:app
The configuration file
# -*- coding: utf-8 -*-
# config.py
from __future__ import print_function, absolute_import, unicode_literals
import multiprocessing
import os
import gevent.monkey
gevent.monkey.patch_all()
if not os.path.exists('log'):
os.mkdir('log')
# debug = True
loglevel = 'debug'
bind = "0.0.0.0:5000"
pidfile = "log/gunicorn.pid"
accesslog = "log/access.log"
errorlog = "log/debug.log"
daemon = True
# Number of processes started
workers = multiprocessing.cpu_count() * 2 + 1
worker_class = 'gevent'
x_forwarded_for_header = 'X-FORWARDED-FOR'
Specify profile startup
gunicorn -c config.py run:app
close
# see PID Number
ps -ef | grep gunicorn
# perhaps
cat log/gunicorn.pid
# stop it
kill -9 PID Number
# restart
kill -HUP PID Number
Integrated into command line
#!/bin/bash
# service.sh
# Add start command
function start(){
echo "start..."
# This is changed to project path
gunicorn -c config.py app:app
echo "start successful"
return 0
}
# Add stop command
function stop(){
echo "stop..."
kill -9 `cat log/gunicorn.pid`
echo "stop successful"
return 0
}
# restart
function restart(){
echo "restart..."
kill -HUP `cat log/gunicorn.pid`
echo "restart successful"
return 0
}
case $1 in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
*)
echo " Please enter : start, stop, restart"
;;
esac
Nginx To configure
server {
listen 80;
server_name _;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
proxy_pass http://127.0.0.1:5000/;
proxy_redirect off;
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 X-Forwarded-Proto $scheme;
}
}
Python Web Tencent cloud deployment :flask+fabric+gunicorn+nginx+supervisor
gunicorn Deploy flask