Issue
I'm using Ubuntu 18.04, and have libapache2-mod-wsgi-py3 and apache2 installed.
I have a Django project located at /home/usr/django_project/ and I created a python 3.8 virtual environment inside the django_project folder. I used pip3 install -r requirments.txt inside the virtual environment to install all the necessary dependencies.
My settings.py is as follows:
import os
import json
with open('/etc/django_personal_website-config.json') as config_file:
config = json.load(config_file)
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config['SECRET_KEY']
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'django_personal_website.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'django_personal_website.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
I created the superuser, migrated files to the static folder, and when I run the server using python3 manage.py runserver, it works fine.
However, I start running into issues with apache. Here's the config file:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.
Alias /static/ /home/webserver/django_personal_website/static/
<Directory /home/webserver/django_personal_website/static>
Require all granted
</Directory>
Alias /static/ /home/webserver/django_personal_website/media/
<Directory /home/webserver/django_personal_website/media>
Require all granted
</Directory>
<Directory /home/webserver/django_personal_website/django_personal_website>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/webserver/django_personal_website/django_personal_website/wsgi.py process-group=django_app
WSGIDaemonProcess django_app python-path=/home/webserver/django_personal_website python-home=/home/webserver/django_personal_website/venv/
WSGIProcessGroup django_app
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
From all the other posts on I've read here and from looking in the docs this seems to be configured correctly, however when I do sudo service apache2 restart I get a 500 Internal Server Error.
When I look at the error log using sudo tail -f /var/log/apache2/error.log I see this:
[Wed Apr 07 19:42:24.993712 2021] [wsgi:error] [pid 9957:tid 140082190063360] [remote 192.168.1.211:52877] Traceback (most recent call last):
[Wed Apr 07 19:42:24.993739 2021] [wsgi:error] [pid 9957:tid 140082190063360] [remote 192.168.1.211:52877] File "/home/webserver/django_personal_website/django_personal_website/wsgi.py", line 12, in <module>
[Wed Apr 07 19:42:24.993742 2021] [wsgi:error] [pid 9957:tid 140082190063360] [remote 192.168.1.211:52877] from django.core.wsgi import get_wsgi_application
[Wed Apr 07 19:42:24.993749 2021] [wsgi:error] [pid 9957:tid 140082190063360] [remote 192.168.1.211:52877] ModuleNotFoundError: No module named 'django'
[Wed Apr 07 19:42:25.109710 2021] [wsgi:error] [pid 9957:tid 140082089293568] [remote 192.168.1.211:52878] mod_wsgi (pid=9957): Target WSGI script '/home/webserver/django_personal_website/django_personal_website/wsgi.py' cannot be loaded as Python module.
[Wed Apr 07 19:42:25.109813 2021] [wsgi:error] [pid 9957:tid 140082089293568] [remote 192.168.1.211:52878] mod_wsgi (pid=9957): Exception occurred processing WSGI script '/home/webserver/django_personal_website/django_personal_website/wsgi.py'.
[Wed Apr 07 19:42:25.109948 2021] [wsgi:error] [pid 9957:tid 140082089293568] [remote 192.168.1.211:52878] Traceback (most recent call last):
[Wed Apr 07 19:42:25.110006 2021] [wsgi:error] [pid 9957:tid 140082089293568] [remote 192.168.1.211:52878] File "/home/webserver/django_personal_website/django_personal_website/wsgi.py", line 12, in <module>
[Wed Apr 07 19:42:25.110018 2021] [wsgi:error] [pid 9957:tid 140082089293568] [remote 192.168.1.211:52878] from django.core.wsgi import get_wsgi_application
[Wed Apr 07 19:42:25.110050 2021] [wsgi:error] [pid 9957:tid 140082089293568] [remote 192.168.1.211:52878] ModuleNotFoundError: No module named 'django'
I'm really stuck here because I believe everything is configured properly in the config file and the WSGIDaemonProcess is pointing to the right spot but it just doesn't seem to see the django folder inside the site-packages folder.
Solution
This issue was fixed by adding
sys.path.append('/home/webserver/django_personal_website')
sys.path.append('/home/webserver/django_personal_website/django_personal_website')
sys.path.append('/home/webserver/django_personal_website/venv/lib/python3.7/site-packages')
to the wsgi.py file.
Answered By - Kolby Wilson Answer Checked By - Clifford M. (WPSolving Volunteer)