10.1 安裝
其中不同版本的MOD_PYTHON對於所支持的APACHE版本及PYTHON版本的是有嚴格限制的,
各位在下載時必須注意,並找到對應的版本,否則可能會出現問題。
下載完畢後,對MOD_PYTHON進行安裝即可。
10.2 配置
<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test">
##<Directory>中寫入要運行的python腳本所在的路徑,這設定將對子目錄有效
AllowOverride FileInfo ##必須加上,否則PY不能運行。
AddHandler mod_python .py ##說明在目錄下所有.py文件都由MOD_PYTHON負責執行
SetHandler mod_python
PythonHandler test ##入口的PY名稱,在這例子,所有PY在運行時,都會先找到TEST.py中的handler函數作初始工作。
##嚴重注意:如果客戶端是使用POST方法傳送資料,則必須在handler中調用read()方法,否則POST信息將被APACHE截取。
PythonHandler mod_python.publisher
##使用publisher後,客戶端就可調用某一.py中的某一函數,如Http://localhost/test/test1.py/hello?word='hi'
PythonDebug On ##將出錄信息打印到APACHE的日誌中
</Directory>
附註:如果要在 apache + mod_python 中只運行 .pyo/.pyc 文件則需要將配置修改如下:
入口(如本例的test222.py)需要保留 .py腳本文件,否則將會找不到入口,
其他文件可以用編譯後的.pyc/.pyo文件
PythonOptimize On //這是引入python 的優化機制,並沒有影響
<Directory "D:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test1">
AllowOverride All
PythonAuthzHandler test::handler //增加這句test::handler其中的handler是入口函數,
//中間的“::”按文檔說的:能提高效率
//如果有包或者是文件夾的情況可以寫成 test1.test::handler
AddHandler mod_python .pyc
AddHandler mod_python .pyo
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
</Directory>