如何在 CentOS 7 上使用 python3.5 为 apache 2.4+ 安装 mod_wgsi

How to install mod_wgsi for apache 2.4+ with python3.5 on CentOS 7

正如标题所说"How to install mod_wgsi for python3.5 on CentOS 7"?

$pip3.5 安装mod_wgsi 没用

Collecting mod_wgsi
  Could not find a version that satisfies the requirement mod_wgsi (from versions: )
No matching distribution found for mod_wgsi

sudo yum install libapache2-mod-wsgi-py3 也失败了:

Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with Subscription Management. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
 * base: mirror.daniel-jost.net
 * epel: mirrors.n-ix.net
 * extras: mirror.daniel-jost.net
 * ius: mirror.amsiohosting.net
 * remi: mirror.23media.de
 * remi-php56: mirror.23media.de
 * remi-safe: mirror.23media.de
 * updates: mirror.daniel-jost.net
No package libapache2-mod-wsgi-py3 available.
Error: Nothing to do

非常欢迎任何关于如何在 CentOS 7 上 运行 apache2.4+ mod_wsgi 和 python3.5 的建议!

您是否查看了 rh-python35 软件集,它提供了 rh-python35-mod_wsgi 软件包?

有关 SCL 的更多信息,请参阅:

我看到您已经启用了 IUS 存储库。无需跳过 SCL 环,您只需安装一个普通包即可。

yum install python35u-mod_wsgi

这将使用标准文件系统位置来处理现有的 Apache HTTPD 2.4。

/etc/httpd/conf.modules.d/10-wsgi-python3.5.conf
/usr/lib64/httpd/modules/mod_wsgi_python3.5.so

我尝试关注,但没有解决问题。原来我安装的版本安装后需要一些额外的配置步骤。

背景

我在安装 mod_wsgi 升级之前查看了 Apache 的 modules 文件夹:

$ ls -l /lib64/httpd/modules
[...]
-rwxr-xr-x. 1 root root 172800 Oct 30 22:44 mod_wsgi.so

然后我安装了 SCL 存储库,并查看了哪些 mod_wsgi 版本可用。

$ sudo yum install -q -y centos-release-scl
[...]
$ yum search mod_wsgi
[...]
koschei-frontend.noarch : Web frontend for koschei using mod_wsgi
mod_wsgi.x86_64 : A WSGI interface for Python web applications in Apache
python27-mod_wsgi.x86_64 : A WSGI interface for Python web applications in Apache
python33-mod_wsgi.x86_64 : A WSGI interface for Python web applications in Apache
rh-python34-mod_wsgi.x86_64 : A WSGI interface for Python web applications in Apache
rh-python35-mod_wsgi.x86_64 : A WSGI interface for Python web applications in Apache
rh-python36-mod_wsgi.x86_64 : A WSGI interface for Python web applications in Apache
viewvc-httpd-wsgi.noarch : ViewVC configuration for Apache/mod_wsgi
[...]

我使用的是 Python 3.6,所以我安装了匹配的版本并重新启动了 Apache。

$ sudo yum install -q -y rh-python36-mod_wsgi
[...]
$ sudo systemctl restart httpd

遗憾的是,这并没有解决问题。当我查看 Apache 的 modules 文件夹时,没有任何变化。奇怪!

$ ls -l /lib64/httpd/modules
[...]
-rwxr-xr-x. 1 root root 172800 Oct 30 22:44 mod_wsgi.so

那么安装了什么?

$ rpm -ql rh-python36-mod_wsgi
/opt/rh/httpd24/root/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf
/opt/rh/httpd24/root/usr/lib64/httpd/modules/mod_rh-python36-wsgi.so
/opt/rh/rh-python36/root/usr/share/doc/rh-python36-mod_wsgi-4.5.18
/opt/rh/rh-python36/root/usr/share/doc/rh-python36-mod_wsgi-4.5.18/CREDITS.rst
/opt/rh/rh-python36/root/usr/share/doc/rh-python36-mod_wsgi-4.5.18/LICENSE
/opt/rh/rh-python36/root/usr/share/doc/rh-python36-mod_wsgi-4.5.18/README.rst

额外的配置步骤

它安装了我需要的文件,但没有把它们放在任何有用的地方。根据 README.rst 文件的一些提示,我将它们复制到正确的位置。

sudo cp /opt/rh/httpd24/root/usr/lib64/httpd/modules/mod_rh-python36-wsgi.so /lib64/httpd/modules
sudo cp /opt/rh/httpd24/root/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf /etc/httpd/conf.modules.d
sudo systemctl restart httpd

现在我有了正确的 mod_wsgi 版本,我的 Django 应用程序将 运行 在 Apache 下。

我会使用 SCL packages for python 3.6(尽管可以随意替换下面的 3.5)。

开始:

yum install centos-release-scl
yum install rh-python36 rh-python36-mod_wsgi

请注意,这将引入 SCL 包 httpd24-httpd 并将 mod_wsgi 文件放入该安装中。我建议您使用该安装方式,而不是 安装基本的 CentOS httpd 软件包。在撰写本文时,对于 CentOS 7,httpd 软件包是 2.4.6,httpd24-httpd 软件包是 2.4.37。

然后你创建一个虚拟环境:

/opt/rh/rh-python36/root/usr/bin/python -m venv /path/to/venv36
source /path/to/venv36/bin/activate
pip install ...

现在您可以将站点的配置放在 /opt/rh/httpd24/root/etc/httpd/conf.d/mysite.conf 中,其中可能包含如下内容:

<VirtualHost *:80>
    LoadModule wsgi_module modules/mod_wsgi.so

    ErrorLog /var/log/httpd24/mysite-err.log
    CustomLog  /var/log/httpd24/mysite.log combined

    # recommended way of setting DJANGO_SETTINGS_MODULE 
    WSGIProcessGroup mysite.settings.production
    WSGIDaemonProcess mysite.settings.production python-path=/path/to/mysite/:/path/to/venv36/lib/python3.6/site-packages
    WSGIScriptAlias / /path/to/mysite/wsgi.py process-group=mysite application-group=%{GLOBAL}
</VirtualHost>

现在启动 SCL apache:

systemctl start httpd24-httpd

而且您的网站应该可以正常运行。