Django + Apache + mod_wsgi权限被拒绝

6

我完成了Django官网有关使用mod_wsgi的教程(在这里),并根据需要替换了路径,但当我尝试访问 / 时,结果是一个大大的“权限被拒绝”。下面是我添加到httpd.conf中的内容(mod_wsgi在conf文件的前面已启用):

# Django configuration

WSGIScriptAlias / /usr/local/django/billing/apache/django.wsgi

<Directory /usr/local/django/billing/apache/django.wsgi>
Order allow,deny
Allow from all
</Directory>

AliasMatch ^/([^/]*\.css) /usr/local/wsgi/static/styles/$1

Alias /media/ /usr/local/django/billing/media/
Alias /static/ /usr/local/django/billing/static/

<Directory /usr/local/django/billing/static>
Order deny,allow
Allow from all
</Directory>

<Directory /usr/local/django/billing/media>
Order deny,allow
Allow from all
</Directory>

编辑 #1:

我已经多次查看了幻灯片,从头开始:仍然没有成功。即使打开脚本路径,将每个相关目录的chmod设置为可读,并将.wsgi脚本的chmod设置为可执行,我仍然遇到权限被拒绝的问题。如果我将目录路径从/usr/local/django/billing/apache/django.wsgi更改为截断django.wsgi,服务器返回配置错误,尽管在幻灯片中是这样配置的。

4个回答

19

相同的配置,相同的环境……但是我的django/python程序中一个简单的Popen()调用却无法正常工作。

"权限被拒绝"

结果发现是SELINUX(强制模式)阻止了apache。

您可以通过运行以下命令让SELINUX与您的应用程序兼容:

# semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/your/app(/.*)?'
# restorecon -R -v /path/to/your/app

3
这也是我的问题出现的原因。我不得不关闭SELinux。您可以通过阅读 /etc/selinux/config 文件来检查当前值。或者运行 getenforce 命令显示当前设置。如果您想要禁用它,运行 setenforce 0 命令将其设置为宽容模式。我不确定保持SELinux同时解决此问题的最佳方法。 - m.hashemian
这是正确的答案。我们应该避免关闭SELinux,因为这是互联网上正确的答案。 - José

6

我遇到了相同的问题。有时,如果WSGI应用程序位于未配置为可访问Apache的任何目录之外的位置,特别是当它位于您的主目录时,就会出现这种情况。在这种情况下,最好指定user=username指令。

/etc/apahe2/sites-avaliable/myvhost [部分]

WSGIDaemonProcess localhost python-path=/home/hemanth/ecm:/home/env/lib/python2.7/site-packages  user=hemanth
WSGIProcessGroup localhost

/etc/apahe2/sites-avaliable/myvhost [full]

    <VirtualHost *:80>
            ServerAdmin xy@gmail.om
            ServerName localhost
            ServerAlias localhost

        DocumentRoot /home/hemanth/ecm

        <Directory /home/hemanth/ecm>
            Order allow,deny
            Allow from all
            </Directory>

           WSGIScriptAlias / /home/hemanth/ecm/index.wsgi       
           WSGIDaemonProcess localhost python-path=/home/hemanth/ecm:/home/env/lib/python2.7/site-packages user=hemanth     
           WSGIProcessGroup localhost


           Alias /static/ /home/hemanth/ecm/static/

           Alias /media/ /home/hemanth/ecm/media/   
           <Directory /home/hemanth/ecm/media/>
           Order allow,deny
           Allow from all
           </Directory>

           <Location "/static/">
            Options -Indexes
            </Location>     

           ErrorLog /home/hemanth/ecm/error.log 
</VirtualHost>

index.wsgi

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/hemanth/env/local/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/home/hemanth/ecm')
sys.path.append('/home/hemanth/ecm/ecm')

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

# Activate your virtual env
activate_env="/home/hemanth/env/bin/activate_this.py"
execfile(activate_env, dict(__file__=activate_env))

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

6

谢谢提供链接!我明天才有时间测试幻灯片中的内容,但我一定会回来分享哪些有效,哪些无效。 - patrickn
请看我上面的编辑:我对这个问题已经束手无策了。我读过的所有内容似乎都与我的情况不符。在第8张幻灯片上应该出现权限被拒绝的错误,但我却收到了服务器配置错误的提示。打开目录以访问脚本会返回一个配置错误。我觉得我快要疯了。 - patrickn
1
成功了!使用http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango,我注意到他们执行了两个sys.path.append命令...一个是用于包含Django项目的根文件夹,另一个是用于实际项目本身(幻灯片使用)...将根文件夹添加到路径中解决了这个问题! - patrickn

1
我安装了flask(在虚拟环境中)并设置了WSGISocketPrefix,终于让它工作了。我正在centos6.8上部署,使用虚拟环境中的python3.6。
这里提到的项目文件夹是存储实际django代码的地方。此处引用的项目公共文件夹由apache访问,并包含指向相关资源的符号链接,以及执行所需的.htaccess和.wsgi文件。
套接字前缀可能因操作系统和apache配置而异。如果有问题,权限可能会因apache版本而异,您可以进行更改:
Order allow,deny
 Allow from all

to

Require all granted

这是我的mod_wsgi配置(httpd.conf):
LoadModule wsgi_module **path to venv**/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so

WSGIPythonHome **path to venv**
WSGIDaemonProcess flask user=**user name** group=**user group**
WSGIProcessGroup flask
WSGISocketPrefix /var/run/wsgi 

<Directory **path-to-project-dir**>
     Options ExecCGI MultiViews Indexes
     MultiViewsMatch Handlers

     AddHandler wsgi-script .py
     AddHandler wsgi-script .wsgi

     DirectoryIndex index.html index.php index.py app.wsgi

     Order allow,deny
     Allow from all
</Directory>

这是虚拟主机(httpd.conf)的配置文件。
<VirtualHost *:80>
    DocumentRoot **project public folder**
    ServerName **www.project.com**
    ServerAlias **project.com**

    WSGIScriptAlias / **project public folder**/site.wsgi

    Alias /media/ **project public folder**/media/
    <Directory **project public folder**/media>
        Order allow,deny
        Allow from all
    </Directory>

    Alias /static/ **project public folder**/static/
    <Directory **project public folder**/static>
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

这是site.wsgi文件。
import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('**path to venv**/lib/python3.6/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('**path to project folder containing manage.py**')
sys.path.append('**path to project folder containing settings.py**')

os.environ['DJANGO_SETTINGS_MODULE'] = '**project name**.settings'

# Activate your virtual env
#activate_venv.py is an empty python file which will activate
#the virtual environment when executed. Create it manually
activate_env=os.path.expanduser("**path to venv**/bin/activate_venv.py")
exec(open(activate_env).read(), dict(__file__=activate_env))

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我不得不将以下两行代码从wsgi.py移动到同一文件夹中的“init.py”中,以解决“应用程序未准备好错误”。
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这是虚拟环境中软件包的列表。
Click==7.0
Django==2.2.1
django-debug-toolbar==1.11
django-redis==4.10.0
django-tastypie==0.14.2
Flask==1.0.2
itsdangerous==1.1.0
Jinja2==2.10.1
MarkupSafe==1.1.1
mod-wsgi==4.6.5
mysqlclient==1.4.2.post1
Pillow==6.0.0
pip==19.1.1
python-dateutil==2.8.0
python-mimeparse==1.6.0
pytz==2019.1
redis==3.2.1
setuptools==41.0.1
simplejson==3.16.0
six==1.12.0
sqlparse==0.3.0
Werkzeug==0.15.4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接