Apache mod_wsgi 与 conda python 3 - 无法加载模块

Apache mod_wsgi 与 conda python 3 - 无法加载模块

我最近发布了这个问题清除在我的 Amazon ec2 RedHat 7 实例上启动 Flask Web 应用程序的障碍。

简而言之,我尝试使用 Python 3.6 通过 Anaconda 环境启动 Flask Web 应用程序。目前,我有一个简单的 Hello World Flask 应用程序/var/www/html/example,按照找到的说明进行操作这里。我之前能够使用预安装的 Python 2.7.5(可在 参考资料中找到/usr/bin/python)运行此脚本,此后一直尝试使用 Python 3.6 运行相同的应用程序。

我一直在参考多个关于类似问题的帖子,包括,, 和,但我还是有点失落。

我已经通过 conda 环境配置了 mod_wsgi(对于 3.6)。

一些相关的系统信息:

$ which python
~/anaconda3/envs/myenv/bin/python (abspath is /home/ec2-user/anaconda3/envs/myenv/bin/python)
$ python -V
Python 3.6.2 :: Continuum Analytics, Inc.
$ which mod_wsgi-express
~/anaconda3/envs/myenv/bin/mod_wsgi-express
$ mod_wsgi-express module-config
LoadModule wsgi_module "/home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/home/ec2-user/anaconda3/envs/myenv"

文件内容/etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html"
WSGIDaemonProcess myenv python-path=/home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages
WSGIScriptAlias / /var/www/html/examples/example.wsgi process-group=myenv application-group=%{GLOBAL}
LoadModule wsgi_module "/home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome /home/ec2-user/anaconda3/envs/myenv

<Directory example>
    WSGIProcessGroup myenv
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

尝试运行时sudo apachectl restart,我收到以下输出systemctl status httpd.service

httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Sun 2018-04-08 21:41:38 UTC; 3min 7s ago
  Docs: man:httpd(8)
         man:apachectl(8)
  Process: 20541 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=1/FAILURE)
  Process: 20540 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=1/FAILURE)
 Main PID: 20540 (code=exited, status=1/FAILURE)

Apr 08 21:41:38 [ip].compute.internal systemd[1]: Starting The Apache HTTP Server...
Apr 08 21:41:38 [ip].compute.internal httpd[20540]: httpd: Syntax error on line 125 of /etc/httpd/conf/httpd.conf: Cannot load /home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so into server: : /home/ec2-user/anaconda3/envs/myenv/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so: cannot open shared object file: Permission denied

相关文件 ( mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so) 对所有用户都具有可执行权限。

我还注意到正在/var/log/httpd/error_log输出ImportError: No module named site,尽管事实上我并没有尝试site/var/www/html/example.

答案1

解决方案:意识到抛出这个错误是因为我的 Python 安装在我的home目录中。因此,Apache 没有访问该环境的权限。

为了解决这个问题,我做了以下操作:

删除了我安装的 anaconda3:

[ec2-user@ip] ~ $ rm -rf anaconda3/
# -- be sure to remove directories .conda/ and .anaconda_backup/ -- #
# -- these should be in the same directory as the anaconda3/ directory -- #

在目录中以 root 身份重新安装 anaconda3 /usr/local(所有用户都可以访问,与 Apache 一起使用):

[ec2-user@ip] ~ $ sudo su -
[root@ip] ~ $ cd /usr/local
[root@ip] ~ $ wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh 
                  # ^ whichever version you want. Go to https://repo.continuum.io/archive/
[root@ip] ~ $ bash Anaconda3-5.1.0-Linux-x86_64.sh
    # specify your path when prompted --> /usr/local/anaconda3
    # note that directory /usr/local/anaconda3 cannot exist prior to running the command

作为根用户将该行添加export PATH='/usr/local/anaconda3/bin:$PATH'到 /root/.bashrc 文件的末尾! (如果旧的 anaconda3 路径已存在于该文件末尾,请编辑它)

[root@ip] ~ $ vi /root/.bashrc # -- add/edit the line here -- #
[root@ip] ~ $ source /root/.bashrc

验证 root 用户是否使用正确的 Python 版本:

[root@ip] ~ $ which python
/usr/local/anaconda3/bin/python

以 root 用户身份退出:

[root@ip] ~ $ exit

验证正确的 Python 版本是否可以作为默认用户使用:

[ec2-user@ip] ~ $ which python
/usr/local/anaconda3/bin/python

如果打印/usr/bin/python或除 以外的内容/usr/local/anaconda3/bin/python,请确保将我上面指定的 PATH 添加到用户的 ~/.bashrc 文件中(就像您作为 root 用户所做的那样)

注意:所有新安装/环境配置现在必须以 ROOT 身​​份完成。 IE:

[ec2-user@ip] ~ $ sudo su -
[root@ip] ~ $ conda install flask
...
[root@ip] ~ $ exit
# -- open Python to verify that your installation worked -- #
[ec2-user@ip] ~ $ python
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 18:10:19)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
# -- shouldn't throw an ImportError -- #
>>> exit()

如果将来有人偶然发现这一点,请评论!我很乐意提供一些帮助。

祝你好运!

相关内容