Flask+ gunicorn+ upstart+ nginx
Posted Python学习助手
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flask+ gunicorn+ upstart+ nginx相关的知识,希望对你有一定的参考价值。
环境:ubuntu-14.04.3(预装python3.4)
1.flask(Web框架)
2.gunicorn(python Wsgi http server)
1.安装
1 sudo pip3 install gunicorn
2.修改入口文件
1 from werkzeug.contrib.fixers import ProxyFix
2 app.wsgi_app = ProxyFix(app.wsgi_app)
3 app.run()
3.启动
1 gunicorn -w 4 -b 127.0.0.1:8000 入口文件名:应用实例
3.upstart(服务进程管理)
1 myapp.conf (开机启动)
2 '''
3 description "flask app"
4 start on runlevel [2345]
5 stop on runlevel [!2345]
6 respawn
7 setuid root
8 setgid www-data
9 chdir /home/zhangxin/PycharmProjects/flaskApp # 指令被执行的目录
10 exec gunicorn -b 0.0.0.0:8000 app:app
11 '''
12 1.目录: cd /etc/init
13 2.启动:sudo start myapp
14 3.状态:sudo status myapp
15 4.停止:sudo stop myapp
16 5.重启:sudo restart myapp
4.nginx(代理服务)
1.安装
1 sudo apt-get install nginx
2 /etc/nginx/sites-available
3 sudo nano default
4 '''
5 server {
6 listen 8899;
7 server_name 127.0.0.1;
8 location / {
9 proxy_pass http://127.0.0.1:8000;
10 proxy_set_header Host $host;
11 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
12 }
13 }
14 '''
2.重启
1 sudo service nginx restart
5.修改pip镜像地址:
1 /home/<username>/.pip/pip.conf
2 [global]
3 index-url = http://mirrors.aliyun.com/pypi/simple/
6.安装 pip3
1 sudo apt-get install python3-pip
7.pipreqs
1.安装
1 sudo pip3 install pipreqs
2.生成
1 pip3 freeze > requirements.txt
3.执行
1 sudo pip3 install -r requirements.txt
以上是关于Flask+ gunicorn+ upstart+ nginx的主要内容,如果未能解决你的问题,请参考以下文章