红联Linux门户
Linux帮助

Apache+mod_wsgi+Flask部署文档--ubuntu

发布时间:2017-06-22 09:44:19来源:linux网站作者:saminsysu
本文档适用于:
1.python3.5
2.apache2.4
3.mod_wsgi4.3
 
部署过程:
1.安装Apache
$ sudo apt-get install apache2
启动apache服务
$ sudo /etc/init.d/apache2 start
2.安装mod_wsgi
$ sudo apt-get install libapache2-mod-wsgi-py3
如果项目开发使用的是python2,则不需要加-py3
启动mod_wsgi
$ sudo a2enmod wsgi
3.创建.wsgi文件,该文件用于启动项目,如下
import os
import sys
import re
//进入虚拟环境,相当于souce .venv/bin/active
activate_this = '/home/sam/Snipe/.venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
//这里需要导入环境变量
from app import app as application
4.创建apache配置文件,配置虚拟主机,将配置文件添加到/etc/apache2/sites-available/,如下
<VirtualHost 172.18.215.158:5000>
ServerAdmin xiemsinsysu@foxmail.com
ServerName sysu.snipe
DocumentRoot /home/sam/Snipe/src/web/
#将/请求转发给/home/sam/Snipe/src/web/Snipe.wsgi
WSGIScriptAlias / /home/sam/Snipe/src/web/Snipe.wsgi
#配置本项目的守护进程
WSGIDaemonProcess Snipe user=sam group=sam threads=10 home=/home/sam/Snipe/src/web/ 
<Directory /home/sam/Snipe/src/web/>
WSGIProcessGroup Snipe
Require all granted
</Directory>
ErrorLog /var/log/apache2/error_snipe.log
</VirtualHost>
使用apachectl configtest命令可以查看配置文件是否正确
5.配置监听端口,添加Listen 172.18.215.158:5000到配置文件/etc/apache2/ports.conf,使apache监听172.18.215.158:5000
6.激活虚拟主机
$ sudo a2ensite /etc/apache2/sites-available/Snipe.conf
7.重新加载apache
$ sudo /etc/init.d/apache2 reload
打开浏览器,输入172.18.215.158:5000,即可看到项目web端~~~!!!
 
项目结构
+/home/sam/Snipe/
  +.venv
  +src/web/
   +app/
    +__init__.py
    +...
   +Snipe.wsgi
   +manage.py
   +.env
   +...
+...
 
参考资料
1.flask官方文档:http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/
2.mod_wsgi官方文档:http://modwsgi.readthedocs.io/en/latest/configuration.html
 
本文永久更新地址:http://www.linuxdiyf.com/linux/31657.html