系列文章目錄
RHCE第0章:RHCE開始前的準(zhǔn)備
RHCE第1章:Web服務(wù)器(上)
RHCE第1章:Web服務(wù)器(下)
RHCE第2章:DNS服務(wù)
RHCE第3章:DHCP服務(wù)器
RHCE第4章:Firewall服務(wù)、
RHCE第5章:SELinux
RHCE第6章:nfs網(wǎng)絡(luò)文件系統(tǒng)
RHCE第7章:samba文件共享
RHCE第8章:鏈路聚合和橋接
RHCE第9章:KVM虛擬化技術(shù)
RHCE第10章:時(shí)間服務(wù)器
RHCE第11章:Mariadb數(shù)據(jù)庫(kù)(上)
RHCE第11章:Mariadb數(shù)據(jù)庫(kù)(中)
RHCE第11章:Mariadb數(shù)據(jù)庫(kù)(下)
RHCE第11章:Mariadb數(shù)據(jù)庫(kù)(后)
RHCE第12章:FTP服務(wù)
RHCE第13章:ISCSI存儲(chǔ)網(wǎng)絡(luò)
RHCE第14章:郵件服務(wù)器
RHCE第15章:Kickstart
Centos7:http/PhP升級(jí)
Centos7:Flask-Apache部署
前言
之前寫過幾個(gè)Flask網(wǎng)頁(yè)來完成一些操作,一直使用的都是Screen+uwsgi所以在并發(fā)上邊支持的不太好(反正就我自己用),但最近完成的一個(gè)項(xiàng)目,對(duì)并發(fā)要求稍微高一點(diǎn),前幾天也在升級(jí)軟件,就順便把Flask的安裝部署也記錄一下。
一、安裝Python虛擬環(huán)境
1.編譯安裝
我使用的是Flask==2.1.2官方的最新穩(wěn)定版。官方建議Python環(huán)境使用3.7以上,所以我選擇了3.8.5版本編譯安裝。
官方下載地址
下載完成后上傳服務(wù)器,解壓-編譯-安裝
tar -xvf Python-3.8.5.tar.xz
cd Python-3.8.5
./configure --enable-shared
make
make install
安裝完成之后,會(huì)自動(dòng)添加到環(huán)境變量
2.虛擬環(huán)境創(chuàng)建
自己選擇一個(gè)合適的地方創(chuàng)建虛擬環(huán)境。
python3 -m venv flask
source flask/bin/activate
現(xiàn)在可以通過pip命令導(dǎo)入自己需要的包。
二、安裝mod_wsgi
官方教程中,可以使用yum命令安裝,但實(shí)際操作不行,所以還是選擇編譯安裝。
mod_wsgi 4.9.4下載
編譯的時(shí)候要指定之前安裝好的apache和python
tar -xvf mod_wsgi-4.9.4.tar.gz
cd mod_wsgi-4.9.4
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/opt/env/flask/bin/python3
make
make install
之后將安裝的模塊添加到apache中
三、編寫配置文件
1.flask文件
在自己的項(xiàng)目,目錄下創(chuàng)建wsgi.py文件
具體內(nèi)容如下
activate_this = '/opt/env/flask/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/web")
from app import app as application
這時(shí)固定寫發(fā)只有這里面只有兩個(gè)參數(shù)需要改變,一個(gè)是第一行的虛擬環(huán)境路徑,一個(gè)是第五行的自己的項(xiàng)目目錄路徑。
2.虛擬環(huán)境文件
第一個(gè)文件在虛擬環(huán)境中是不存在的,需要我們自己創(chuàng)建
具體內(nèi)容也是固定寫法
vim /opt/env/flask/bin/activate_this.py
"""By using execfile(this_file, dict(__file__=this_file)) you will
activate this virtualenv environment.
This can be used when you must use an existing Python interpreter, not
the virtualenv bin/python
"""
try:
__file__
except NameError:
raise AssertionError(
"You must run this like execfile('path/to/activate_this.py', dict(__file__='path/to/activate_this.py'))")
import sys
import os
old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.dirname(os.path.abspath(__file__)) + os.pathsep + old_os_path
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if sys.platform == 'win32':
site_packages = os.path.join(base, 'Lib', 'site-packages')
else:
site_packages = os.path.join(base, 'lib', 'python%s' % sys.version[:3], 'site-packages')
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = base
# Move the added items to the front of the path:
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
3.httpd文件
編寫apache的conf文件
最底下添加一行,意思是將這個(gè)目錄下的.conf文件都導(dǎo)入
編寫.conf文件(前綴隨意)
Listen 5000(可以自己設(shè)定端口)
<VirtualHost *:5000>
ServerName 你的域名
WSGIDaemonProcess web user=www group=www threads=5
WSGIScriptAlias / /var/www/web/wsgi.py(你的配置文件)
ErrorLog /var/www/web/error.log(日志目錄)
CustomLog /var/www/web/access.log combined
DocumentRoot /var/www/web(項(xiàng)目目錄)
<Directory /var/www/web>
WSGIProcessGroup web(項(xiàng)目名稱)
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
然后重啟apache服務(wù)器,瀏覽器訪問即可。
注意 一些常用的運(yùn)維知識(shí),比如服務(wù)器開放端口之類,請(qǐng)自行學(xué)習(xí),也可以看我的RHCE系列專欄。文章來源:http://www.zghlxwxcb.cn/news/detail-610214.html
總結(jié)
至此Flask-Apache的部署完成文章來源地址http://www.zghlxwxcb.cn/news/detail-610214.html
到了這里,關(guān)于Centos7:Flask-Apache部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!