Python Apache mod_wsgi 错误:禁止您无权访问 / 在此服务器上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4807176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:37:18  来源:igfitidea点击:

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

pythondjangoapachemod-wsgiwsgi

提问by wong2

I'm using Ubuntu 10.04.
I create a django project under /home/wong2/Code/python/django2/named atest
and create a wsgi file setting.wsgiin the same directory
Here is the content of setting.wsgi:

我正在使用 Ubuntu 10.04。
我在/home/wong2/Code/python/django2/named下创建了一个django项目,atest
setting.wsgi在同一目录下创建了一个wsgi文件,
内容如下setting.wsgi

import os 
import sys

path = '/home/wong2/Code/python/django2'

if path not in sys.path:
    sys.path.append(path)
os.environ["DJANGO_SETTINGS_MODULE"] = "atest.settings" 
from django.core.handlers.wsgi import WSGIHandler 
application = WSGIHandler()

and here is what I add to my my httpd.conf:

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

<VirtualHost *:80>
    ServerName localhost
    WSGIScriptAlias / /home/wong2/Code/python/django2/setting.wsgi
    <Directory />
        Options FollowSymLinks
        AllowOverride None
        Order deny,allow  
        Allow from all 
    </Directory>
    <Directory "/home/wong2/Code/python/django2/atest">
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

The problem is, when I visit http://localhost, it says

问题是,当我访问http://localhost 时,它说

Forbidden

You don't have permission to access / on this server.

禁止的

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

Thanks a lot.

非常感谢。

采纳答案by Graham Dumpleton

The second directory block doesn't match where you have your WSGI script file installed. It is very bad practice though to stick the WSGI script file in a location where source code or other sensitive files exist, ie., same directory or sub directory. Instead you should stick it in a sub directory of its own. Thus:

第二个目录块与您安装 WSGI 脚本文件的位置不匹配。尽管将 WSGI 脚本文件粘贴在源代码或其他敏感文件存在的位置,即相同的目录或子目录中,这是非常糟糕的做法。相反,你应该把它放在它自己的子目录中。因此:

WSGIScriptAlias / /home/wong2/Code/python/django2/atest/apache/setting.wsgi
<Directory "/home/wong2/Code/python/django2/atest/apache">
    Order allow,deny
    Allow from all
</Directory>

So, create 'apache' subdirectory under 'atest'. Move 'setting.wsgi' into that 'apache' subdirectory and change config to above.

因此,在“test”下创建“apache”子目录。将 'setting.wsgi' 移动到那个 'apache' 子目录并将配置更改为上面的。

Your problem also may be caused by restrictive permisions on your home directory as Apache cannot see inside.

您的问题也可能是由于 Apache 无法看到您的主目录中的限制性权限引起的。

Go watch:

去观看:

http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations

http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations

as it explains these permissions problems as well as issues like where to stick your code and the WSGI script file.

因为它解释了这些权限问题以及诸如在哪里粘贴代码和 WSGI 脚本文件之类的问题。

Also read:

另请阅读:

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

回答by Plahcinski

try http://localhost/index.htmldoes that show the apache page?

尝试http://localhost/index.html是否显示了 apache 页面?

If you wsgi is configured on root correctly it wont.

如果您在 root 上正确配置了 wsgi,则不会。

You get that error message because you might be asking the webserver for the directory listing of www folder

您收到该错误消息是因为您可能正在向网络服务器询问 www 文件夹的目录列表

edit:

编辑:

My projects are always in

我的项目总是在

/var/pyproj//pysrc/ and server files i put in /var/py_proj//server/

/var/pyproj//pysrc/ 和服务器文件我放在 /var/py_proj//server/

Here is my go to wsgi script

这是我转到 wsgi 脚本

import os, sys

import logging
logger =logging.getLogger("")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(levelname)-8s %(messages)s')
handler.setFormatter(formatter)
logger.addHandler(handler)

sys.stdout = sys.stderr
from os.path import abspath, dirname, join
import site

PROJECT_ROOT = abspath(join(dirname(__file__), "../pysrc"))
SETTINGS_LOC = abspath(join(PROJECT_ROOT, ''))
site.addsitedir(SETTINGS_LOC)
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"

from django.core.management import setup_environ
import settings
setup_environ(settings)

sys.path.insert(0, join(PROJECT_ROOT, "apps"))

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()

and i use a vhost.conf

我使用 vhost.conf

WSGIDaemonProcess pinax_demo user=www-data group=www-data threads=10 python-path=/var/.virtualenvs/pinax_demo/lib/python2.6/site-packages

<Directory /var/py_proj/pinax_demo/server/>
    Order deny,allow
    Allow from all
</Directory>

Alias /pinax_demo /var/py_proj/pinax_demo/server/pinax_demo.wsgi
<Location /pinax_demo/>
    #WSGIProcessGroup must match the WSGIDaemonProcess
    WSGIProcessGroup pinax_demo
    SetHandler wsgi-script
    Options +ExecCGI
</Location>

which i include in the sites-available folder in a file that looks like this

我将其包含在一个看起来像这样的文件中的 sites-available 文件夹中

<VirtualHost *:8080>
        DocumentRoot /var/www/

        <Directory /var/www/>
                Order deny,allow
                Allow from all
                Options -Indexes FollowSymLinks
                DirectoryIndex index.php
                AllowOverride None
        </Directory>

        ServerAdmin [email protected]

        LogLevel error
        ErrorLog  /var/srv_logs/apache_error.log
        CustomLog  /var/srv_logs/apache_access.log combined
        #insert
        Include /var/py_proj/pinax_demo/server/vhost.conf
        Include /var/py_proj/<other>/server/vhost.conf
        Include /var/py_proj/<other>/server/vhost.conf

</VirtualHost>

httpd.conf is empty. Apache is also terrible at sending static files so i have my apache setup on port 8080 and nginx setup on port 80 which forwards to apache. You should be able to just change the port on my sites-avaiable file to run it without nginx but i would not go live that way

httpd.conf 为空。Apache 在发送静态文件方面也很糟糕,所以我在端口 8080 上设置了 apache,在端口 80 上设置了 nginx,它转发到 apache。您应该能够更改我的站点可用文件上的端口以在没有 nginx 的情况下运行它,但我不会那样做

回答by Viktor

I had the same problem due to restricted file permissions.

由于文件权限受限,我遇到了同样的问题。

By default your home folder user has the settings:

默认情况下,您的主文件夹用户具有以下设置:

ls -l /home/

drwx------ 36 user user 12288 Nov 28 20:56 user

drwx------ 36 user user 12288 Nov 28 20:56 user

meaning that no one else but yourself is able to even browse that folder.

这意味着除了您自己之外,其他任何人都无法浏览该文件夹。

Adding execute option to the folder fixed my problem

向文件夹添加执行选项修复了我的问题

chmod o+x /home/user/
ls -l /home/

drwx-----x 36 user user 12288 Nov 28 20:56 user

drwx-----x 36 用户 用户 12288 十一月 28 日 20:56 用户

回答by user2100427

You may get a surprise where your WSGI has its home directory! On my admittedly naive wsgi_mod installation, it is / - I was so surprised I had to write this script to make sure:

你可能会惊奇地发现你的 WSGI 有它的主目录!在我公认的天真 wsgi_mod 安装中,它是 / - 我很惊讶我不得不编写这个脚本来确保:

import os

def application(environ, start_response):
    status = '200 OK'; e=os.listdir(os.getcwd())
    output = '''WSGI %s<hr>'''%(e)
    response_headers = [('Content-type', 'text/html'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

That means imported applications will need a full path to be found, e.g.:

这意味着导入的应用程序需要找到完整路径,例如:

sys.path.append('/home/foo/www/Forms')
from DataCommon import page

This is the recommended (by Google) approach to this:

这是(由谷歌)推荐的方法:

import sys; path='/home/foo/www/Forms';
if path not in sys.path: sys.path.append(path)

回答by neosergio

With Django 1.5+ you should use the suggested way described in the documentation:

对于 Django 1.5+,您应该使用文档中描述的建议方式:

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/modwsgi/

https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/modwsgi/