urls 路径 Django + Apache + mod_wsgi 的权限被拒绝

Permission denied for urls paths Django + Apache + mod_wsgi

提问人:André Mouro 提问时间:6/14/2023 最后编辑:Nathan WailesAndré Mouro 更新时间:6/20/2023 访问量:50

问:

我已经使用 mod_wsgi 将 Django 应用程序配置为托管在 Apache 服务器中。我已成功打开主页,但无法打开其他 URL。

例如,如果我尝试访问,它会返回 403 禁止错误。/pop/

这是我的代码:

urls.py

from django.contrib import admin
from django.urls import path, include
from dashboard import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.NewView.as_view()),
    path('pop', views.PopView.as_view()),
]

urlpatterns += staticfiles_urlpatterns()

wsgi.py

import os
import sys


from django.core.wsgi import get_wsgi_application


path = '/home/andremou/DashboardUnicamp/unicamp/unicamp'
if path not in sys.path:
    sys.path.append(path)

#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'unicamp.settings')
os.environ["DJANGO_SETTINGS_MODULE"] = "unicamp.settings"

application = get_wsgi_application()

.conf 文件

<VirtualHost *:443>
ServerAdmin [CENSORED]
DocumentRoot [CENSORED]
#ServerName [CENSORED]
ServerName [CENSORED]
#ServerAlias [CENSORED]
ServerAlias [CENSORED]

RewriteEngine On
RewriteCond [CENSORED]
RewriteCond [CENSORED]
RewriteRule [CENSORED]

Alias /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/

# Executar o Django como usuario apache
SuexecUserGroup apache apache
#Header set X-Frame-Options SAMEORIGIN

    <Directory /home/andremou/DashboardUnicamp/unicamp/unicamp> 
        <Files wsgi.py>
            Order deny,allow
            Allow from all
            Require all granted
        </Files>
    </Directory>

# Certificados TLS/SSL
SSLEngine on
SSLCertificateFile /etc/httpd/conf.d/ssl/moodle.pem
SSLCertificateKeyFile /etc/httpd/conf.d/ssl/moodle.key
SSLCertificateChainFile /etc/httpd/conf.d/ssl/moodle_chain.crt

WSGIScriptAlias /dashboardteste /home/andremou/DashboardUnicamp/unicamp/unicamp/wsgi.py
WSGIDaemonProcess unicamp python-home=/home/andremou/DashboardUnicamp/my_env python-path=/home/andremou/DashboardUnicamp/unicamp
WSGIProcessGroup unicamp

WSGIApplicationGroup %{GLOBAL}
</VirtualHost>

我的文件结构:

home
|-andremou
|-DashboardUnicamp
|-my_env
|-unicamp
||-dashboard
| |-migrations
| |-static
| |-templates
| |-models.py
| |-views.py
||-unicamp
| |-settings.py
| |-wsgi.py
| |-urls.py

我尝试配置 wsgi 和 .conf 文件,但没有成功。

Python django mod-wsgi

评论

0赞 Nathan Wailes 6/17/2023
你能上传 403 错误页面的屏幕截图和 PopView 的代码吗?如果更改为 ?path('pop', views.PopView.as_view()),path('pop', views.NewView.as_view()),
0赞 André Mouro 6/19/2023
嗨,内森,我刚刚发现了代码的问题。显然,我的应用程序有两个别名:别名 /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/ WSGIScriptAlias /dashboardteste /home/andremou/DashboardUnicamp/unicamp/unicamp/wsgi.py 我只需要删除第一个别名并让 WSGIScriptAlias。

答:

0赞 Razenstein 6/14/2023 #1

试试这种方式:

<Directory /home/andremou/DashboardUnicamp/unicamp> 
    Require all granted
</Directory>

评论

0赞 André Mouro 6/15/2023
谢谢拉岑斯坦!我已经进行了建议的更改,但仍然无法访问 url 路径。
1赞 André Mouro 6/19/2023 #2

对于遇到此问题的任何人,我已经解决了检查此资源的问题: https://serverfault.com/questions/555955/can-i-configure-apache-mod-wsgi-so-that-the-alias-url-path-is-not-stripped-befor

显然,我的应用程序有两个别名:

Alias /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/

还有这个:

WSGIScriptAlias /dashboardteste /home/andremou/DashboardUnicamp/unicamp/unicamp/wsgi.py

对于 Django 应用程序,正确的别名由 WSGIScriptAlias 管理。

因此,我解决了从我的.conf文件中删除以下行的问题:

Alias /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/