您好,登錄后才能下訂單哦!
一、安裝Apache
# yum install -y httpd httpd-devel
# systemctl start httpd.service # 啟動 # systemctl stop httpd.service # 關閉 # systemctl restart httpd.service # 重啟 # systemctl enable httpd.service # 開機自啟
防火墻開放80端口
# firewall-cmd --zone=public --add-port=80/tcp --permanent # firewall-cmd --reload
開啟Apache,外網已經可以通過ip訪問Apache的默認頁面了
二、安裝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
三、創建項目
創建flask項目(最簡單的,一個項目文件夾、一個啟動文件)
# mkdir /var/www/flask_test # 項目文件夾 # vi /var/www/flask_test/app.py # 啟動文件
啟動文件例子:
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()
在項目文件夾下創建虛擬環境,安裝flask
# cd /var/www/flask_test # virtualenv py3env # 創建虛擬環境 # source py3env/bin/activate # 進入虛擬環境 (py3env) # pip install flask # 安裝flask (py3env) # deactivate # 退出虛擬環境
四、在虛擬環境中用pip安裝mod_wsgi
# yum install -y python36-devel.x86_64 # 一個依賴,不安裝的話,下面pip會報錯。。 # source py3env/bin/activate # 進入虛擬環境 (py3env) # pip install mod_wsgi # 安裝mod_wsgi (py3env) # mod_wsgi-express install-module # 執行該命令,把輸出內容復制下來 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 # 退出虛擬環境
修改Apache的配置
# vi /etc/httpd/conf/httpd.conf
復制上面得到的這行內容,添加到配置文件的最后
五、配置mod_wsgi
# vi /var/www/html/flask_test/app.wsgi
寫入以下內容(根據: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
寫入以下內容(根據: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了,啟動Apache,通過這臺服務器的ip就能訪問網頁了
測試一些app.py里面寫的兩個路徑
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。