要在Apache服务器上使用Python,您需要安装并配置Apache模块mod_python或mod_wsgi。以下是使用mod_python的基本步骤:
1. 安装Apache服务器。
2. 安装mod_python模块。
3. 配置Apache以使用mod_python。
5. 测试Python脚本是否通过Apache正确运行。
安装Apache和mod_python
对于Ubuntu/Debian系统:
安装Apache$ sudo apt-get install apache2安装mod_python$ sudo apt-get install libapache2-mod-python

对于CentOS系统:
启动httpd服务$ service httpd start设置httpd服务开机自启$ chkconfig --level 345 httpd on
配置Apache
编辑Apache配置文件,通常位于`/etc/apache2/sites-available/`或`/etc/httpd/conf/`目录下,并添加以下内容:
SetHandler mod_python .pyPythonHandler testPythonDebug On
创建Python脚本文件
在Apache的`htdocs`目录下创建一个Python脚本文件,例如`test.py`,并添加以下内容:
from mod_python import apachedef handler(req):req.content_type = 'text/html'req.write('Hello World!
