mod_wsgi 无法将目标 WSGI 脚本加载为 Python 模块

mod_wsgi Target WSGI script cannot be loaded as Python module

关注flask的文档

http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/

Apache/2.4.10 烧瓶 0.12.2

目录结构:

├── movie_douban.py
├── movie_douban.wsgi
└── app
    ├── web
    ├── models
    ├── templates
    └── static

movie_douban.wsgi:

activate_this='/root/.local/share/virtualenvs/movie_douban-og6kh8C1/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

import sys
sys.path.insert(0, '/root/.local/share/virtualenvs/movie_douban-og6kh8C1/lib/python3.4/site-packages/')

from movie_douban import app as application

Apache:

<VirtualHost *:80>
    ServerName gdd.python.com

    WSGIDaemonProcess movie_douban user=www-data group=www-data threads=5 
    WSGIScriptAlias / /var/www/movie_douban/movie_douban.wsgi

    ErrorLog "/private/var/log/apache2/python-error_log"
    <Directory /var/www/movie_douban>
        WSGIProcessGroup movie_douban
        WSGIApplicationGroup %{GLOBAL}
        Require all granted
    </Directory>
</VirtualHost>

Apache 错误:

[Sun Apr 29 10:59:56.835279 2018] [wsgi:error] [pid 16942] [remote 172.17.0.1:50790] mod_wsgi (pid=16942): Target WSGI script '/var/www/movie_douban/movie_douban.wsgi' cannot be loaded as Python module.
[Sun Apr 29 10:59:56.835321 2018] [wsgi:error] [pid 16942] [remote 172.17.0.1:50790] mod_wsgi (pid=16942): Exception occurred processing WSGI script '/var/www/movie_douban/movie_douban.wsgi'.
[Sun Apr 29 10:59:56.835470 2018] [wsgi:error] [pid 16942] [remote 172.17.0.1:50790] Traceback (most recent call last):
[Sun Apr 29 10:59:56.835603 2018] [wsgi:error] [pid 16942] [remote 172.17.0.1:50790]   File "/var/www/movie_douban/movie_douban.wsgi", line 2, in <module>
[Sun Apr 29 10:59:56.837206 2018] [wsgi:error] [pid 16942] [remote 172.17.0.1:50790]     with open(activate_this) as file_:
[Sun Apr 29 10:59:56.837265 2018] [wsgi:error] [pid 16942] [remote 172.17.0.1:50790] IOError: [Errno 13] Permission denied: '/root/.local/share/virtualenvs/movie_douban-og6kh8C1/bin/activate_this.py'

root@345ad0ab8386:/var/www/movie_douban# ls -l /root/.local/share/virtualenvs/movie_douban-og6kh8C1/bin/activate_this.py
-rwxrwxrwx 1 root root 1137 Apr 28 03:31 /root/.local/share/virtualenvs/movie_douban-og6kh8C1/bin/activate_this.py

我该怎么办?

Permission denied 错误通常是由于要加载的文件或指向该文件的子目录之一的所有者、组或文件模式设置不正确造成的。另一个可能的原因是 SELinux,但前提是它已启用并处于强制模式。

Permission denied: '/root/.local/share/virtualenvs/movie_douban-og6kh8C1/bin/activate_this.py'

在这种情况下,几乎可以肯定 Apache 进程无法访问 /root 目录(即管理员的主目录),因为它是 运行 在 apache 或 httpd 用户的 UID 下。

您应该将您的 virtualenv 文件移动到更合适的位置。