溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS7部署Flask(Apache、mod_wsgi、Python36、venv)

發(fā)布時(shí)間:2020-10-15 13:15:41 來源:腳本之家 閱讀:192 作者:yuan_zw 欄目:服務(wù)器

一、安裝Apache

# yum install -y httpd httpd-devel
# systemctl start httpd.service    # 啟動
# systemctl stop httpd.service     # 關(guān)閉
# systemctl restart httpd.service   # 重啟
# systemctl enable httpd.service    # 開機(jī)自啟

防火墻開放80端口

# firewall-cmd --zone=public --add-port=80/tcp --permanent
# firewall-cmd --reload

開啟Apache,外網(wǎng)已經(jīng)可以通過ip訪問Apache的默認(rèn)頁面了

二、安裝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)目(最簡單的,一個(gè)項(xiàng)目文件夾、一個(gè)啟動文件)

# mkdir /var/www/flask_test    # 項(xiàng)目文件夾
# 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()

在項(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è)依賴,不安裝的話,下面pip會報(bào)錯。。
# 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ù)制下來
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

寫入以下內(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

寫入以下內(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了,啟動Apache,通過這臺服務(wù)器的ip就能訪問網(wǎng)頁了

測試一些app.py里面寫的兩個(gè)路徑

CentOS7部署Flask(Apache、mod_wsgi、Python36、venv)

CentOS7部署Flask(Apache、mod_wsgi、Python36、venv)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI