centos+python2+apache2+flask环境搭建
环境:
1.centos7
2.python2
3.flask1.1.2
4.apache2
1.安装python虚拟环境
pip install virtualenv
2.创建虚拟环境
mkdir flask_project cd flask_project virtualenv venv
3.在虚拟环境里安装flask
source source venv/bin/activate pip install flask
4.创建flask项目
vim app.py
from flask import Flask app = Flask(__name__) @app.route('/') def test1(): return 'I am a flask example!' @app.route('/example') def test2(): return 'Have a nice day!' if __name__ == '__main__': app.jinja_env.auto_reload = True app.config['TEMPLATES_AUTO_RELOAD'] = True app.run()
vim wsgi.py
activate_this = '/var/www/html/flask_project/venv/bin/activate_this.py' with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this)) import sys sys.path.insert(0, '/var/www/html/flask_project') from app import app as application
5.安装apache
yum install httpd httpd-devel -y
6.安装apache所需的mod_wsgi模块
yum -y install mod_wsgi #或者pip install mod_wsgi
7.查看apache2是否导入mod_wsgi模块
grep -r 'mod_wsgi.so' /etc/httpd/
8.如果第7步导入,跳过这一步,否则,手动导入mod_wsgi模块
#在/etc/httpd/conf末尾添加 LoadModule wsgi_module modules/mod_wsgi.so
9.新建一个apache的虚拟主机
我的项目详情:
vi /etc/httpd/conf.d/flask.conf
<Directory /var/www/html/flask_project> <Files wsgi.py> Require all granted </Files> </Directory> WSGIPythonHome /var/www/html/flask_project/venv WSGIPythonPath /var/www/html/flask_project/venv/lib/python2.7/site-packages Listen 8080 <VirtualHost *:8080> ServerName flask.example.com WSGIScriptAlias / /var/www/html/flask_project/wsgi.py </VirtualHost>
重启apache
systemctl restart httpd
访问站点测试
curl 127.0.0.1:8080