【问题标题】:Apache mod_wsgi throwing error "403 Forbidden" when deploying a Django projectApache mod_wsgi 在部署 Django 项目时抛出错误“403 Forbidden”
【发布时间】:2014-12-19 11:31:40
【问题描述】:

我最近一直在尝试在 CentOS 服务器上使用 mod_wsgi 部署 Django 站点,但到目前为止,当我尝试通过笔记本电脑访问 django 站点时,网页只显示 error: 403 Forbidden You don't have permission to access / on this server.

除了阅读所有明显的文档之外,我还查看了这些以前的问题:

环境:

  • Centos 6.5
  • Python 2.6
  • Django 1.6

我正在运行以下版本的 apache:

# apachectl -V  
Server version: Apache/2.2.15 (Unix)  
Server built:   Apr  3 2014 23:56:16  
Server's Module Magic Number: 20051115:25  
Server loaded:  APR 1.3.9, APR-Util 1.3.9  
Compiled using: APR 1.3.9, APR-Util 1.3.9  
Architecture:   64-bit  
Server MPM:     Prefork  
threaded:     no  
forked:     yes (variable process count)

我使用 Yum 安装了 mod_wsgi 并确认它已安装在服务器上:

# httpd -M | grep wsgi 
wsgi_module (shared)
Syntax OK

我的httpd.conf wsgi config sn -p 如下:

#
# Add WSGI configuration
# 

WSGIScriptAlias / /usr/local/django/basic/basic/apache/wsgi.py
WSGIPythonPath /usr/local/django/basic/
WSGIDaemonProcess ###.###.###.###
WSGIProcessGroup ###.###.###.###

<Directory /usr/local/django/basic/basic/apache>
    <Files wsgi.py>
        Options FollowSymLinks
        Order deny,allow
        Allow from all
    </Files>
</Directory>

最后我的 wsgi.py 脚本是:

"""
WSGI config for basic project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""

import os
import sys

path = "/usr/local/django/basic/basic/apache"
if path not in sys.path:
     sys.path.append(path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "basic.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

错误日志的输出:

[Fri Oct 24 14:10:43 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic  
[Fri Oct 24 14:11:25 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic  
[Fri Oct 24 14:14:02 2014] [error] [client (redacted)] Symbolic link not allowed or link target not accessible: /usr/local/django/basic  

注意事项:

  • django 项目位于我用户的主目录中,但在 `/usr/local/django/ 中有一个符号链接指向它
  • 过去我在项目中工作时,错误 403 通常意味着文件的权限错误,但我已经检查过,文件应该都允许 apache 用户访问它们
  • 当我注释掉 Apache 配置的 wsgi 相关行时,我的 Web 服务器工作正常。

【问题讨论】:

    标签: python linux django apache mod-wsgi


    【解决方案1】:

    抱歉,我发布的有点晚了。这是对我的 apache 配置的最终修复,最终奏效了。

    WSGIScriptAlias /basic /var/www/django/basic/basic/wsgi.py
    WSGIPythonPath /var/www/django/basic/
    
    <Directory /var/www/django/basic/basic>
        Options FollowSymLinks
        <Files wsgi.py>
            Order allow,deny
            Allow from all
        </Files>
    </Directory>
    
    Alias /static /var/www/django/basic/basic/static
    

    这是我在 python 中的 wsgi.py 文件的最终版本。这里的关键代码行是PYTHON_EGG_CACHE。该变量默认设置为不存在的目录。我将它设置为 /tmp/.python-eggs 确保 .python-eggs 具有正确的权限,让 apache 用户可以在任何可以放置此文件的位置读取/写入它。

    """
    WSGI config for basic project.
    
    It exposes the WSGI callable as a module-level variable named ``application``.
    
    For more information on this file, see
    https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
    """
    
    import os
    import sys
    
    path = "/usr/local/django/basic/basic/apache"
        if path not in sys.path:
            sys.path.append(path)
    
    os.environ['PYTHON_EGG_CACHE'] = '/tmp/.python-eggs'
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "basic.settings")
    
    #print os.getenv("DJANGO_SETTINGS_MODULE")
    #print os.getenv("PYTHON_EGG_CACHE")
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    

    旁注:

    • 一个友好的提醒,以确保 django 应用程序中的每个文件都可以被 apache 用户读取(如果需要,也可以写入)。 Git 曾经将一个文件覆盖为我曾经设置过的旧权限,我花了一点时间才发现权限已在不知不觉中发生了变化。

    【讨论】:

      【解决方案2】:

      可能是你忘记设置 DocumentRoot。您应该使 DocumentRoot 可以被 apache 用户读写。 就这样做:

      sudo chown -R www_default:www_default /path/to/you/DocumentRoot
      

      你也可以这样做:

      sudo chmod -R 755 /path/to/your/DocumentRoot
      

      【讨论】:

        猜你喜欢
        • 2020-12-22
        • 2014-02-18
        • 2014-07-31
        • 1970-01-01
        • 1970-01-01
        • 2012-12-19
        • 2021-06-16
        • 2011-07-14
        相关资源
        最近更新 更多