arlyon arlyon - 28 days ago 16
Apache Configuration Question

Django mod_wsgi: ImportError: No module named '$myproject'

So I've been scratching my head for a while trying to figure this one out. I am on Ubuntu 16.04, with 2 different django projects. The intention here is to run both of the side by side on two separate domains on the same server, using two different files in the "sites-(enabled|available)" folder. One is working fine (and has been for a while, this second site is new). The second is not. I'm guessing it has to do with my wsgi settings. To complicate things, I am using lets-encrypt as well. I'm sure that that isn't the issue, but I'll post their confs just to be sure.

Project one is in the dir

/home/arlyon/arlyon
and nr2 is in the dir
/home/arlyon/threeswords
.

000-default.conf

<VirtualHost *:80>

ServerName www.site1.com
ServerAlias site1.com
ServerAdmin mail@me.com
DocumentRoot /home/arlyon/arlyon

WSGIScriptAlias / /home/arlyon/arlyon/arlyon/apache/wsgi.py
WSGIDaemonProcess arlyon python-home=/home/arlyon/arlyon/venv python-path=/home/arlyon/arlyon
WSGIProcessGroup arlyon


Alias /static/ /home/arlyon/arlyon/static/
<Directory /home/arlyon/arlyon/static>
Require all granted
</Directory>

Alias /media/ /home/arlyon/arlyon/media/
<Directory /home/arlyon/arlyon/media>
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.site1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

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

</VirtualHost>


000-default-le-ssl

<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerName www.site1.com
ServerAlias site1.com
ServerAdmin mail@me.com
DocumentRoot /home/arlyon/arlyon

# user/project/projectapp
# project and home dir are the same

WSGIScriptAlias / /home/arlyon/arlyon/arlyon/apache/wsgi.py
Alias /static/ /home/arlyon/arlyon/static/
<Directory /home/arlyon/arlyon/static>
Require all granted
</Directory>

Alias /media/ /home/arlyon/arlyon/media/
<Directory /home/arlyon/arlyon/media>
Require all granted
</Directory>


ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

SSLCertificateFile /etc/letsencrypt/live/www.site1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.site1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>
</IfModule>


site2.conf

<VirtualHost *:80>

ServerName www.site2.com
ServerAlias *.site2.com
ServerAdmin mail@me.com
DocumentRoot /home/arlyon/threeswords

WSGIScriptAlias / /home/arlyon/threeswords/threeswords/wsgi.py
WSGIDaemonProcess threeswords python-path=/home/arlyon/threeswords python-home=/home/arlyon/threeswords/venv
WSGIProcessGroup threeswords

<Directory "/home/arlyon/threeswords/threeswords/">
<Files wsgi.py>
Require all granted
</Files>
</Directory>

Alias /static/ /home/arlyon/threeswords/static/
<Directory /home/arlyon/threeswords/static>
Require all granted
</Directory>

Alias /media/ /home/arlyon/threeswords/media/
<Directory /home/arlyon/threeswords/media>
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/ts_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.site2.no [OR]
RewriteCond %{SERVER_NAME} =*.site2.no
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

</VirtualHost>


threeswords-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerName www.site2.no
ServerAlias *.site2.no
ServerAdmin arlyon@me.com
DocumentRoot /home/arlyon/threeswords

WSGIScriptAlias / /home/arlyon/threeswords/threeswords/wsgi.py

Alias /static/ /home/arlyon/threeswords/static/
<Directory /home/arlyon/threeswords/static>
Require all granted
</Directory>

Alias /media/ /home/arlyon/threeswords/media/
<Directory /home/arlyon/threeswords/media>
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/ts_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteEngine on
RewriteCond %{SERVER_NAME} =www.site2.no
# Some rewrite rules in this file were were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]

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

SSLCertificateFile /etc/letsencrypt/live/www.site1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.site1.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>
</IfModule>


There is some issue with my wsgi config that is causing the server to not be able to import the myproject. However, I have no idea why it is that way. Here is the offending wsgi.py.

from __future__ import absolute_import, unicode_literals
import site
import os

from django.core.wsgi import get_wsgi_application

site.addsitedir('/home/arlyon/threeswords/venv/lib/python3.5/site-packages')
os.environ["DJANGO_SETTINGS_MODULE"] = "threeswords.settings.dev"

application = get_wsgi_application()


The venv is pointing to the right place. When trying to set the settings module, it throws the import error. Lastly here is the error:

[Mon Nov 07 23:27:29.484760 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] mod_wsgi (pid=30535): Target WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py' cannot be loaded as Python module.
[Mon Nov 07 23:27:29.484844 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] mod_wsgi (pid=30535): Exception occurred processing WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py'.
[Mon Nov 07 23:27:29.485206 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] Traceback (most recent call last):
[Mon Nov 07 23:27:29.485321 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/home/arlyon/threeswords/threeswords/wsgi.py", line 19, in <module>
[Mon Nov 07 23:27:29.485349 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] application = get_wsgi_application()
[Mon Nov 07 23:27:29.485369 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Mon Nov 07 23:27:29.485379 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] django.setup()
[Mon Nov 07 23:27:29.485397 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 17, in setup
[Mon Nov 07 23:27:29.485407 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Nov 07 23:27:29.485425 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 55, in __getattr__
[Mon Nov 07 23:27:29.485435 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] self._setup(name)
[Mon Nov 07 23:27:29.485452 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 43, in _setup
[Mon Nov 07 23:27:29.485462 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] self._wrapped = Settings(settings_module)
[Mon Nov 07 23:27:29.485480 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 99, in __init__
[Mon Nov 07 23:27:29.485489 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] mod = importlib.import_module(self.SETTINGS_MODULE)
[Mon Nov 07 23:27:29.485507 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Mon Nov 07 23:27:29.485517 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] return _bootstrap._gcd_import(name[level:], package, level)
[Mon Nov 07 23:27:29.485534 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Nov 07 23:27:29.485554 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Nov 07 23:27:29.485573 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon Nov 07 23:27:29.485592 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon Nov 07 23:27:29.485612 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Nov 07 23:27:29.485631 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Nov 07 23:27:29.485650 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon Nov 07 23:27:29.485669 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon Nov 07 23:27:29.485688 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon Nov 07 23:27:29.485707 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon Nov 07 23:27:29.485736 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Mon Nov 07 23:27:29.485774 2016] [wsgi:error] [pid 30535:tid 140472987141888] [client 92.221.32.65:56377] ImportError: No module named 'threeswords'
[Mon Nov 07 23:27:29.700491 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] mod_wsgi (pid=30535): Target WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py' cannot be loaded as Python module., referer: https://www.site2.no/
[Mon Nov 07 23:27:29.700569 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] mod_wsgi (pid=30535): Exception occurred processing WSGI script '/home/arlyon/threeswords/threeswords/wsgi.py'., referer: https://www.site2.com/
[Mon Nov 07 23:27:29.700934 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] Traceback (most recent call last):, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701050 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/home/arlyon/threeswords/threeswords/wsgi.py", line 19, in <module>, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701061 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] application = get_wsgi_application(), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701081 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701091 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] django.setup(), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701110 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 17, in setup, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701120 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701139 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 55, in __getattr__, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701149 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] self._setup(name), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701183 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 43, in _setup, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701193 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] self._wrapped = Settings(settings_module), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701212 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 99, in __init__, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701222 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] mod = importlib.import_module(self.SETTINGS_MODULE), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701240 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701250 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] return _bootstrap._gcd_import(name[level:], package, level), referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701285 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701306 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701326 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701346 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701366 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701386 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701405 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701425 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701445 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 986, in _gcd_import, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701465 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 969, in _find_and_load, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701484 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked, referer: https://www.site2.com/
[Mon Nov 07 23:27:29.701523 2016] [wsgi:error] [pid 30535:tid 140472978749184] [client 92.221.32.65:56378] ImportError: No module named 'threeswords', referer: https://www.site2.com/


Nothing I have tried so far (my google is a dark sea of purple links) have managed to solve the issue for me. It isn't permissions, it isn't the group. It can be imported into python just fine. I don't understand what is causing it.

Thank you all in advance.

Answer

You are missing WSGIProcessGroup directive in both of your 443 VirtualHost entries. For both of those the requests aren't being delegated to the correct mod_wsgi daemon process group. As the Python module search path is being set up as part of the daemon process group definition, it will not be able to find your application code.