一、安裝Apache
# yum install -y httpd httpd-devel
# systemctl start httpd.service # 啟動(dòng)
# systemctl stop httpd.service # 關(guān)閉
# systemctl restart httpd.service # 重啟
# systemctl enable httpd.service # 開(kāi)機(jī)自啟
防火墻開(kāi)放80端口
# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --reload
開(kāi)啟Apache,外網(wǎng)已經(jīng)可以通過(guò)ip訪問(wèn)Apache的默認(rèn)頁(yè)面了
二、安裝Python36、pip3、virtualenv
# yum install -y epel-release
# yum install -y python36
# python36 -V
Python 3.6.6
# yum install -y python36-setuptools
# easy_install-3.6 pip
# pip3 -V
pip 18.1 from /usr/local/lib/python3.6/site-packages/pip-18.1-py3.6.egg/pip (python 3.6)
# pip3 install virtualenv
三、創(chuàng)建項(xiàng)目
創(chuàng)建flask項(xiàng)目(最簡(jiǎn)單的,一個(gè)項(xiàng)目文件夾、一個(gè)啟動(dòng)文件)
# mkdir /var/www/flask_test # 項(xiàng)目文件夾
# vi /var/www/flask_test/app.py # 啟動(dòng)文件
啟動(dòng)文件例子:
from flask import Flask, request
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World'
@app.route('/hello')
def hello():
name = request.args.get('name','')
return 'Hello ' + name + '!'
if __name__ == '__main__':
app.run()
在項(xiàng)目文件夾下創(chuàng)建虛擬環(huán)境,安裝flask
# cd /var/www/flask_test
# virtualenv py3env # 創(chuàng)建虛擬環(huán)境
# source py3env/bin/activate # 進(jìn)入虛擬環(huán)境
(py3env) # pip install flask # 安裝flask
(py3env) # deactivate # 退出虛擬環(huán)境
四、在虛擬環(huán)境中用pip安裝mod_wsgi
# yum install -y python36-devel.x86_64 # 一個(gè)依賴(lài),不安裝的話,下面pip會(huì)報(bào)錯(cuò)。。
# source py3env/bin/activate # 進(jìn)入虛擬環(huán)境
(py3env) # pip install mod_wsgi # 安裝mod_wsgi
(py3env) # mod_wsgi-express install-module # 執(zhí)行該命令,把輸出內(nèi)容復(fù)制下來(lái)
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/flask_test/py3env"
(py3env) # deactivate # 退出虛擬環(huán)境
修改Apache的配置
# vi /etc/httpd/conf/httpd.conf
復(fù)制上面得到的這行內(nèi)容,添加到配置文件的最后
復(fù)制代碼 代碼如下:
LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
五、配置mod_wsgi
# vi /var/www/html/flask_test/app.wsgi
寫(xiě)入以下內(nèi)容(根據(jù):https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html)
activate_this = '/var/www/flask_test/py3env/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/flask_test')
from app import app as application
配置Apache
# vi /etc/httpd/conf/httpd.conf
寫(xiě)入以下內(nèi)容(根據(jù):https://dormousehole.readthedocs.io/en/latest/deploying/mod_wsgi.html#id1)
<VirtualHost *:80>
ServerName example.com
WSGIScriptAlias / /var/www/flask_test/app.wsgi
<Directory /var/www/flask_test>
Require all granted
</Directory>
</VirtualHost>
OK了,啟動(dòng)Apache,通過(guò)這臺(tái)服務(wù)器的ip就能訪問(wèn)網(wǎng)頁(yè)了
測(cè)試一些app.py里面寫(xiě)的兩個(gè)路徑
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。