在 CentOS 6 上透過 EPEL 來源安裝 mod_python,以最簡單方式運行 Python CGI 腳本。注意:mod_python 目前已停止維護,新專案建議改用 mod_wsgi 或 uWSGI/Gunicorn + Nginx 架構。
yum install epel-release
yum install mod_python
LoadModule python_module modules/mod_python.so
<Directory "/var/www/html/py">
AllowOverride FileInfo
AddHandler mod_python .py
# PythonHandler index # 視專案入口點決定是否啟用
PythonDebug On
Order allow,deny
Allow from all
</Directory>
AddHandler mod_python .py:將 .py 副檔名交給 mod_python 處理。PythonHandler index:指定預設處理器 (模組名.函式名),註解掉則依檔案名匹配。PythonDebug On:開啟除錯訊息,開發階段建議開啟,生產環境關閉。建立 /var/www/html/py/test.py:
def index(req):
req.content_type = 'text/plain'
return 'Hello from mod_python!'
瀏覽 https://server-ip/py/test.py 應顯示輸出。
遷移建議: mod_python 專案建議規劃遷移至 mod_wsgi (Apache 內嵌) 或獨立 WSGI 伺服器 (Gunicorn/uWSGI) + Nginx 反向代理,獲得更好效能、穩定性與社群支援。