|
|
❯ sudo cp /var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so /etc/httpd/modules/mod_wsgi_python3.so
这里用3.6版本的覆盖了3.12版本的so,当然标准的程序是不能这么做的。应该自己配置好哪个so文件会被优先访问。毕竟在/etc/httpd/conf.d/wsgi.conf 中写明了:
LoadModule wsgi_module "/var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/Treemap/venv" 请问应该如何配置好httpd哪个so文件会被优先访问?
● 问题在于 Fedora 系统的 mod_wsgi 包也会装一个加载配置。两个 LoadModule 冲突时,先加载的那个生效。
排查
先看系统有没有自带的 mod_wsgi 加载配置:
ls /etc/httpd/conf.modules.d/*wsgi*
通常会有个 10-wsgi.conf,内容类似:
LoadModule wsgi_module modules/mod_wsgi_python3.so
这个文件会在你的 conf.d/wsgi.conf 之前加载(conf.modules.d 优先于 conf.d)。
解决方法
禁用系统自带的 mod_wsgi,只保留你的:
# 1. 把系统自带的 mod_wsgi 配置禁用(不要删,改后缀即可)
sudo mv /etc/httpd/conf.modules.d/10-wsgi.conf /etc/httpd/conf.modules.d/10-wsgi.conf.bak
# 2. 确认你的 /etc/httpd/conf.d/wsgi.conf 内容正确
# LoadModule 指向 venv 里的 .so,WSGIPythonHome 指向 venv
你的 /etc/httpd/conf.d/wsgi.conf 保持:
LoadModule wsgi_module "/var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/Treemap/venv"
# 3. 如果之前覆盖了系统 .so,还原回去
sudo dnf reinstall mod_wsgi # 恢复原始的 3.12 版 .so 文件
# 4. 验证配置无误后重启
sudo apachectl configtest
sudo systemctl restart httpd
加载顺序说明
httpd 按字母序加载配置:
1. conf.modules.d/ — 模块加载(10-wsgi.conf 等)
2. conf.d/ — 应用配置(你的 wsgi.conf)
LoadModule 对同一个模块名只能出现一次,第二次会报错 module wsgi_module is already loaded。所以必须禁用其中一个,不存在"优先级"的概念——只能加载一个。
❯ 按你的改了.sudo systemctl restart httpd 报错:The job identifier is 9182.
Mar 27 21:23:38 bogon (httpd)[23124]: httpd.service: Referenced but unset environment variable evaluates to an empty string: OPTIONS
Mar 27 21:23:38 bogon httpd[23124]: httpd: Syntax error on line 358 of /etc/httpd/conf/httpd.conf: Syntax error on line 1 of /etc/httpd/conf.d/wsgi.conf: Cannot
load /var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so into server:
/var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so: failed to map segment from shared object
Mar 27 21:23:38 bogon systemd[1]: httpd.service: Main process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ An ExecStart= process belonging to unit httpd.service has exited.
░░
░░ The process' exit code is 'exited' and its exit status is 1.
Mar 27 21:23:38 bogon systemd[1]: httpd.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ The unit httpd.service has entered the 'failed' state with result 'exit-code'.
Mar 27 21:23:38 bogon systemd[1]: Failed to start httpd.service - The Apache HTTP Server.
░░ Subject: A start job for unit httpd.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░
░░ A start job for unit httpd.service has finished with a failure.
░░
░░ The job identifier is 9182 and the job result is failed.
● failed to map segment from shared object — 这是 SELinux 阻止 httpd 加载 venv 目录下的 .so 文件。
修复:
# 给 venv 下的 mod_wsgi .so 文件设置正确的 SELinux 上下文
sudo semanage fcontext -a -t httpd_modules_t "/var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/.*\.so"
sudo restorecon -v /var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
# 验证上下文已生效
ls -Z /var/www/Treemap/venv/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
应该看到类型变成 httpd_modules_t。然后:
sudo apachectl configtest
sudo systemctl restart httpd
如果 semanage 命令不存在,先安装:
sudo dnf install policycoreutils-python-utils
——如果selinux仍有问题,对之最好的方法是停用selinux。
- sudo sed -i 's/^SELINUX=enforcing/SELINUX=disable/' /etc/selinux/config
复制代码 完全停用不记录。
或SELINUX=permissive 只记录,不阻止。
|
|