
- Python Pyramid 教程
- Python Pyramid - 首頁
- Python Pyramid - 概述
- Pyramid - 環境設定
- Python Pyramid - Hello World
- Pyramid - 應用配置
- Python Pyramid - URL 路由
- Python Pyramid - 檢視配置
- Python Pyramid - 路由字首
- Python Pyramid - 模板
- Pyramid - HTML 表單模板
- Python Pyramid - 靜態資源
- Python Pyramid - 請求物件
- Python Pyramid - 響應物件
- Python Pyramid - 會話
- Python Pyramid - 事件
- Python Pyramid - 訊息閃現
- Pyramid - 使用 SQLAlchemy
- Python Pyramid - Cookiecutter
- Python Pyramid - 建立專案
- Python Pyramid - 專案結構
- Python Pyramid - 包結構
- 手動建立專案
- 命令列 Pyramid
- Python Pyramid - 測試
- Python Pyramid - 日誌記錄
- Python Pyramid - 安全性
- Python Pyramid - 部署
- Python Pyramid 有用資源
- Python Pyramid - 快速指南
- Python Pyramid - 有用資源
- Python Pyramid - 討論
Python Pyramid - 部署
本教程中迄今為止開發的 Pyramid 應用示例已在本地機器上執行。為了使其公開訪問,必須將其部署到能夠支援 WSGI 標準的生產伺服器上。
為此,可以使用許多相容 WSGI 的 HTTP 伺服器。例如:
- waitress
- paste.httpserver
- CherryPy
- uWSGI
- gevent
- mod_wsgi
我們已經討論瞭如何使用 Waitress 伺服器來託管 Pyramid 應用。它可以在具有公共 IP 地址的機器的 80(HTTP)和 443(HTTPS)埠上執行。
mod_wsgi
Apache 伺服器是一種流行的開源 HTTP 伺服器軟體,由 Apache 軟體基金會發行。它為網際網路上的大多數 Web 伺服器提供支援。mod_wsgi(由Graham Dumpleton開發)是一個 Apache 模組,它提供了一個 WSGI 介面,用於在 Apache 上部署基於 Python 的 Web 應用。
在本節中,將解釋在 Apache 伺服器上部署 Pyramid 應用的分步過程。在這裡,我們將使用 XAMPP,這是一個流行的開源 Apache 發行版。它可以從https://www.apachefriends.org/download.html下載。
mod_wsgi 模組使用 PIP 安裝程式安裝。在安裝之前,請將 MOD_WSGI_APACHE_ROOTDIR 環境變數設定為 Apache 可執行檔案所在的目錄。
C:\Python310\Scripts>set MOD_WSGI_APACHE_ROOTDIR=C:/xampp/apache C:\Python310\Scripts>pip install mod_wsgi
接下來,在命令終端執行以下命令。
C:\Python310\Scripts>mod_wsgi-express module-config LoadFile "C:/Python310/python310.dll" LoadModule wsgi_module "C:/Python310/lib/site-packages/mod_wsgi/server/mod_wsgi.cp310-win_amd64.pyd" WSGIPythonHome "C:/Python310"
這些是需要新增到 Apache 配置檔案的 mod_wsgi 模組設定。開啟 XAMPP 安裝的httpd.conf檔案,並將上述命令列的輸出複製到其中。
接下來,為我們的應用建立一個虛擬主機配置。Apache 將虛擬主機資訊儲存在httpd-vhosts.conf檔案中,該檔案位於 C:\XAMPP\Apache\conf\extra\ 資料夾中。開啟該檔案並在其中新增以下幾行:
<VirtualHost *> ServerName localhost:6543 WSGIScriptAlias / e:/pyramid-env/hello/production.ini <Directory e:/pyramid-env/hello> Order deny,allow Allow from all Require all granted </Directory> </VirtualHost>
這裡假設使用 Cookiecutter 實用程式構建了一個 hello Pyramid 專案。這裡使用了在生產環境中使用的 PasteDeploy 配置檔案。
需要將此虛擬主機配置新增到 Apache 的 httpd.conf 檔案中。這可以透過在其中新增以下幾行來完成:
# Virtual hosts Include conf/extra/httpd-vhosts.conf
現在,我們必須將以下程式碼儲存為pyramid.wsgi檔案,該檔案返回 Pyramid WSGI 應用物件。
from pyramid.paster import get_app, setup_logging ini_path = 'e:/pyramid-env/hello/production.ini' setup_logging(ini_path) application = get_app(ini_path, 'main')
完成上述步驟後,重新啟動 XAMPP 伺服器,我們應該能夠在 Apache 伺服器上執行 Pyramid 應用。
在 Uvicorn 上部署
Uvicorn 是一個相容 ASGI 的伺服器(ASGI 代表非同步閘道器介面)。由於 Pyramid 是一個基於 WSGI 的 Web 框架,我們需要藉助asgiref.wsgi模組中定義的WsgiToAsgi()函式,將 WSGI 應用物件轉換為 ASGI 物件。
from asgiref.wsgi import WsgiToAsgi from pyramid.config import Configurator from pyramid.response import Response def hello_world(request): return Response("Hello") with Configurator() as config: config.add_route("hello", "/") config.add_view(hello_world, route_name="hello") wsgi_app = config.make_wsgi_app() app = WsgiToAsgi(wsgi_app)
將上述程式碼儲存為 app.py。使用 pip 實用程式安裝 Uvicorn。
pip3 install uvicorn
在 ASGI 模式下執行 Pyramid 應用。
uvicorn app:app
同樣,它可以使用daphne伺服器執行。
daphne app:app