Django + Apache + mod_wsgi权限被拒绝

时间:2012-02-21 22:53:55

标签: django apache mod-wsgi

我在Django的网站上完成了关于使用mod_wsgihere)的教程,并且在适当的时候替换了我的路径,导致了很大的“权限被拒绝”。当我尝试访问/。以下是我添加到httpd.conf的内容(先前在conf文件中启用了mod_wsgi):

# 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,则服务器会返回配置错误,尽管它是如何在幻灯片中配置的。

4 个答案:

答案 0 :(得分:16)

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

答案 1 :(得分:6)

答案 2 :(得分:5)

我遇到了同样的问题,如果WSGI应用程序位于已经配置为Apache可访问的任何目录之外,有时会发生这种情况,特别是当它位于您的主目录时,可以指定user = username指令。

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

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

答案 3 :(得分:0)

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

此处引用的项目文件夹是实际django代码的存储位置。 apache可以访问这里的项目公用文件夹引用,其中包含指向相关资源的simlink以及与执行相关的.htaccess和.wsgi文件

套接字前缀可能取决于os和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