首页 文章

Apache mod_wsgi在部署Django项目时抛出错误“403 Forbidden”

提问于
浏览
0

我最近在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配置代码片段如下:

#
# 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服务器工作正常 .

2 回答

  • 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曾经将文件覆盖了我曾经设置过的旧权限,我花了一点时间才弄清楚权限已经改变而没有意识到 .
  • 0

    可能是你忘了设置DocumentRoot . 你应该让apache用户可以读取和编写DocumentRoot . 就像这样:

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

    你也可以这样做:

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

相关问题