通过Gevent+Apache(反向代理)部署Flask应用
1. 安装gevent
Windows下的话有exe包的,先装greenlet,再装gevent
Debian下先安装libevent
cd /tmp wget https://github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz tar zxvf libevent-2.0.20-stable.tar.gz cd libevent-2.0.20-stable ./configure --prefix=/usr/local/libevent --enable-shared make make check make install
再安装gevent
pip install gevent
2. 安装Apache
详细步骤请参考。
3. 修改Apache配置文件,重启Apache
示例文件:/etc/apache2/sites-available/example.net
<VirtualHost *:80> ServerAdmin admin@example.org ServerName example.org ServerAlias www.example.org DocumentRoot /srv/www/example.org/public_html/ ErrorLog /srv/www/example.org/logs/error.log CustomLog /srv/www/example.org/logs/access.log combined ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://127.0.0.1:9000/ ProxyPassReverse / http://127.0.0.1:9000/ </VirtualHost>
重启Apache
/etc/init.d/apache2 reload
/etc/init.d/apache2 restart
4. 修改Flask项目下的执行文件(run.py)
@manager.command def rungevent(port=9000, name=''): from gevent.wsgi import WSGIServer """ from werkzeug.debug import DebuggedApplication app.wsgi_app=DebuggedApplication(app.wsgi_app,True) app.debug = True """ server = WSGIServer((name, port), app) server.serve_forever()
5. 在virtualenv的环境下后台加载Flask项目
nohup python run.py rungevent
6. 停止Flask网站时的操作
执行命令列出所有Linux进程。
ps -aux
找到nohup python进程并记住进程pid。
执行命令
kill [进程pid]
立即中止。