当前位置: 代码迷 >> 综合 >> Ubutun+Flask+mod_wsgi+apache2
  详细解决方案

Ubutun+Flask+mod_wsgi+apache2

热度:80   发布时间:2023-11-18 06:40:58.0
在Ubutun系统下阿帕奇配置Flask
希望对大家有提示
一:安装apache2 服务器,mod_wsgi,在虚拟环境中安装 flask,以下命令可以解决
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
sudo apt-get install python-virtualenv
二:配置站点
apache有个默认的站点存放目录:/var/www
现在我们要在 www 文件夹下新建一个名为test的测试站点(文件),现在站点存放目录的机构如下:
www 
├── html
│   └── index.html
├── test1. 然后我们向test目录中添加三个文件,一个是app.wsgi一个是hello.py,还有一个venv存放虚拟环境hello.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():return '<h1>Hello, world</h1>'
if __name__ == '__main__':app.run()app.wsgi:
import sys
activate_this = '/var/www/test/venv/bin/activate_this.py' # 虚拟环境的启动脚本
execfile(activate_this, dict(__file__=activate_this)) # 执行虚拟环境启动脚本
sys.path.insert(0, '/var/www/test')
from hello import app as application创建虚拟环境:virtualenv venv,venv为虚拟环境的名字,该虚拟环境的所有相关文件都保存在里面。
激活虚拟环境:source venv/bin/activate
在虚拟环境中安装flask:pip install flask
在虚拟环境中打开python命令行,输入import flask,如果没有任何消息(Linux主张没有消息就是最好的消息),说明安装成功。
按Ctrl+D可以退出python命令行
然后输入deactivate可以退出虚拟环境(安装pip包,都在虚拟环境中)2.下面来配置apache2,打开apache2的站点配置目录:cd /etc/apache2/sites-avalible,
创建一个名为test.conf的站点配置文件:sudo gedit test.conf,
在test.conf中输入以下内容:<virtualhost *:80>ServerName www.test.comServerAlias *.test.comWSGIDaemonProcess test threads=5 WSGIScriptAlias / /var/www/test/app.wsgi<directory /var/www/test>WSGIProcessGroup testWSGIApplicationGroup %{GLOBAL}WSGIScriptReloading OnOrder deny,allowAllow from all</directory>
</virtualhost>3.然后修改系统的hosts文件:sudo gedit /etc/hosts 
添加如下内容: 127.0.0.1 www.test.com4.开启阿帕奇 sudo service apache2 start,如果访问www.test.com成功,配置成功5.如果你的网站要跑到公网上,忽视第三步,在/etc/apache2/sites-enabled添加之前test.conf,错误日志在/var/log/apache2/error.log,出错可以详细参看6.最后补上apache2详细配置说明http://www.cnblogs.com/ylan2009/archive/2012/02/25/2368028.html
  相关解决方案