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

发布于 2024-10-14 02:52:59 字数 1138 浏览 3 评论 0原文

我使用的是 Ubuntu 10.04。
我在 /home/wong2/Code/python/django2/ 下创建一个名为 atest
的 django 项目 并在同一目录中创建一个 wsgi 文件 setting.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()

这是我添加到我的 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>

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

禁止

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

多谢。

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.wsgi in the same directory
Here is the content of 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:

<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

Forbidden

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

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一张白纸 2024-10-21 02:52:59

第二个目录块与您安装 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>

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

您的问题也可能是由于您的主目录的权限限制造成的,因为 Apache 无法看到内部。

请观看:

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

因为它解释了这些权限问题以及代码和 WSGI 脚本文件的粘贴位置等问题。

另请阅读:

http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango< /a>

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:

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.

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

Go watch:

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.

Also read:

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

ゝ偶尔ゞ 2024-10-21 02:52:59

对于 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/

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

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/

空宴 2024-10-21 02:52:59

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

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

ls -l /home/

drwx------ 36 位用户 用户 12288 11 月 28 日 20:56 用户

意思是除了你自己之外没有其他人能够浏览该文件夹。

将执行选项添加到文件夹解决了我的问题

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

drwx-----x 36 位用户 用户 12288 11 月 28 日 20:56 用户

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

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

少女的英雄梦 2024-10-21 02:52:59

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

如果你的 wsgi 在 root 上配置正确,它就不会。

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

edit:

我的项目始终位于

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

这是我的 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()

,我使用 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>

,我将其包含在文件中的站点可用文件夹中,该文件看起来像

<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 是空的。 Apache 在发送静态文件方面也很糟糕,所以我在端口 8080 上设置了 apache,在端口 80 上设置了 nginx,转发到 apache。你应该能够只更改我的站点可用文件上的端口以在没有 nginx 的情况下运行它,但我不会那样运行

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

If you wsgi is configured on root correctly it wont.

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

edit:

My projects are always in

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

Here is my go to wsgi script

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

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

<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

眼睛会笑 2024-10-21 02:52:59

您可能会惊讶地发现您的 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]

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

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

这是(Google)推荐的方法这:

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

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:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文