flask + uwsgi 生产环境

时间:2022-08-28 05:33:00

https://www.digitalocean.com/community/tutorials/how-to-deploy-flask-web-applications-using-uwsgi-behind-nginx-on-centos-6-4

http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/

https://www.linode.com/docs/websites/nginx/wsgi-using-uwsgi-and-nginx-on-centos-5

https://zafiel.wingall.com/archives/7513

yum -y install nginx
rm -rf /etc/nginx/conf.d/default.conf yum -y install python-devel python-pip python-virtualenv gcc
mkdir -p /opt/demo/app

virtualenv /opt/demo/venv
source /opt/demo/venv/bin/activate
pip install flask -i https://pypi.douban.com/simple
pip install uwsgi -i https://pypi.douban.com/simple pip freeze
Flask==0.11.1
Jinja2==2.8
MarkupSafe==0.23
Werkzeug==0.11.11
click==6.6
itsdangerous==0.24
uWSGI==2.0.14
cat > /opt/demo/app/__init__.py << EOF
#!/usr/bin/python
# -*- coding: utf-8 -*- from flask import Flask
app = Flask(__name__) @app.route("/")
def hello():
return "Hello World!" if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
EOF
cat > /opt/demo/run.py << EOF
#!/usr/bin/python
# -*- coding: utf-8 -*- from app import app if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000)
EOF
python /opt/demo/run.py
* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
deactivate
cat > /etc/nginx/conf.d/demo.conf << EOF
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 100M; location / {
try_files \$uri @demo;
} location @demo {
include uwsgi_params;
uwsgi_pass unix:///tmp/demo_uwsgi.sock;
}
}
EOF
/etc/init.d/nginx start && chkconfig nginx on
cat > /opt/demo/demo_uwsgi.ini << EOF
[uwsgi]
base = /opt/demo app = run
module = %(app)
callable = app virtualenv = %(base)/venv
pythonpath = %(base) socket = /tmp/%n.sock
chmod-socket = 666 vacuum = true
daemonize = %(base)/%n.log master = true
worker = 1
threads = 2
max-requests = 1000
EOF
cat > /etc/init.d/demo_uwsgi << EOF
#!/bin/bash
#
# bye-reader start script
#
# chkconfig: 345 99 1
# description: demo_uwsgi start script
# processname: demo_uwsgi
# pidfile: /var/run/demo_uwsgi.pid
# # Source function library.
. /etc/init.d/functions PROGNAME="demo_uwsgi"
PID="/var/run/\$PROGNAME.pid"
LOCK="/var/run/\$PROGNAME.lock"
RETVAL=0
PROG="/opt/demo/venv/bin/uwsgi" [ -f \$PROG ] || exit 0 start() {
echo -n "Starting \$PROGNAME: "
daemon \$PROG --ini /opt/demo/\$PROGNAME.ini --uid nginx --gid nginx --pidfile \$PID
echo
[ \$RETVAL = 0 ] && touch \$LOCK
return \$RETVAL
} stop() {
echo -n "Stopping \$PROGNAME: "
killproc -p \$PID \$PROGNAME -INT
RETVAL=\$?
echo
[ \$RETVAL = 0 ] && rm -f \$LOCK \$PID
} show_status() {
status \$PROGNAME
} case "\$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
show_status
;;
*)
echo "Usage: \$PROGNAME {start|stop|restart|status}"
RETVAL=2
;;
esac
exit \$RETVAL
EOF
chmod 755 /etc/init.d/demo_uwsgi
chkconfig --add demo_uwsgi
chkconfig demo_uwsgi on
/etc/init.d/demo_uwsgi start
Starting demoapp_uwsgi: [uWSGI] getting INI configuration from /opt/demoapp/demoapp_uwsgi.ini
[ OK ]
ps aux|grep uwsgi
nginx 1856 1.9 1.5 219276 16084 ? S 15:12 0:00 /opt/demo/venv/bin/uwsgi --ini /opt/demoapp/demo_uwsgi.ini --uid nginx --gid nginx --pidfile /var/run/demo_uwsgi.pid
nginx 1861 0.0 1.2 295056 12820 ? Sl 15:12 0:00 /opt/demo/venv/bin/uwsgi --ini /opt/demoapp/demo_uwsgi.ini --uid nginx --gid nginx --pidfile /var/run/demo_uwsgi.pid
root 1864 0.0 0.0 103312 908 pts/0 S+ 15:12 0:00 grep uwsgi