Andrea Citrolo Andrea Citrolo - 3 months ago 205
Apache Configuration Question

Deploy with Apache2, mod_wgi and django 1.9 on ubuntu server 16.04

I am trying to set up a deployment enviroment for my django website on ubuntu server 16.04. When try to connect to the server I get an internal server error message.

The content of my /var/apache2/site-available/000-default.conf is as follows:

<VirtualHost *:80>
ServerAdmin mymailaddress@pippo.com
DocumentRoot /var/www/MySite

Alias /static /var/www/MySite/static
<Directory /var/www/MySite/static>
Require all granted
</Directory>

Alias /media /var/www/MySite/media
<Directory /var/www/MySite/media>
Require all granted
</Directory>

WSGIScriptAlias / /var/www/MySite/MySite/wsgy.py
<Directory /var/www/MySite/MySite>
<Files wsgy.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess MySite python-path=/var/www/MySite:/usr/lib/python3.5/dist-packages
WSGIProcessGroup MySite

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log
</VirtualHost>


the django website is in /var/www/MySite and the folder has the following permissions:

drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 MySite


Inner permissions are as follows:

total 44
drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 .
drwxr-xr-x 4 root root 4096 Aug 29 17:44 ..
drwxr-xr-x 3 root www-data 4096 Aug 30 04:44 MySite
-rw-r--r-- 1 root www-data 0 Aug 29 17:39 __init__.py
-rwxr-xr-x 1 root www-data 251 Aug 29 17:39 manage.py
drwxr-xr-x 3 root www-data 4096 Aug 29 17:39 media
drwxr-xr-x 4 root www-data 4096 Aug 29 18:28 navigation
drwxr-xr-x 5 root www-data 4096 Aug 29 18:28 projects
drwxr-xr-x 4 root www-data 4096 Aug 29 18:28 public_interfaces
drwxr-xr-x 6 root www-data 4096 Aug 29 17:39 static
drwxr-xr-x 3 root www-data 4096 Aug 29 17:39 templates
drwxr-xr-x 4 root www-data 4096 Aug 29 18:28 user_extended


and finally into MySite/Mysite:

total 32
drwxr-xr-x 3 root www-data 4096 Aug 30 04:44 .
drwxr-xr-x 10 root www-data 4096 Aug 29 17:39 ..
-rw-r--r-- 1 root www-data 0 Aug 29 17:39 __init__.py
drwxr-xr-x 2 root www-data 4096 Aug 29 18:38 __pycache__
-rw-r--r-- 1 root www-data 4408 Aug 29 19:12 settings.py
-rw-r--r-- 1 root www-data 1440 Aug 29 17:39 urls.py
-rw-r--r-- 1 root www-data 95 Aug 29 17:39 views.py
-rw-r--r-- 1 root www-data 393 Aug 30 04:44 wsgi.py


The content of the wsgy.py file is the following (default generated from django):

"""
WSGI config for MySite project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MySite.settings")

application = get_wsgi_application()


Finally my settings.py is as follows:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = **************

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['www.mysite.com', 'mysite.com']


# Application definition

INSTALLED_APPS = [
'crispy_forms',
'haystack',
'projects.apps.ProjectsConfig',
'navigation.apps.NavigationConfig',
'public_interfaces.apps.PublicInterfacesConfig',
'user_extended.apps.UserExtendedConfig',
'django.contrib.admin',
'django.contrib.sites',
'registration',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'MySite.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'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 = 'MySite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME':'mysitedb',
'USER': 'citro',
'PASSWORD': *****************,
'HOST': 'localhost',
'PORT': '',
}
}

# Password validation
# https://docs.djangoproject.com/en/1.9/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/1.9/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/1.9/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')

# Email settings
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.email.com'
EMAIL_HOST_USER = 'pippo@email.com'
EMAIL_HOST_PASSWORD = *******
EMAIL_PORT = ********

HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; you may, of course, use a different value.
REGISTRATION_FORM = 'user_extended.forms.MySiteRegistrationForm'

CRISPY_TEMPLATE_PACK = 'bootstrap3'


I am not using virtualenv I installed all the packages with pip3, I am using apache2 version:

Server version: Apache/2.4.18 (Ubuntu)


and libapache2-mod-wsgy-py3, after running sudo apache2ctl restart I get the following errors on the error.log:

[Tue Aug 30 07:11:41.452762 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00494: SIGHUP received. Attempting to restart AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Tue Aug 30 07:11:41.515933 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Aug 30 07:11:41.515946 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Runtime using Python/3.5.2.
[Tue Aug 30 07:11:41.516784 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Aug 30 07:11:41.516816 2016] [core:notice] [pid 4217:tid 140136362780544] AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 30 07:11:41.592253 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Tue Aug 30 07:11:41.592282 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '/usr/local/lib/python3.5/dist-packages'.


when I try to reach the site from the windows host machine (through the broswer) I get an Internal Server Error message and these addictional logs in error.log:

[Tue Aug 30 07:11:41.452762 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00494: SIGHUP received. Attempting to restart AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
[Tue Aug 30 07:11:41.515933 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Aug 30 07:11:41.515946 2016] [wsgi:warn] [pid 4217:tid 140136362780544] mod_wsgi: Runtime using Python/3.5.2.
[Tue Aug 30 07:11:41.516784 2016] [mpm_event:notice] [pid 4217:tid 140136362780544] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Tue Aug 30 07:11:41.516816 2016] [core:notice] [pid 4217:tid 140136362780544] AH00094: Command line: '/usr/sbin/apache2'
[Tue Aug 30 07:11:41.592253 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Tue Aug 30 07:11:41.592282 2016] [wsgi:error] [pid 10223:tid 140136362780544] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '/usr/local/lib/python3.5/dist-packages'.
[Tue Aug 30 07:17:54.414936 2016] [wsgi:error] [pid 10223:tid 140136256911104] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '(null)', stopping.
[Tue Aug 30 07:17:54.414984 2016] [wsgi:error] [pid 10223:tid 140136256911104] mod_wsgi (pid=10223): Call to 'site.addsitedir()' failed for '/usr/local/lib/python3.5/dist-packages'.
[Tue Aug 30 07:17:55.464287 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] mod_wsgi (pid=10223): Target WSGI script '/var/www/MySite/MySite/wsgi.py' cannot be loaded as Python module.
[Tue Aug 30 07:17:55.464350 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] mod_wsgi (pid=10223): Exception occurred processing WSGI script '/var/www/MySite/MySite/wsgi.py'.
[Tue Aug 30 07:17:55.483045 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] Traceback (most recent call last):
[Tue Aug 30 07:17:55.483118 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/var/www/MySite/MySite/wsgi.py", line 16, in <module>
[Tue Aug 30 07:17:55.483123 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] application = get_wsgi_application()
[Tue Aug 30 07:17:55.483129 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Tue Aug 30 07:17:55.483132 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] django.setup()
[Tue Aug 30 07:17:55.483141 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 17, in setup
[Tue Aug 30 07:17:55.483144 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Aug 30 07:17:55.483150 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 55, in __getattr__
[Tue Aug 30 07:17:55.483152 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] self._setup(name)
[Tue Aug 30 07:17:55.483157 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 43, in _setup
[Tue Aug 30 07:17:55.483160 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] self._wrapped = Settings(settings_module)
[Tue Aug 30 07:17:55.483164 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 99, in __init__
[Tue Aug 30 07:17:55.483167 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Aug 30 07:17:55.483181 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Tue Aug 30 07:17:55.483184 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] return _bootstrap._gcd_import(name[level:], package, level)
[Tue Aug 30 07:17:55.483189 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Tue Aug 30 07:17:55.483194 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Tue Aug 30 07:17:55.483199 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Tue Aug 30 07:17:55.483203 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Tue Aug 30 07:17:55.483208 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Tue Aug 30 07:17:55.483213 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Tue Aug 30 07:17:55.483217 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Tue Aug 30 07:17:55.483237 2016] [wsgi:error] [pid 10223:tid 140136256911104] [remote 10.9.11.150:19964] ImportError: No module named 'MySite'


Any suggestion is appreciated. Thanks.

Answer

FWIW. You log files don't quite match what you says your configuration says you are using.

For a start, try dropping /usr/lib/python3.5/dist-packages from python-path option. It shouldn't be required and adding a second path may be triggering an issue under Python 3 that was fixed in mod_wsgi some time back. Ubuntu unfortunately ships a mod_wsgi version from 2 years ago, even in recent Ubuntu versions. They are more than 30 versions behind the latest version. In general it is recommended not to use the mod_wsgi packages supplied by Ubuntu, compile mod_wsgi from source code yourself. The version supplied by Ubuntu is not supported because it is so old.