I have configured a Django application to be hosted in an Apache server using mod_wsgi. I have successfully opened the home page, but haven't been able to open other URLs.
For instance, if I try to access /pop/
, it returns 403 forbidden error.
Here is my code:
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()
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()
<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>
The structure of my files:
home
|-andremou
|-DashboardUnicamp
|-my_env
|-unicamp
||-dashboard
| |-migrations
| |-static
| |-templates
| |-models.py
| |-views.py
||-unicamp
| |-settings.py
| |-wsgi.py
| |-urls.py
I have tried to configure the wsgi and .conf files, but had no success.
For anyone running into this problem I have solved checking this resource: https://serverfault.com/questions/555955/can-i-configure-apache-mod-wsgi-so-that-the-alias-url-path-is-not-stripped-befor
Apparently I had two Alias for my application:
Alias /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/
And this one:
WSGIScriptAlias /dashboardteste /home/andremou/DashboardUnicamp/unicamp/unicamp/wsgi.py
For Django applications the correct Alias is managed by the WSGIScriptAlias.
So, I solved removing the following line from my .conf file:
Alias /dashboardteste/ /home/andremou/DashboardUnicamp/unicamp/unicamp/