mod_python安装配置教程(apache+mod_python配置笔记)

发布时间:2019-07-24编辑:脚本学堂
本文介绍了mod_python的安装与配置方法,在apache服务器上添加python扩展支持,需要mod_python.so模块的安装,有关mod_python的安装配置实例,需要的朋友参考下。

apache下mod_python安装配置教程

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>

jbxue小编提醒,如果是yum安装的,配置文件已自动写到httpd目录下的conf.d中,文件名称为python.conf,无需再手动添加。

5、测试python程序能否运行
在/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中

以上通过实例介绍了mod_python在apache上的安装配置方法,学习了python web程序的简单例子与测试,希望对大家有所帮助。