确认Apache2是否为dev版本(开发版本才会配置apxs2)
在终端输入
whereis apxs2
如果没有显示路径,则需要安装dev版本,命令如下
sudo apt install apache2-dev
安装完成后再次查找apxs2此时应显示以下路径
apxs2: /usr/bin/apxs2 /usr/share/man/man1/apxs2.1.gz
接下来就可以安装mod_python
安装mod_python
1.确认python安装路径
whereis python3
此时应该显示以下路径,如果没有显示路径则需要安装python
python3: /usr/bin/python3 /usr/lib/python3 /etc/python3 /usr/share/python3 /usr/share/man/man1/python3.1.gz
2.从Github网站上下载mod_python压缩包
GitHub - grisha/mod_python: mod_python
下载后解压,请注意,解压包的文件路径上不能有中文字符,否则会导致后续步骤失败
进入解压后的目录运行终端并输入以下命令
./configure --with-apxs=/usr/bin/apxs2 --with-python=/usr/bin/python3
3.配置完成后进行编译
sudo make
sudo make install
安装完成后会在/usr/lib/apache2/modules/下生成mod_python.so文件,接下来需要配置Apache2,以加载这个文件
4.配置Apache2
编辑以下文件
/etc/apache2/apache2.conf
在其中增加以下代码,并保存
LoadModule python_module /usr/lib/apache2/modules/mod_python.so
<Directory /目标路径> #设定的路径,通常为py执行根目录
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On
</Directory>
然后使用以下命令重启Apache2,完成配置
sudo systemctl restart apache2.service