简体   繁体   English

尝试在CentOS上以守护程序模式配置Django / mod_wsgi应用程序时python-home选项失败

[英]python-home option fails when attempting to configure Django/mod_wsgi application in daemon mode on CentOS

I have a Django application running on Centos 7.5 with Apache 2.4 and mod_wsgi 4.6.4. 我有一个在Apache 2.4和mod_wsgi 4.6.4的Centos 7.5上运行的Django应用程序。 I'm having a hard time getting mod_wsgi to use the virtual environment I want it to use. 我很难让mod_wsgi使用想要使用的虚拟环境。 I installed Python 3.6 via yum and used it to create a virtual environment. 我通过yum安装了Python 3.6,并用它创建了一个虚拟环境。 I compiled mod_wsgi from source, using the with-python option to point it to the correct Python binary. 我从源代码编译了mod_wsgi,使用with-python选项将其指向正确的Python二进制文件。 The (approximate) Apache configuration I expected to work, but which does not work, is the following: 我期望可以(但不起作用)的(大约)Apache配置如下:

# Required because of some third-party packages that use the 
# simplified GIL state API
WSGIApplicationGroup %{GLOBAL}

<VirtualHost *:80>
    WSGIDaemonProcess myapp python-home=/path/to/venv python-path=/path/to/my/app
    WSGIProcessGroup myapp
    WSGIScriptAlias / /path/to/my/app/wsgi.py
</VirtualHost>

<VirtualHost *:443>
    WSGIProcessGroup myapp
    WSGIScriptAlias / /path/to/my/app/wsgi.py
</VirtualHost>

This configuration can't find any of the packages installed in the virtual environment. 此配置找不到在虚拟环境中安装的任何软件包。 Dumping sys.path shows that the path doesn't contain /path/to/venv/lib/python3.6/site-packages as expected, but rather /lib/python3.6/site-packages (ie the system directory). 转储sys.path会显示该路径未按预期包含/path/to/venv/lib/python3.6/site-packages,而是/lib/python3.6/site-packages(即系统目录)。 The only way I've been able to get my app to run is by changing the WSGIDaemonProcess directive to: 我能够运行我的应用程序的唯一方法是将WSGIDaemonProcess指令更改为:

WSGIDaemonProcess myapp python-path=/path/to/my/app:/path/to/venv/lib/python3.6/site-packages

The first configuration (using python-home as recommended) does work for me on macOS 10.12 with Python 3.6 and mod_wsgi 4.5.24. 第一个配置(建议使用python-home )在具有Python 3.6和mod_wsgi 4.5.24的macOS 10.12上确实适合我。 A big difference between the two environments is the fact that SELinux is enabled on the CentOS server, but audit2allow shows nothing that explains my issue. 两种环境之间的最大区别是在CentOS服务器上启用了SELinux,但audit2allow却没有显示任何解释我问题的事实。

I got it figured out. 我明白了。 I'm still not sure why I wasn't able to figure this out the first time, but it was an SELinux issue, as suspected. 我仍然不确定为什么我不能第一次弄清楚这一点,但是怀疑这是SELinux的问题。 The problem was that everything under the virtual environment directory needed to have the httpd_sys_content_t context, except dynamically-loaded binary libraries, which need httpd_sys_script_exec_t . 问题在于,虚拟环境目录下的所有内容都需要具有httpd_sys_content_t上下文,但动态加载的二进制库除外,后者需要httpd_sys_script_exec_t Once I did that, the python-home argument to WSGIDaemonProcess worked as expected. 一旦完成此操作, WSGIDaemonProcesspython-home参数即可按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM