杰瑞科技汇

linux 安装 mod_python

下面我将详细介绍在主流发行版(如 Debian/Ubuntu 和 CentOS/RHEL)上的安装步骤,并提供一个简单的测试方法。

linux 安装 mod_python-图1
(图片来源网络,侵删)

⚠️ 重要提示:mod_python 的现状

mod_python 项目已经不再积极维护多年,对于新的项目,强烈推荐使用更现代、更活跃的替代方案,

  • mod_wsgi: 这是目前 Python 在 Apache 上部署的事实标准,它性能更好,功能更全,并且持续更新,如果你在规划新项目,请优先考虑 mod_wsgi
  • uWSGI + Nginx: 这是一个非常流行的组合,性能和稳定性都非常出色。

尽管如此,如果你需要维护一个旧的 mod_python 项目,或者有特殊需求,下面的安装指南仍然适用。


在 Debian / Ubuntu 系统上安装

在基于 Debian 和 Ubuntu 的系统上,mod_python 通常可以通过系统的包管理器 apt 直接安装。

更新软件包列表

确保你的软件包列表是最新的:

linux 安装 mod_python-图2
(图片来源网络,侵删)
sudo apt update

安装 mod_python 和 Apache

使用 apt 一键安装 libapache2-mod-pythonapache2 包。

sudo apt install apache2 libapache2-mod-python

这个命令会自动安装 Apache Web 服务器,并启用 mod_python 模块。

验证模块是否加载

安装完成后,Apache 应该已经自动加载了 mod_python,你可以使用以下命令来验证:

apache2ctl -M | grep python

如果看到类似下面的输出,说明安装成功:

linux 安装 mod_python-图3
(图片来源网络,侵删)
python_module (shared)

如果没有看到,你可能需要手动启用它:

sudo a2enmod python
sudo systemctl restart apache2

在 CentOS / RHEL / Rocky Linux / AlmaLinux 系统上安装

在基于 Red Hat 的系统上,情况稍微复杂一些。mod_python 通常不在官方的默认软件源中,但可以在 EPEL (Extra Packages for Enterprise Linux) 软件源中找到。

安装 EPEL 软件源

你需要启用 EPEL 软件源,以 CentOS 7/8/9 为例:

# 对于 CentOS/RHEL 7
sudo yum install epel-release
# 对于 CentOS/RHEL 8 / Rocky Linux 8 / AlmaLinux 8
sudo dnf install epel-release
# 对于 CentOS/RHEL 9 / Rocky Linux 9 / AlmaLinux 9
sudo dnf install epel-release

安装 mod_python 和 Apache

启用 EPEL 后,就可以使用 yumdnf 来安装了。

# 对于使用 yum 的系统 (如 CentOS 7)
sudo yum install mod_python httpd
# 对于使用 dnf 的系统 (如 CentOS 8/9, RHEL 8/9)
sudo dnf install mod_python httpd

验证模块是否加载

安装后,启动 Apache 服务并验证模块:

# 启动并设置开机自启
sudo systemctl start httpd
sudo systemctl enable httpd
# 验证模块
httpd -M | grep python

你应该能看到类似下面的输出:

python_module (shared)

如果没有,确保 mod_python 模块在 Apache 的配置文件中被正确加载,安装包会自动处理。


从源代码编译安装(通用方法)

如果你的发行版没有预编译的包,或者你需要特定版本的 mod_python,可以从源代码编译安装。

安装编译依赖

你需要安装 make, gcc, httpd-devel (或 apache2-dev) 等工具。

  • 在 Debian/Ubuntu 上:

    sudo apt install build-essential apache2-dev
  • 在 CentOS/RHEL 上:

    sudo yum install gcc make httpd-devel  # 对于 dnf, 使用 dnf install

下载 mod_python 源码

从官方的 mod-python 下载页面 获取最新的源码压缩包,这里以 mod_python-3.5.0 为例。

# 创建一个临时目录
cd /tmp
wget http://downloads.sourceforge.net/project/mod-python/mod-python/3.5.0/mod_python-3.5.0.tgz
tar -xvzf mod_python-3.5.0.tgz
cd mod_python-3.5.0

配置、编译和安装

运行标准的 configure, make, make install 命令。

# 运行配置脚本,指定 Apache 的 apxs 路径
# apxs 通常在 /usr/bin/apxs 或 /usr/sbin/apxs
./configure --with-apxs=/usr/bin/apxs
# 编译
make
# 安装
sudo make install

配置 Apache

安装完成后,你需要手动在 Apache 的主配置文件(通常是 /etc/apache2/apache2.conf/etc/httpd/conf/httpd.conf)中添加一行来加载模块:

LoadModule python_module modules/mod_python.so

重启 Apache

保存配置文件后,重启 Apache 以使更改生效:

# Debian/Ubuntu
sudo systemctl restart apache2
# CentOS/RHEL
sudo systemctl restart httpd

配置和测试

安装完成后,你需要配置一个目录来运行 Python 脚本。

创建一个测试目录和脚本

# 创建一个网站目录
sudo mkdir -p /var/www/python-test
sudo chown -R $USER:$USER /var/www/python-test  # 将所有权给你当前用户,方便编辑
# 创建一个 Python 测试脚本
nano /var/www/python-test/test.py

粘贴到 test.py 文件中:

from mod_python import apache
def handler(req):
    req.content_type = 'text/plain'
    req.write("Hello from mod_python!\n")
    req.write("The requested URI was: %s\n" % req.uri)
    return apache.OK

配置 Apache 虚拟主机或目录

编辑 Apache 的配置文件,为你的测试目录添加配置。

  • 方法 A: 在主配置文件中添加 (/etc/apache2/apache2.conf/etc/httpd/conf/httpd.conf)

    <Directory /var/www/python-test>
        # 关闭 Options 中的 ExecCGI,因为我们用 mod_python
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
        # 关键配置:将 .py 文件映射到 mod_python 的 handler
        AddHandler mod_python .py
        PythonHandler test
        PythonDebug On
    </Directory>
  • 方法 B: 创建一个新的虚拟主机配置文件 (推荐)

    创建一个新文件,/etc/apache2/sites-available/python-test.conf (Debian/Ubuntu) 或 /etc/httpd/conf.d/python-test.conf (CentOS/RHEL):

    <VirtualHost *:80>
        ServerName your_server_ip_or_domain
        DocumentRoot /var/www/python-test
        <Directory /var/www/python-test>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
            AddHandler mod_python .py
            PythonHandler test
            PythonDebug On
        </Directory>
    </VirtualHost>

启用配置并重启 Apache

  • 在 Debian/Ubuntu 上:

    # 启用新站点
    sudo a2ensite python-test.conf
    # 如果修改了主配置,确保没有冲突
    # 重启 Apache
    sudo systemctl restart apache2
  • 在 CentOS/RHEL 上:

    # 重启 Apache (配置文件在 conf.d 中会自动加载)
    sudo systemctl restart httpd

访问测试页面

打开你的浏览器,访问 http://your_server_ip_or_domain/test.py

你应该能看到类似下面的输出:

Hello from mod_python!
The requested URI was: /test.py

如果看到这个页面,恭喜你,mod_python 已经成功安装并配置好了!

分享:
扫描分享到社交APP
上一篇
下一篇