Search code examples
djangoapachepython-3.xmod-wsgidjango-settings

Deploying Django app on Ubuntu 16.04 apache2 with mod_wsgi


I am trying to deploy my django app to Ubuntu 16.04 running Apache with mod_wdgi. I have the apache server setup correctly (i think) but I am running into a issue when apache trying to run my "prod_wgsi.py" script, it is trying to import the wrong settings module.

I am able to start the python interpreter with "python -i prod_wsgi.py" load my settings file and run django.setup().

File structure

NOCduncan

   website
     ___init__.py
     manage.py
     prod_wsgi.py
     website
        settings
           ___init__.py
           dev.py
           prod.py
           base.py
        templates
        ___init__.py

prod_wsgi.py

import os
from django.core.wsgi import get_wsgi_application
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/biffduncan/.virtualenvs/nocduncanenv/lib/python3.5/site-packages')

# Add the app's directory to the PYTHONPATH
paths = [
    '/home/biffduncan/opt/NOCduncan',
    '/home/biffduncan/opt/NOCduncan/website/',
    '/var/www/nocduncan/website',
    '/var/www/nocduncan',
]

for path in paths:
    if path not in sys.path:
        sys.path.append(path)

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings.prod')

# Activate your virtual env
activate_env=os.path.expanduser('/home/biffduncan/.virtualenvs/nocduncanenv/bin/activate_this.py')
exec(open(activate_env).read())

application = get_wsgi_application()

prod.py

from website.settings.base import *


WSGI_APPLICATION = 'prod_wsgi.application'

Apache config

<VirtualHost *:80>
    #My site Name
    ServerName noc.biffduncan.com

    #Demon process for multiple virtual hosts
    WSGIDaemonProcess noc.biffduncan.com threads=5

    #Pointing wsgi script to config file
    WSGIScriptAlias / /var/www/nocduncan/prod_wsgi.py
    WSGIProcessGroup noc.biffduncan.com

    #Your static files location
    Alias /static/ "/var/www/nocduncan/website/static"
    <Location "/static/">
        Options -Indexes
    </Location>
</VirtualHost>

Apache Error

[Wed Oct 18 20:57:08.018901 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] mod_wsgi (pid=21696): Target WSGI script '/var/www/nocduncan/prod_wsgi.py' cannot be loaded as Python module.
[Wed Oct 18 20:57:08.019036 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] mod_wsgi (pid=21696): Exception occurred processing WSGI script '/var/www/nocduncan/prod_wsgi.py'.
[Wed Oct 18 20:57:08.020480 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] Traceback (most recent call last):
[Wed Oct 18 20:57:08.020709 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/var/www/nocduncan/prod_wsgi.py", line 27, in <module>
[Wed Oct 18 20:57:08.020741 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     application = get_wsgi_application()
[Wed Oct 18 20:57:08.020765 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Wed Oct 18 20:57:08.020866 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     django.setup(set_prefix=False)
[Wed Oct 18 20:57:08.020957 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 22, in setup
[Wed Oct 18 20:57:08.021004 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Wed Oct 18 20:57:08.021032 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 56, in __getattr__
[Wed Oct 18 20:57:08.021046 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     self._setup(name)
[Wed Oct 18 20:57:08.021064 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 41, in _setup
[Wed Oct 18 20:57:08.021073 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     self._wrapped = Settings(settings_module)
[Wed Oct 18 20:57:08.021089 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 110, in __init__
[Wed Oct 18 20:57:08.021098 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Wed Oct 18 20:57:08.021113 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Wed Oct 18 20:57:08.021122 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     return _bootstrap._gcd_import(name[level:], package, level)
[Wed Oct 18 20:57:08.021137 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Wed Oct 18 20:57:08.021194 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Wed Oct 18 20:57:08.021247 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Wed Oct 18 20:57:08.021304 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Wed Oct 18 20:57:08.021335 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Wed Oct 18 20:57:08.021355 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Wed Oct 18 20:57:08.021373 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Wed Oct 18 20:57:08.021413 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] ImportError: No module named 'website.settings'

From that log it looks like its using the wrong python environment variable (its using the system python3.5 not the virtual environment python3.5) so i think the virtual environment is not being executed correctly.


Solution

  • Going off of Graham's comment:

    Changed this:

    # Add the app's directory to the PYTHONPATH
    paths = [
        '/home/biffduncan/opt/NOCduncan',
        '/home/biffduncan/opt/NOCduncan/website/',
        '/var/www/nocduncan/website',
        '/var/www/nocduncan',
    ]
    

    To this:

    # Add the app's directory to the PYTHONPATH
    paths = [
        '/var/www/nocduncan/website',
        '/var/www/nocduncan',
    ]