【问题标题】:Django + Apache + mod_wsgi permission deniedDjango + Apache + mod_wsgi 权限被拒绝
【发布时间】:2012-03-12 07:08:44
【问题描述】:

我在 Django 网站上完成了关于使用 mod_wsgi (here) 的教程,并根据需要替换了我的路径,结果导致“权限被拒绝”。当我尝试访问 /.这是我添加到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'ing 每个相关目录以使其可读,并chmod'ing .wsgi 脚本,我仍然被拒绝权限。如果我将目录路径从/usr/local/django/billing/apache/django.wsgi 更改为截断django.wsgi,服务器会返回一个配置错误,尽管它在幻灯片中是这样配置的。

【问题讨论】:

    标签: django apache mod-wsgi


    【解决方案1】:

    安装flask(在venv 中)并设置WSGISocketPrefix 后,我让它工作了。 我正在部署在 venv 中运行 python3.6 的 centos6.8。

    这里引用的项目文件夹是存放实际django代码的地方。此处引用的项目公共文件夹由 apache 访问,并包含相关资源的 simlinks 以及与执行相关的 .htaccess 和 .wsgi 文件

    套接字前缀可能因操作系统和 apache 配置而异。 如果您遇到可以更改的问题,权限可能会因 apache 版本而异:

    Order allow,deny
     Allow from all
    

    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
    

    【讨论】:

      【解决方案2】:

      相同的配置,相同的环境...但除了在我的一个 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
      

      【讨论】:

      • 这也是我的问题的原因。我不得不关闭 SELinux。您可以通过阅读 /etc/selinux/config 文件来检查当前值。或者运行 getenforce 会显示当前设置。如果要禁用它,请运行 setenforce 0 将其设置为允许。我不确定在保持 SELinux 运行的同时解决此问题的最佳方法是什么。
      • 这是正确答案 我们应该避免关闭 selinux 作为互联网上的正确答案
      【解决方案3】:

      我遇到了同样的问题,如果 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 [完整]

          <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()
      

      【讨论】:

        【解决方案4】:

        【讨论】:

        • 感谢您的链接!直到明天我才能测试幻灯片中的内容,但我一定会返回有用和无效的内容。
        • 查看我上面的编辑:我对这个问题束手无策。我读到的任何东西似乎都反映了我的情况。当我应该在幻灯片 #8 上收到权限被拒绝错误时,我收到了服务器配置错误。打开目录以访问脚本会返回配置错误。我想我要疯了。
        • 成功!使用code.google.com/p/modwsgi/wiki/IntegrationWithDjango,我注意到他们执行了两个 sys.path.append 命令...一个用于包含 django 项目的根文件夹,另一个用于实际项目本身(幻灯片使用)...在根目录中添加文件夹到路径解决了问题!
        猜你喜欢
        • 1970-01-01
        • 2015-05-15
        • 2012-08-13
        • 2014-06-29
        • 1970-01-01
        • 1970-01-01
        • 2018-06-11
        相关资源
        最近更新 更多