centos+python2+apache2+flask环境搭建

Posted NAVYSUMMER

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

  

  

以上是关于centos+python2+apache2+flask环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

centos7 Nginx1.14+php7+mysql5.7 以及 centos7 Apache2.4+PHP7+mysql 安装 Linux 配置 composer 以及Python2.7升级到

CentOS7下 将django工程部署到Apache2.4上

Windows10下Apache2.4配置Django

安装Apache2.4 操作系统:Centos7.4

centos5.5 Apache2 Web 服务器的安装

centos7 apache2.4 测试