简体   繁体   English

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

[英]Apache mod_wsgi throwing error “403 Forbidden” when deploying a Django project

I have been trying to deploy a Django site using mod_wsgi on a CentOS server recently, but so far when I try to access the django site through my laptop, the web page has only been displaying error: 403 Forbidden You don't have permission to access / on this server. 我最近一直在尝试在CentOS服务器上使用mod_wsgi部署Django站点,但是到目前为止,当我尝试通过笔记本电脑访问django站点时,该网页仅显示error: 403 Forbidden You don't have permission to access / on this server.

In addition to reading all the obvious documentation, I have looked at these previous questions: 除了阅读所有显而易见的文档之外,我还研究了以下先前的问题:

Environment: 环境:

  • Centos 6.5
  • Python 2.6
  • Django 1.6

I am running the following version of apache: 我正在运行以下版本的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)

I installed mod_wsgi using Yum and have confirmed it is installed on the server: 我使用Yum安装了mod_wsgi ,并确认它已安装在服务器上:

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

My httpd.conf wsgi config snippet is as follows: 我的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>

Finally my wsgi.py script is: 最后,我的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()

Output from error log: 错误日志的输出:

[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  

Notes: 笔记:

  • The django project is in my user's home directory but has a symbolic link in `/usr/local/django/ pointing to it django项目位于用户的主目录中,但在`/ usr / local / django /中有一个指向它的符号链接
  • In the past when I have worked on projects Error 403 usually meant that the permissions on a file were wrong, but I had check that and the files should all allow the apache user to access them 过去,当我从事项目工作时,错误403通常意味着文件的权限是错误的,但是我检查了一下,并且文件都应允许apache用户访问它们
  • My web server works fine when I comment out the wsgi related lines of the Apache config. 当我注释掉Apache配置中与wsgi相关的行时,我的Web服务器工作正常。

Sorry, that I posted this a bit late. 抱歉,我发布得晚了一点。 This was the final fix to my apache config that ultimately worked. 这是对我的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

And this is the final version of my wsgi.py file in python. 这是python中wsgi.py文件的最终版本。 The key line of code here was the PYTHON_EGG_CACHE . 此处的关键代码行是PYTHON_EGG_CACHE That variable was by default set to a directory that did not exist. 默认情况下,该变量设置为不存在的目录。 I set it to /tmp/.python-eggs Make sure that .python-eggs has correct permissions for the apache user to read/write to it wherever you may place this file. 我将其设置为/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()

Side note: 边注:

  • A friendly reminder to make sure that every file in your django application is readable, (and writeable if needed) by the apache user. 一个友好的提醒,以确保apache用户可以读取django应用程序中的每个文件(并在需要时可写)。 Git once overwrote a file to an old permission I had set up once and it took me a little time to figure out the permissions had changed without realizing it. Git曾经将文件改写为我曾经设置的旧许可,我花了一些时间才知道许可已更改而没有意识到。

May be you forget set DocumentRoot. 可能是您忘记了设置DocumentRoot。 You should make the DocumentRoot can be read and wrote by apache users. 您应该使DocumentRoot可以被apache用户读取和写入。 just do like this: 就是这样:

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

and you can also do like this : 而且您也可以这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM