要讓 Apache 支援 Python,可以安裝 mod_python。
在 ports 裡有兩個版本,一個是 mod_python 2.7、一個是 mod_python 3.3,這兩種分別適用於不同版本的 Apache:
- mod_python 2.7
-
- Python 1.5.2, 1.6, 2.0 or 2.1
- Apache 1.3
- ports 路徑: /usr/ports/www/mod_python/
- mod_python 3.3
-
- Python 2.3.4 or later.
- Apache 2.0.54 or later.
- ports 路徑: /usr/ports/www/mod_python3/
挑選適合的 mod_python 版本後,到 ports 目錄下執行 make install clean 完成安裝。
安裝後會出現設定的說明:
Also remember to add to your Apache configuration in the appropriate context: PythonPath "['/path/to/foo', '/path/to/bar']" AddHandler python-program .py PythonHandler foobar PythonDebug On For more information, see http://www.modpython.org/. ================================================================================ ===> Registering installation for mod_python-3.3.1_2
接著修改 httpd.conf,加入以下設定,讓 Apache 支援 Python:
LoadModule python_module libexec/apache22/mod_python.so
(mod_python 安裝之後,在 /usr/local/libexec/apache22/ 目錄下會多了一個 mod_python.so)
然後再依據安裝完之後的說明設定便可,比如說要在目錄 /some/directory/ 下運行 Python,則設定如下:
<Directory /some/directory/>
AddHandler mod_python .py
PythondHandler python
PythonDebug On
</Directory>