Python – The issue of running django applications via WSGI on Ubuntu 17

The issue of running django applications via WSGI on Ubuntu 17… here is a solution to the problem.

The issue of running django applications via WSGI on Ubuntu 17

I need your help.
I get an error when trying to run my django project on wsgi.
I’m using Ubuntu 17, Apache 2, Django 2.0, Python 3.6

When I run from manage.py, everything works fine, but when running through wsgi I get the next error:

AH01276: Cannot serve directory /var/cardsite/cardsite/: No matching
DirectoryIndex
(index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found,
and server-generated directory index forbidden by Options directive

Not sure why, because I guess everything is set up correctly. Here is my configuration:

apache.conf

<VirtualHost *:80>
CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log
DocumentRoot /var/cardsite/cardsite/
Alias /static /var/cardsite/cardsite/static/
<Directory /var/cardsite/cardsite/static>
Require all granted
</Directory>
<Directory /var/cardsite/cardsite>
    <Files wsgi.py>
    Require all granted
    </Files>
</Directory>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
WSGIDaemonProcess cardsite python-path=/var/cardsite/ python-home=/var/venv_python36/
WSGIProcessGroup cardsite
WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py
</VirtualHost>

wsgi.py

import os
import sys
PROJECT_DIR = '/var/cardsite'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cardsite.settings")
def execfile(filename):
     globals = dict( __file__ = filename )
     exec( open(filename).read(), globals )

activate_this = os.path.join( '/var/venv_python36/bin', 'activate_this.py' )
execfile( activate_this )
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

P.S. The permissions I grant to the folder and everything inside it.
P.S. also installs packages such as “libapache2-mod-wsgi-py3” or “mod-wsgi” via pip3.

Thank you all for your suggestions

Solution

Do not set DocumentRoot as the parent directory of the source code. If you want to remove WSGIScriptAlias, people can download your source code. You should avoid this risk, even if WSGIScriptAlias currently blocks everything under /. Not allowing access to the DocumentRoot directory can also be part of the problem.

Try:

<VirtualHost *:80>

CustomLog /var/log/apache2/cardsite-access.log common
ErrorLog /var/log/apache2/cardsite-error.log

DocumentRoot /var/cardsite/htdocs

<Directory /var/cardsite/htdocs>
    Require all granted
</Directory>

Alias /static /var/cardsite/cardsite/static

<Directory /var/cardsite/cardsite/static>
    Require all granted
</Directory>

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WSGIDaemonProcess cardsite python-path=/var/cardsite python-home=/var/venv_python36

WSGIProcessGroup cardsite
WSGIApplicationGroup %{GLOBAL}

WSGIScriptAlias / /var/cardsite/cardsite/wsgi.py

<Directory /var/cardsite/cardsite>
    <Files wsgi.py>
    Require all granted
    </Files>
</Directory>

</VirtualHost>

WSGIApplicationGroup has been added, which is always a good idea, with only one WSGI application delegated to the daemon group.

Make sure to create a directory:

/var/cardsite/htdocs

Finally, you are missing a ServerName directive. Therefore, if this is not the default VirtualHost, your request may not even be processed by this VirtualHost.

Related Problems and Solutions