Issue: Internal Server Error with Django and mod_wsgi on Ubuntu Server
Setup:
- Ubuntu 20.04
- Python 3.8 for existing project
- Python 3.12.4 for new project
- Apache
- mod_wsgi installed in respective virtual environments. The system level one has been removed.
Projects:
-
Existing Project (Working )
- Directory:
/var/www/existing-project/
- Python Version: 3.8
- Django Version: 3.x
- Virtual Environment Path:
/var/www/existing-project/venv
- WSGI File Path:
/var/www/existing-project/project/project/wsgi.py
- Apache Configuration:
Protocols h2 http/1.1 WSGIApplicationGroup %{GLOBAL} LoadModule wsgi_module "/var/www/existing-project/venv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi.cpython-38-x86_64-linux-gnu.so" <VirtualHost *:80> ServerAdmin admin@example.com ServerName existing-project.com DocumentRoot /var/www/existing-project/project/ RemoteIPHeader CF-Connecting-IP ErrorLog ${APACHE_LOG_DIR}/existing-project_error.log CustomLog ${APACHE_LOG_DIR}/existing-project_access.log combined <Directory /var/www/existing-project/project/project> <Files wsgi.py> Require all granted </Files> </Directory> Alias /favicon.ico /var/www/existing-project/project/static/images/favicon/favicon.ico Alias /static /var/www/existing-project/project/static <Directory /var/www/existing-project/project/static> Require all granted </Directory> <IfModule mod_expires.c> <FilesMatch "\.(png|jp?g|gif|ico|mp4|wmv|mov|mpeg|css|map|woff?|eot|svg|ttf|js|json|pdf|csv)"> ExpiresActive on ExpiresDefault "access plus 1 year" </FilesMatch> </IfModule> WSGIDaemonProcess existing-project python-home=/var/www/existing-project/venv python-path=/var/www/existing-project/project WSGIProcessGroup existing-project WSGIScriptAlias / /var/www/existing-project/project/project/wsgi.py RewriteEngine on RewriteCond %{SERVER_NAME} =existing-project.com RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
- Directory:
-
New Project (Not working )
- Directory:
/var/www/new-project/
- Python Version: 3.12.4
- Django Version: 5
- Virtual Environment Path:
/var/www/new-project/venv
- WSGI File Path:
/var/www/new-project/project/project/wsgi.py
- Apache Configuration:
WSGIApplicationGroup %{GLOBAL} LoadModule wsgi_module "/var/www/new-project/venv/lib/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so" <VirtualHost *:80> ServerAdmin admin@example.com ServerName new-project.com DocumentRoot /var/www/new-project/project/ RemoteIPHeader CF-Connecting-IP # Basic HTTP Authentication <Directory /var/www/new-project/project> AuthType Basic AuthName "Restricted Area" AuthUserFile /etc/apache2/.htpasswd Require valid-user </Directory> ErrorLog ${APACHE_LOG_DIR}/new-project_error.log CustomLog ${APACHE_LOG_DIR}/new-project_access.log combined <Directory /var/www/new-project/project/project> <Files wsgi.py> Require all granted </Files> </Directory> Alias /favicon.ico /var/www/new-project/project/static/images/favicon/favicon.ico Alias /static /var/www/new-project/project/static <Directory /var/www/new-project/project/static> Require all granted </Directory> <IfModule mod_expires.c> <FilesMatch "\.(png|jp?g|gif|ico|mp4|wmv|mov|mpeg|css|map|woff?|eot|svg|ttf|js|json|pdf|csv)"> ExpiresActive on ExpiresDefault "access plus 1 year" </FilesMatch> </IfModule> WSGIDaemonProcess new-project python-home=/var/www/new-project/venv python-path=/var/www/new-project/project WSGIProcessGroup new-project WSGIScriptAlias / /var/www/new-project/project/project/wsgi.py # RewriteEngine on # RewriteCond %{SERVER_NAME} =new-project.com # RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] </VirtualHost>
- Directory:
Issue:
When accessing new-project.com
, I receive an “Internal Server Error” with the following error logs:
[wsgi:error] [pid 1198771] mod_wsgi (pid=1198771): Exception occurred processing WSGI script '/var/www/new-project/project/project/wsgi.py'.
[wsgi:error] [pid 1198771] Traceback (most recent call last):
File "/var/www/new-project/project/project/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application
ModuleNotFoundError: No module named 'django'
Troubleshooting Steps Taken:
- Confirmed Django installation:
(venv) $ pip show django (venv) $ python -m django --version 5.0.6
- Verified permissions:
sudo chown -R www-data:www-data /var/www/new-project/ sudo chmod -R 755 /var/www/new-project/
- Checked mod_wsgi installation:
(venv) /usr/bin$ mod_wsgi-express module-config LoadModule wsgi_module "/var/www/new-project/venv/lib/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so" WSGIPythonHome "/var/www/new-project/venv" ldd /var/www/new-project/venv/lib/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so
- Verified Python version in WSGI script:
import sys import os print("Python executable:", sys.executable) print("Python version:", sys.version) print("Python path:", sys.path) print("Current working directory:", os.getcwd())
[wsgi:error] [pid 1201444] Python executable: /var/www/new-project/venv/bin/python [wsgi:error] [pid 1201444] Python version: 3.8.10 (default, Mar 25 2024, 10:42:49) [wsgi:error] [pid 1201444] [GCC 9.4.0] [wsgi:error] [pid 1201444] Python path: ['/var/www/new-project/project', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload'] [wsgi:error] [pid 1201444] Current working directory: /
Question:
I’ve been stuck on this for three days and I am out of options to troubleshoot. How can I resolve the “ModuleNotFoundError”? What is happening here?