使用apache + mod_wsgi运行,导入错误:无法启用可执行堆栈,因为共享对象需要:权限被拒绝

时间:2015-10-15 09:30:12

标签: python apache flask scipy mod-wsgi

我在Flask和mod_wsgi中使用apache。 当我在Flask脚本上导入scipy模块时出现此错误。

[Thu Oct 15 18:15:45 2015] [error]   File "/usr/local/lib/python2.7/site-packages/pymatgen/electronic_structure/plotter.py", line 671, in get_plot3

[Thu Oct 15 18:15:45 2015] [error]     import scipy.interpolate as scint

[Thu Oct 15 18:15:45 2015] [error]   File "/usr/local/lib/python2.7/site-packages/scipy/interpolate/__init__.py", line 145, in <module>

[Thu Oct 15 18:15:45 2015] [error]     from .interpolate import *

[Thu Oct 15 18:15:45 2015] [error]   File "/usr/local/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 16, in <module>

[Thu Oct 15 18:15:45 2015] [error]     import scipy.special as spec

[Thu Oct 15 18:15:45 2015] [error]   File "/usr/local/lib/python2.7/site-packages/scipy/special/__init__.py", line 608, in <module>

[Thu Oct 15 18:15:45 2015] [error]     from ._ellip_harm import ellip_harm, ellip_harm_2, ellip_normal

[Thu Oct 15 18:15:45 2015] [error]   File "/usr/local/lib/python2.7/site-packages/scipy/special/_ellip_harm.py", line 7, in <module>

[Thu Oct 15 18:15:45 2015] [error]     from ._ellip_harm_2 import _ellipsoid, _ellipsoid_norm

[Thu Oct 15 18:15:45 2015] [error]   File "scipy/special/_ellip_harm_2.pyx", line 2, in init scipy.special._ellip_harm_2 (scipy/special/_ellip_harm_2.c:7444)

[Thu Oct 15 18:15:45 2015] [error]   File "/usr/local/lib/python2.7/ctypes/__init__.py", line 10, in <module>

[Thu Oct 15 18:15:45 2015] [error]     from _ctypes import Union, Structure, Array

[Thu Oct 15 18:15:45 2015] [error] ImportError: /usr/local/lib/python2.7/lib-dynload/_ctypes.so: cannot enable executable stack as shared object requires: Permission denied

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我想你正在使用像CentOS这样的支持SELinux的发行版。当您的应用使用mod_wsgi支持Apache时,您必须在应用可以访问它们之前将标签httpd_sys_script_exec_t设置为共享对象文件。

在下面的示例中,我将假设您的应用程序位于/var/www/myapp目录中,而{2.7} virtualenv位于/var/www/myapp/venv中,Numpy和Scipy也安装在该virtualenv中。

您可以使用semanage永久添加新的SELinux政策:

semanage fcontext -a -t httpd_sys_script_exec_t "/var/www/myapp/venv/lib/python2.7/site-packages(/.*)?"

然后您可以使用restorecon激活它们:

restorecon -R /var/www/myapp/venv/lib/python2.7/site-packages/

如果在virtualenv中安装了一个包含共享对象文件的新包,则必须再次运行restorecon命令(或重启)。