Apache 2.4 部署 Flask 應用
Windows環境
Windows 10 (x64)
安裝Apache 2.4
- 下載安裝包
- 鏈接 https://www.apachehaus.com/cgi-bin/download.plx
- 本文中使用的版本爲 2.4.41
- 注意,務必將服務器安裝在磁盤的頂級目錄內,如C:\Apache24
- 系統服務的安裝與卸載
- 以管理員身份打開命令行,進入安裝目錄下的bin目錄
- 目錄結構如下
D:\APP-SERVER\APACHE24 ├─bin │ └─iconv ├─cgi-bin ├─conf │ ├─extra │ ├─original │ │ └─extra │ └─ssl ├─error │ └─include ├─htdocs ├─icons │ └─small ├─include ├─lib ├─logs └─modules
- 將Apache安裝爲系統服務
.\httpd.exe -k install 自行制定服務名稱時,使用 -n 選項 .\httpd.exe -k install -n "Apache-2.4.41"
- 將系統服務中的Apache卸載
.\httpd.exe -k uninstall 如果你在安裝時指定了名字,卸載時也要用-n選項指定名字 .\httpd.exe -k uninstall -n "Apahce-2.4.41"
- 環境變量的設置 - 可選
- 進入 控制面板 -> 系統和安全 -> 系統
- 打開Windows的高級系統設置
- 打開環境變量設置
- 修改用戶變量中的Path變量,爲其新增一項,內容爲Apache2.4的安裝目錄下的bin目錄
在啓動前的修改必要的配置
設置SRVROOT和ServerRoot
修改httpd.conf,設置ServerRoot( 服務器所在目錄 )與SRVROOT(如果配置文件存在此關鍵詞則設置,否則不必理會,有的配置文件可能沒有) 【Apache安裝目錄/conf/httpd.conf】
Define SRVROOT "D:\Apache24"
ServerRoot "D:\Apache24"
啓動與停止Apache2.4
- 使用windows的服務進行啓動和停止
- 在命令行啓動/停止服務
啓動服務 httpd.exe -k start 停止服務 httpd.exe -k stop
新增監聽地址和端口
若要監聽多個端口,則需要修改httpd.conf,新增Listen選項【Apache安裝目錄/conf/httpd.conf】
httpd.conf截選,如下所示的配置將監聽本機全部IP的80端口和換回測試地址的9000端口
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 0.0.0.0:80
Listen 127.0.0.1:9000
配置虛擬主機
- 修改httpd.conf,加載虛擬主機模塊 【Apache安裝目錄/conf/httpd.conf】
找到此行內容,把行首的#號去掉 LoadModule vhost_alias_module modules/mod_vhost_alias.so
- 修改httpd.conf, 包含子配置文件httpd-vhosts.conf 【Apache安裝目錄/conf/extra/httpd-vhosts.conf】
找到此行內容,把行首的#號去掉 Include conf/extra/httpd-vhosts.conf
- 修改httpd-vhosts.conf,新增虛擬主機配置,示例如下
<VirtualHost *:80> #監聽本機全部IP的80號端口 ServerAdmin [email protected] DocumentRoot "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend" #指定網站文件位置 ServerName www.GBlog.com ErrorLog "logs/GBlog-error.log" #指定錯誤日誌文件的位置 TransferLog "logs/GBlog-access.log" #指定訪問日誌文件的位置 #控制指定文件夾的訪問權限和其他權限 <Directory "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend"> # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Require all granted </Directory> </VirtualHost>
- 重啓Apahce服務
httpd -k restart
配置反向代理 - 跨域的需求
- 修改httpd.conf,加載反向代理必備模塊 【Apache安裝目錄/conf/httpd.conf】
#將一下條目行首的'#'去掉 LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_http_module modules/mod_proxy_http.so
- 修改httpd-vhosts.conf中的虛擬主機配置,示例如下【Apache安裝目錄/conf/extra/httpd-vhosts.conf】
<VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend" ServerName www.GBlog.com ErrorLog "logs/GBlog-error.log" TransferLog "logs/GBlog-access.log" <Directory "E:/Projects/Python-VS/learning/Blog Website/GBlog_frontend"> # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Require all granted </Directory> ProxyPass /api/ http://127.0.0.1:9000/ ProxyPassReverse /api/ http://127.0.0.1:9000/ </VirtualHost>
關於mod_wsgi
自行搜索下載即可
注意
mod_wsgi使用的編譯器版本必須與Apache httpd使用的編譯器版本保持一致,
如均爲VC14,或均爲VC9,或均爲VC15
若使用Python2.7,那麼,你可能只能找到VC9編譯的mod_wsgi,此時也請搜索使用VC9編譯的Apache,如Apache 2.4.27
Flask應用部署案例 (以一個簡易的博客爲例)
與下文CentOS 7部署Flask應用方式一致
Linux環境
CentOS 7 (x64)
編譯安裝Apache 2.4.41
- 安裝依賴組件, 下載解壓安裝包
yum install -y apr apr-util pcre apr-devel apr-util-devel pcre-devel openssl-devel openssl yum install -y perl perl-devel wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.4.41.tar.bz2 tar -xvf httpd-2.4.41.tar.bz2
- 配置編譯選項
cd httpd-2.4.41 ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite -enable-ssl --with-npm=event
- 編譯和安裝
make make install
mod_wsgi的編譯,安裝與加載
首先,在 https://github.com/GrahamDumpleton/mod_wsgi/releases 下載mod_wsgi的源碼
Python 2.7.17 + mod_wsgi 4.6.5
注意,建議編譯前進入Python 2.7.17的虛擬環境,或者手動指定Python路徑
Python2.7.17的動態鏈接庫必須在系統的動態鏈接庫搜索路徑中
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.tar.gz
tar -xvf 4.6.5.tar.gz
cd mod_wsgi-4.6.5
# 指定apxs的路徑和Python的路徑
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/opt/Python/2.7.17/bin/python2.7
make
make install
在上述流程完成後,即可在apache2安裝目錄/modules找到mod_wsgi.so
編輯httpd.conf,加載模塊mod_wsgi 【Apache安裝目錄/conf/httpd.conf】
在加載模塊處,新增如下行
LoadModule wsgi_module modules/mod_wsgi.so
Python 3.x + mod_wsgi 4.6.5
方式同Python 2.7.x
在啓動前的修改必要的配置
測試配置文件是否存語法錯誤
apachectl -t
配置文件無語法錯誤則輸出
Syntax OK
設置ServerRoot
與windows下配置方式一致
啓動與停止Apache2.4
- 使用apachectl控制服務
查詢服務的狀態 apachectl status 啓動服務 apachectl start 停止服務 apachectl stop
配置虛擬主機
前置步驟與windows一致,見上文windows配置部分。
httpd-vhost.conf 示例配置如下
# 監聽全部IP的80端口
<VirtualHost 0.0.0.0:80>
ServerAdmin *********@qq.com
DocumentRoot "/home/web/GBlog/" # 項目所在目錄
<Directory "/home/web/GBlog/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog "logs/GBlog-error_log" # 錯誤日誌
CustomLog "logs/GBlog-access_log" common # 一般訪問日誌
</VirtualHost>
配置反向代理 - 跨域的需求
前置步驟與windows一致,見上文windows配置部分。
httpd-vhost.conf 示例配置如下
# 監聽全部IP的80端口
<VirtualHost 0.0.0.0:80>
ServerAdmin *********@qq.com
DocumentRoot "/home/web/GBlog/" # 項目所在目錄
<Directory "/home/web/GBlog/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog "logs/GBlog-error_log" # 錯誤日誌
CustomLog "logs/GBlog-access_log" common # 一般訪問日誌
#將以url以/api/開頭的請求轉發給本機的9000端口處理
ProxyPass /api/ http://127.0.0.1:9000/
ProxyPassReverse /api/ http://127.0.0.1:9000/
</VirtualHost>
Flask應用部署案例 (以一個簡易的博客爲例)
- 項目結構如下
|-- GBlog | |-- blueprints | | |-- auth.py | | |-- blog.py | | |-- __init__.py | | | |-- __init__.py | |-- models.py | |-- settings.py | |-- wsgi.py
- 在httpd-vhost.conf中的相應配置,需要指定wsgi文件的具體位置 【Apache安裝目錄/conf/extra/httpd-vhost.conf】
# 監聽全部IP的80端口 <VirtualHost 0.0.0.0:80> ServerAdmin *********@qq.com DocumentRoot "/home/web/GBlog/" # 項目所在目錄 <Directory "/home/web/GBlog/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> ErrorLog "logs/GBlog-error_log" # 錯誤日誌 CustomLog "logs/GBlog-access_log" common # 一般訪問日誌 WSGIScriptAlias / "/home/web/GBlog/wsgi.py" # wsgi文件路徑 </VirtualHost>
- wsgi文件內容 【使用指定的python虛擬環境】
import os import sys # 獲取項目所在的目錄的絕對路徑,並追加到Python的搜索路徑 BASE_DIR = os.path.abspath( os.path.dirname( os.path.dirname(__file__) ) ) sys.path.append(BASE_DIR) # 指定要使用的Python虛擬環境的具體路徑 activate_this = os.path.join(BASE_DIR, r'.venv/bin/activate_this.py') # 激活Python虛擬環境 execfile(activate_this, dict(__file__=activate_this)) from GBlog import create_app # 此函數創建一個Flask實例 app = create_app() application = app # application務必存在