Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Apache 載入 mod_python 執行 Python CGI 簡易配置

編輯:關於伺服器

在 CentOS 6 上透過 EPEL 來源安裝 mod_python,以最簡單方式運行 Python CGI 腳本。注意:mod_python 目前已停止維護,新專案建議改用 mod_wsgi 或 uWSGI/Gunicorn + Nginx 架構。

安裝

yum install epel-release
yum install mod_python

httpd.conf 配置片段

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 反向代理,獲得更好效能、穩定性與社群支援。

Copyright © Windows教程網 All Rights Reserved