1.到www.modpython.org下載個新版 (注意版本問題apache和python版本)
2.拷到linux機器上,下面在命令行執行:
tar -zxvf mod_python-3.3.1.tgz
cd mod_python-3.3.1
./configure --with-apxs=/usr/local/apache/bin/apxs # 配置apxs目錄
./configure --with-python=/usr/bin/python2.5 # 配置本地python
make
make install
3.這些編譯完了,會在apache/modules/目錄下生成mod_python.so,大概3M左右。
4.配置apache的http.conf
LoadModule python_module modules/mod_python.so
<Directory "/usr/modpython"> # 能用apache訪問的目錄
#AddHandler mod_python .py
SetHandler mod_python
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
5.測試
在/usr/modpython/目錄下新建一個test.py
#coding:gb2312
def index(req):
req.write("hello,world!")
return
6.運行,啓動apache沒有錯誤後,打開http://localhost/modpython/test
即可看到helloworld了
7.定義其他方法:
#coding:gb2312
def index(req):
req.write("hello,world!")
return
def hello(req):
req.write("hello!!!")
return
可以通過:http://localhost/modpython/test/hello來訪問。
8.傳遞參數
def get(req,name=""):
if name:
req.write("參數:"+name);
else:
req.write("no param.");
return
可以通過:http://localhost/modpython/test/hello?name=smallfish來訪問。
POST表單一樣,只要參數名寫對就行。
9.python包
在當前目錄下建立一個包,然後在test.py導入時候會出錯,找不到包。後來修改了下方法
import os,sys
sys.path.append(os.path.dirname(__file__)) # 把當前目錄加入到sys.path中
import 自己的包
安裝結束了。