Apache2.4 mod_wsgi error: Forbidden You don't have permission to access / on this server

Posted

技术标签:

【中文标题】Apache2.4 mod_wsgi error: Forbidden You don\'t have permission to access / on this server【英文标题】:Apache2.4 mod_wsgi error: Forbidden You don't have permission to access / on this serverApache2.4 mod_wsgi error: Forbidden You don't have permission to access / on this server 【发布时间】:2017-12-25 08:02:01 【问题描述】:

我使用的是 Ubuntu 14.04。 我在 /opt/cashpro/python_app/cashsite/trunk/cash 下创建了一个 django 项目 并在同一目录下创建一个wsgi文件cash.wsgi

这是 cash.wsgi 的内容

 import os
 import sys

 path = '/opt/cashpro/python_app/cashsite/trunk/'
 sys.path.insert(0, path)
 from cash import settings
 import django.core.management
 sys.path.insert(1, '/opt/cashpro/python_app/cashsite/trunk/cash/')
 django.core.management.setup_environ(settings)
 utility = django.core.management.ManagementUtility()
 command = utility.fetch_command('runserver')
 command.validate()
 import django.conf
 import django.utils
 django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)
 import django.core.handlers.wsgi
 application = django.core.handlers.wsgi.WSGIHandler()

这是我添加到我的 httpd.conf 中的内容:

   <VirtualHost *:443>
    ServerName www.abcd.org
    DocumentRoot /var/www/test
    SSLEngine on
    SSLCipherSuite HIGH:+MEDIUM:!SSLv2:!EXP:!ADH:!aNULL:!eNULL:!NULL
    SSLCertificateFile "/opt/certs/www.abcd.org.crt"
    SSLCertificateKeyFile "/opt/ssl/www.abcd.key"
    SSLCertificateChainFile "/opt/certs/DigiCertCA.crt"
    #SSLCertificateChainFile "/opt/ssl/IntermediatesCA3.crt"
    BrowserMatch ".*MSIE.*" \
             nokeepalive ssl-unclean-shutdown \
             downgrade-1.0 force-response-1.0
    Alias /media-cash/ /opt/cashpro/cashfiles
   <Directory /opt/cashpro/cashfiles>
           Options MultiViews
           AllowOverride None
           Require all granted
    </Directory>
    WSGIPassAuthorization On
    WSGIScriptAlias /site 
   /opt/cashpro/python_app/cashsite/trunk/cash/cash.wsgi
   ErrorLog /var/log/apache2/serror.log
   CustomLog /var/log/apache2/saccess.log common
   </VirtualHost> 

问题是,当我访问 https://www.abcd.org/site 时,它说

禁止

您无权访问此服务器上的 /。

Apache 错误日志

[Thu Jul 20 22:24:56.263573 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340] mod_wsgi (pid=4448): Target WSGI script 
 '/opt/cashpro/python_app/cashsite/trunk/cash/cash.wsgi' cannot be loaded as 
 Python module., referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.263816 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340] mod_wsgi (pid=4448): Exception occurred 
 processing WSGI script 
 '/opt/cashpro/python_app/cashsite/trunk/cash/cash.wsgi'., referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.263978 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340] Traceback (most recent call last):, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264085 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File 
 "/opt/cashpro/python_app/cashsite/trunk/cash/cash.wsgi", line 11, in 
 <module>, referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264223 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     command = 
 utility.fetch_command('runserver'), referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264261 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/core/management/__init__.py", line 261, in fetch_command, 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264325 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     klass = load_command_class(app_name, 
 subcommand), referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264360 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/core/management/__init__.py", line 69, in 
 load_command_class, referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264410 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     module = 
 import_module('%s.management.commands.%s' % (app_name, name)), referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264452 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/utils/importlib.py", line 35, in import_module, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264495 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     __import__(name), referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264524 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/core/management/commands/runserver.py", line 8, in <module>, 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264570 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     from django.core.servers.basehttp import 
 AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application, 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264599 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/core/servers/basehttp.py", line 26, in <module>, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264637 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     from django.views import static, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264665 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/views/static.py", line 95, in <module>, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264700 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     template_translatable = 
 ugettext_noop(u"Index of %(directory)s"), referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264727 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/utils/translation/__init__.py", line 75, in gettext_noop, 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264780 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     return _trans.gettext_noop(message), 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264821 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/utils/translation/__init__.py", line 48, in __getattr__, 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.264852 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     if settings.USE_I18N:, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264879 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/utils/functional.py", line 184, in inner, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264912 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     self._setup(), referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264938 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/conf/__init__.py", line 42, in _setup, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.264987 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     self._wrapped = Settings(settings_module), 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.265015 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/local/lib/python2.7/dist-
 packages/django/conf/__init__.py", line 135, in __init__, referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.265044 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     logging_config_func(self.LOGGING), 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.265071 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/lib/python2.7/logging/config.py", 
 line 794, in dictConfig, referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.265116 2017] [:error] [pid 4448:tid 140570219427584] [ 
 client xxx.xxx.xxx.xxx:3340]     dictConfigClass(config).configure(), 
 referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.265151 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]   File "/usr/lib/python2.7/logging/config.py", 
 line 576, in configure, referer: https://www.abcd.org
 [Thu Jul 20 22:24:56.265201 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340]     '%r: %s' % (name, e)), referer: 
 https://www.abcd.org
 [Thu Jul 20 22:24:56.265284 2017] [:error] [pid 4448:tid 140570219427584] 
 [client xxx.xxx.xxx.xxx:3340] ValueError: Unable to configure handler 
 'default': [Errno 13] Permission denied: '/opt/cash_site.log', referer: 
 https://www.abcd.org

另一个 python web 应用程序托管相同的服务器。但是出现错误

 <VirtualHost *:80>
 ServerAdmin webmaster@localhost
 ServerName www.xyz.com
 ServerAlias xyz.com
 DocumentRoot /opt/nadasnapp/html
 <Directory /opt/nadasnapp/html/>
    Require all granted
 AllowOverride All
    </Directory>
  LogLevel warn
 ##FOR NADA
 Alias /media/ /opt/nadasnapp/python_app/NaDaWebPro/media/
    <Directory /opt/nadasnapp/python_app/NaDaWebPro/media/>
    Require all granted
    AllowOverride All
    </Directory>
    Alias /static/ /opt/nadasnapp/python_app/NaDaWebPro/static_root/
    <Directory /opt/nadasnapp/python_app/NaDaWebPro/static_root/>
    Require all granted
    AllowOverride All
    </Directory>
    #FOR NADA
  WSGIPassAuthorization On
  WSGIScriptAlias / /opt/nadasnapp/python_app/NaDaWebPro/nada/wsgi.py
  <Directory /opt/nadasnapp/python_app/NaDaWebPro/nada>
  <Files wsgi.py>
  Require all granted
  </Files>
  </Directory> 
   </VirtualHost>

错误是

 [Sun Jul 23 04:23:14.151745 2017] [:error] [pid 4594:tid 
139820915422976] 
[client xxx.xxx.xxx.xxx:2244] mod_wsgi (pid=4594): Exception occurred 
processing WSGI script 
 '/opt/nadasnapp/python_app/NaDaWebPro/nada/wsgi.py'.
[Sun Jul 23 04:23:14.151993 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244] Traceback (most recent call last):
[Sun Jul 23 04:23:14.152065 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]   File "/usr/local/lib/python2.7/dist-
packages/django/core/handlers/wsgi.py", line 219, in __call__
[Sun Jul 23 04:23:14.152135 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]     self.load_middleware()
[Sun Jul 23 04:23:14.152166 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]   File "/usr/local/lib/python2.7/dist-
packages/django/core/handlers/base.py", line 39, in load_middleware
 [Sun Jul 23 04:23:14.152203 2017] [:error] [pid 4594:tid 1 
39820915422976] 
[client xxx.xxx.xxx.xxx:2244]     for middleware_path in 
settings.MIDDLEWARE_CLASSES:
[Sun Jul 23 04:23:14.152234 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]   File "/usr/local/lib/python2.7/dist-
packages/django/utils/functional.py", line 184, in inner
[Sun Jul 23 04:23:14.152319 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]     self._setup()
[Sun Jul 23 04:23:14.152357 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]   File "/usr/local/lib/python2.7/dist-
packages/django/conf/__init__.py", line 42, in _setup
[Sun Jul 23 04:23:14.152402 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]     self._wrapped = 
Settings(settings_module)
[Sun Jul 23 04:23:14.152445 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]   File "/usr/local/lib/python2.7/dist-
packages/django/conf/__init__.py", line 95, in __init__
[Sun Jul 23 04:23:14.152484 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244]     raise ImportError("Could not import 
settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Sun Jul 23 04:23:14.152540 2017] [:error] [pid 4594:tid 139820915422976] 
[client xxx.xxx.xxx.xxx:2244] ImportError: Could not import settings 
'cash.settings' (Is it on sys.path?): No module named cash.settings

【问题讨论】:

目录/opt/cashpro/python_app/cashsite/trunk/ 是否属于apache 或者apache 用户是否有足够的权限来访问它? @jgmh 它只有 root 所有权。 请记住 apache 或 httpd.conf 中定义的任何用户都需要该目录的权限。尝试使用 chown -R apache:apache /opt/cashpro/python_app/cashsite/trunk/ 更改该目录的所有权 @jgmh 如您所说更改了所有权。但问题未解决。 你重启/重新加载apache服务了吗? 【参考方案1】:

作为Directory 指令参数的目录是/opt/cashpro/cashfiles,它应该是/opt/cashpro/python_app/cashsite/trunk/cash/。即WSGI脚本文件所在的位置。

【讨论】:

我得到一个类似这样的错误 mod_wsgi (pid=4448): Target WSGI script '/opt/cashpro/python_app/cashsite/trunk/cash/cash.wsgi' 不能作为 Python 模块加载。 ] mod_wsgi (pid=4448): 处理 WSGI 脚本 '/opt/cashpro/python_app/cashsite/trunk/cash/cash.wsgi' 时发生异常 在那之后应该有某种 Python 异常和回溯。它以前如何?它会告诉你代码中有什么问题。猜测你没有告诉 mod_wsgi 你的 Python 虚拟环境。确保您使用的是守护程序模式,但更重要的是阅读modwsgi.readthedocs.io/en/develop/user-guides/… 它在运行 python /opt/cashpro/python_app/cashsite/trunk/cash/manage.py runserver 0.0.0.0:8000 时对我有用。 正如我上面所说,Apache 错误日志中的内容应该比您提供的更多。那条线之后还有什么? 请检查我的问题的错误日志。我已更新。

以上是关于Apache2.4 mod_wsgi error: Forbidden You don't have permission to access / on this server的主要内容,如果未能解决你的问题,请参考以下文章

centos7 + Apache2.4 + python3.5 +mod_wsgi4.5.15 + Django1.10安装指导

Windows10下Apache2.4配置Django

mod_wsgi (3.4-14) / Apache 2.4.12 / Red Hat (6.7) / Django 1.8.2 在负载下挂起

使用 Python 3.6、Apache 2.4 和 Django 1.11 在 Ubuntu 上安装 mod_wsgi

如何在 Windows 上为 apache 2.4 和 python 3.4 安装 mod_wsgi?

在 Django/mod_wsgi 虚拟环境中配置 WSGIPythonHome 的问题