Target wsgi script will not load properly - python

I am trying to deploy a Django web server with mod_wsgi and apache2 and I am getting an internal server error.
[Sun Mar 29 16:13:00.382493 2015] [mpm_event:notice] [pid 1194:tid 139655348275072] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Sun Mar 29 16:13:00.382514 2015] [core:notice] [pid 1194:tid 139655348275072] AH00094: Command line: '/usr/sbin/apache2'
[Sun Mar 29 20:13:03.159156 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] mod_wsgi (pid=26383): Target WSGI script '/var/www/mbusuite/mbusuite/wsgi.py' cannot be loaded as Python module.
[Sun Mar 29 20:13:03.159199 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] mod_wsgi (pid=26383): Exception occurred processing WSGI script '/var/www/mbusuite/mbusuite/wsgi.py'.
[Sun Mar 29 20:13:03.159221 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] Traceback (most recent call last):
[Sun Mar 29 20:13:03.159258 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] File "/var/www/mbusuite/mbusuite/wsgi.py", line 16, in <module>
[Sun Mar 29 20:13:03.159317 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] application = get_wsgi_application()
[Sun Mar 29 20:13:03.159329 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Mar 29 20:13:03.159368 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] django.setup()
[Sun Mar 29 20:13:03.159379 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 18, in setup
[Sun Mar 29 20:13:03.159415 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] apps.populate(settings.INSTALLED_APPS)
[Sun Mar 29 20:13:03.159427 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
[Sun Mar 29 20:13:03.159563 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] app_config = AppConfig.create(entry)
[Sun Mar 29 20:13:03.159575 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 87, in create
[Sun Mar 29 20:13:03.159654 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] module = import_module(entry)
[Sun Mar 29 20:13:03.159665 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Sun Mar 29 20:13:03.159708 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] __import__(name)
[Sun Mar 29 20:13:03.159739 2015] [:error] [pid 26383:tid 139655211136768] [remote 71.88.97.195:25099] ImportError: No module named storage
My apache config file is:
<VirtualHost *:80>
ServerName mbusuite.duckdns.org
WSGIDaemonProcess mbusuite.duckdns.org python-path=/var/www/mbusuite:/usr/local/lib/python2.7/site-packages
WSGIProcessGroup mbusuite.duckdns.org
WSGIScriptAlias / /var/www/mbusuite/mbusuite/wsgi.py process-group=mbusuite.duckdns.org
ServerAdmin mbu#wpi.edu
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
#<Directory /var/www/mbusuite/static>
# Require all granted
#</Directory>
#<Directory /var/www/mbusuite/core>
# Require all granted
#</Directory>
#<Directory /var/www/mbusuite/templates>
# Require all granted
#</Directory>
<Directory /var/www/mbusuite/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
And the wsgi.py is
"""
WSGI config for mbusuite 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/dev/howto/deployment/wsgi/
"""
import os
import sys
os.environ["DJANGO_SETTINGS_MODULE"] = "mbusuite.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I am not quite sure if this is a permissions issue or a configuration issue. Any help at all would be great.

I don't think you can specify process group on the WSGIScriptAlias directive, and your python-path directive must match what you have in your wsgi.py file (i.e. DJANGO_SETTINGS_MODULE must be importable from the python path you specify). You shouldn't give permissions to the wsgi.py file (ideally you wouldn't have your source under /var/www either since that's usually Apache's docroot area -- and you don't want any possibility of serving your raw source). Something like:
<VirtualHost *:80>
ServerName mbusuite.duckdns.org
WSGIScriptAlias / /var/www/mbusuite/mbusuite/wsgi.py
WSGIProcessGroup mbusuite.duckdns.org
WSGIDaemonProcess mbusuite.duckdns.org \
python-path=/var/www:/usr/local/lib/python2.7/site-packages
ServerAdmin mbu#wpi.edu
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
should work with your current wsgi.py (you might have to declare a docroot too -- you can use it to e.g. serve favicon.ico files ;-)

Related

httpd with django3 with python3.6 on Centos7

we are trying to run django3 (python3.6 on Centos7) with httpd.
i get following error when i run it by HTTPD.
[Tue Jan 12 06:52:04.575618 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] Traceback (most recent call last):
[Tue Jan 12 06:52:04.575665 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/srv/django/django/wsgi.py", line 16, in <module>
[Tue Jan 12 06:52:04.575670 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] application=get_wsgi_application()
[Tue Jan 12 06:52:04.575678 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Jan 12 06:52:04.575682 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] django.setup(set_prefix=False)
[Tue Jan 12 06:52:04.575690 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
[Tue Jan 12 06:52:04.575694 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Tue Jan 12 06:52:04.575701 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 82, in __getattr__
[Tue Jan 12 06:52:04.575706 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] self._setup(name)
[Tue Jan 12 06:52:04.575713 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 69, in _setup
[Tue Jan 12 06:52:04.575717 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] self._wrapped = Settings(settings_module)
[Tue Jan 12 06:52:04.575724 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 170, in __init__
[Tue Jan 12 06:52:04.575729 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] mod = importlib.import_module(self.SETTINGS_MODULE)
[Tue Jan 12 06:52:04.575736 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
[Tue Jan 12 06:52:04.575740 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] return _bootstrap._gcd_import(name[level:], package, level)
[Tue Jan 12 06:52:04.575747 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[Tue Jan 12 06:52:04.575755 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[Tue Jan 12 06:52:04.575762 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
[Tue Jan 12 06:52:04.575780 2021] [wsgi:error] [pid 16778] [remote 100.10.100.10:53489] ModuleNotFoundError: No module named 'django.settings'```
my django.conf file in /etc/httpd/conf.d is as follows:
Alias /static /srv/static
<Directory /srv/static>
Require all granted
</Directory>
<Directory /srv/django/django>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess django python-path=/srv/django:/usr/local/lib/python3.6/site-packages
WSGIProcessGroup django
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias / /srv/django/django/wsgi.py
and my wsgi.py as as follows:
import os
import sys
from django.core.wsgi import get_wsgi_application
os.chdir('/srv/django/')
sys.path.append('/srv/django/')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django.settings')
application=get_wsgi_application()
we installed following packages
python3-devel.x86_64
rh-python36-mod_wsgi.x86_64
yum install httpd-devel
django project as such is working fine
100.10.100.10 (/srv/django/)-1003> python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
January 12, 2021 - 05:22:31
Django version 3.1.5, using settings 'django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
what am i missing?
I am not too familiar on how well Apache Httpd works with WSGI, but if you're starting Django separately and you simply want to a reverse proxy up an running quickly you could do the following:
<VirtualHost *:80>
ServerAdmin admin#localhost
ProxyRequests off
DocumentRoot /var/www
# SSLProxyEngine on
# ProxyPreserveHost On
ServerName www.yoursite.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel error
<Location />
ProxyPass http://www.yourwebsite.net/
ProxyPassReverse http://localhost:8080/
Order allow,deny
Allow from all
</Location>
</VirtualHost>
In terms of the error, it looks like wsgi.py can't find django.settings meaning the django directory and settings module aren't in the current path.

Django WSGI APACHE ModuleNotFoundError: No module named 'django'

its been a nightmare trying to deploy django using apache and wsgi on ubuntu server 18, I created a virtual environment, installed dependencies, restarted apache many times however apache wsgi cant find django in my virtual environment.
I can run manage.py runserver so virtual environment dependencies are ok.
My python version for venv is 3.8
this is apache error log:
[Sun Dec 08 02:09:53.028651 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59496] from django.core.wsgi import get_wsgi_application
[Sun Dec 08 02:09:53.028666 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59496] ModuleNotFoundError: No module named 'django'
[Sun Dec 08 02:09:53.335770 2019] [wsgi:error] [pid 18902:tid 140235648796416] [remote 186.116.79.225:59497] mod_wsgi (pid=18902): Target WSGI script '/var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py' cannot be loaded as Python module.
[Sun Dec 08 02:09:53.335820 2019] [wsgi:error] [pid 18902:tid 140235648796416] [remote 186.116.79.225:59497] mod_wsgi (pid=18902): Exception occurred processing WSGI script '/var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py'.
[Sun Dec 08 02:09:53.335893 2019] [wsgi:error] [pid 18902:tid 140235648796416] [remote 186.116.79.225:59497] Traceback (most recent call last):
[Sun Dec 08 02:09:53.335915 2019] [wsgi:error] [pid 18902:tid 140235648796416] [remote 186.116.79.225:59497] File "/var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py", line 17, in <module>
[Sun Dec 08 02:09:53.335919 2019] [wsgi:error] [pid 18902:tid 140235648796416] [remote 186.116.79.225:59497] from django.core.wsgi import get_wsgi_application
[Sun Dec 08 02:09:53.335933 2019] [wsgi:error] [pid 18902:tid 140235648796416] [remote 186.116.79.225:59497] ModuleNotFoundError: No module named 'django'
[Sun Dec 08 02:09:53.665977 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59498] mod_wsgi (pid=18902): Target WSGI script '/var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py' cannot be loaded as Python module.
[Sun Dec 08 02:09:53.666021 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59498] mod_wsgi (pid=18902): Exception occurred processing WSGI script '/var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py'.
[Sun Dec 08 02:09:53.666097 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59498] Traceback (most recent call last):
[Sun Dec 08 02:09:53.666120 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59498] File "/var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py", line 17, in <module>
[Sun Dec 08 02:09:53.666124 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59498] from django.core.wsgi import get_wsgi_application
[Sun Dec 08 02:09:53.666139 2019] [wsgi:error] [pid 18902:tid 140235766351616] [remote 186.116.79.225:59498] ModuleNotFoundError: No module named 'django'
I don't know what to do,
this is my /etc/apache2/sites-available/000-default.conf file:
<VirtualHost *:80>
Alias /static /var/www/iotconfigserver/IOT_config_rest/static
<Directory /var/www/iotconfigserver/IOT_config_rest/static>
Require all granted
</Directory>
<Directory /var/www/iotconfigserver/IOT_config_rest/IOT_config_rest >
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess IOT_config_rest python-home=/var/www/iotconfigserver/venv python-path=/var/www/iotconfigserver/IOT_config_r$
WSGIProcessGroup IOT_config_rest
WSGIScriptAlias / /var/www/iotconfigserver/IOT_config_rest/IOT_config_rest/wsgi.py
</VirtualHost>
thanks to anyone trying to help
I could deploy by downgrading python venv to 3.6, but I still don't know why

Django on Apache2 (Ubuntu 16.04) reports 'No module named mysite.settings'

I'm running Apache 2.4.18 on Ubuntu 16.04. I've set up a virtual server with the following settings. The virtual host has been registered with a2ensite and appears to be being accessed ok.
<VirtualHost *:80>
ServerName www.factsfromfigures.com
WSGIScriptAlias / /home/user/mycode/mysite/mysite/wsgi.py
<Directory /home/user/mycode/mysite/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
When it runs I get the following error in the Apache log.
[Thu Mar 01 17:35:00.968878 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] mod_wsgi (pid=12278): Target WSGI script '/home/user/mycode/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 01 17:35:00.968895 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] mod_wsgi (pid=12278): Exception occurred processing WSGI script '/home/user/mycode/mysite/mysite/wsgi.py'.
[Thu Mar 01 17:35:00.968923 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] Traceback (most recent call last):
[Thu Mar 01 17:35:00.968933 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/home/user/mycode/mysite/mysite/wsgi.py", line 18, in <module>
[Thu Mar 01 17:35:00.968964 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] application = get_wsgi_application()
[Thu Mar 01 17:35:00.968971 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Mar 01 17:35:00.968988 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] django.setup()
[Thu Mar 01 17:35:00.968992 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 17, in setup
[Thu Mar 01 17:35:00.969007 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Mar 01 17:35:00.969024 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 48, in __getattr__
[Thu Mar 01 17:35:00.969057 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] self._setup(name)
[Thu Mar 01 17:35:00.969062 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 44, in _setup
[Thu Mar 01 17:35:00.969075 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] self._wrapped = Settings(settings_module)
[Thu Mar 01 17:35:00.969079 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 92, in __init__
[Thu Mar 01 17:35:00.969084 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 01 17:35:00.969087 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 01 17:35:00.969103 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] __import__(name)
[Thu Mar 01 17:35:00.969113 2018] [wsgi:error] [pid 12278] [client 192.168.1.68:61552] ImportError: No module named mysite.settings
The mysite settings.py file is at /home/user/mycode/mysite/mysite/settings.py
The wsgi.py file is at /home/user/mycode/mysite/mysite/wsgi.py
I'm not sure what version of python the server is accessing as I get these warning messages in the log: (I've no idea why it reports 2 different version)
[Thu Mar 01 18:32:03.219991 2018] [wsgi:warn] [pid 13753] mod_wsgi: Compiled for Python/2.7.11.
[Thu Mar 01 18:32:03.220008 2018] [wsgi:warn] [pid 13753] mod_wsgi: Runtime using Python/2.7.12.
My history on this is that I've taken a very good Python and Django course. I have 40 years programming experience so don't find coding a problem. I'm relatively new to Linux/Ubuntu and have spent over a week trying to get as little as a 'hello world' appearing on my browser. The documentation is not clear. For example, the official Django documentation recommends putting various settings in httpd.conf but there isn't one. I've tried just about every suggestion on SO into practice but nothing seems to work.
Now I guess this might be bad etiquette but I'm getting desperate and on the verge of giving up on Django. I would really like someone who knows their way around to help me out. I can reward with a suitable number of Amazon vouchers or goodies. I have RealVNC installed so you would be able to log in and have a look as there is nothing sensitive on the server yet. The truth of the matter is that I'm a developer and find the system admin/configuration side of things to be difficult. Thanks for your attention and hopefully you'll help me out.
The path is incomplete at <Directory /home/user/mycode/mysite/>. It should be <Directory /home/user/mycode/mysite/mysite> (note that mysite should occur twice).
Anyway, from an old project, here's what my apache config file looks like:
WSGIScriptAlias / /home/user/mycode/mysite/mysite/wsgi.py
WSGIPythonPath /home/user/mycode/mysite/mysite
<Directory /home/user/mycode/mysite/mysite>
<Files wsgi.py>
Order deny,allow
Require all granted
</Files>
</Directory>

Django and requirements

Im a bit stuck. I can't get this configuration to work and I don't know why. The code I site below is from https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04. The reason I get stuck on it is because he named his project directory and his project the same thing. If I am right, then where he is locating files like the settings.py and wsgi.py should be /home/user/myproject/myproject/myproject but I'm not sure anymore because I can't even get it right myself. Earlier in the document he cd's into the directory he created, which would put him in /home/user/myproject. He then proceeds to create a virtual environment, enter it, and run django-admin startproject myproject. So, if all this holds true, at least what I see on my own server tells me that when you start a django project it actually creates two folders with the same name, nested. Am I wrong? Can someone help me straighten the below code out to make more sense?
. . .
Alias /static /home/user/myproject/static
<Directory /home/user/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject:/home/user/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/wsgi.py
</VirtualHost>
This is what I consistently see in my apache log:
[Sun Oct 09 11:48:15.875313 2016] [wsgi:warn] [pid 47964] mod_wsgi: Compiled for Python/3.5.1+.
[Sun Oct 09 11:48:15.875353 2016] [wsgi:warn] [pid 47964] mod_wsgi: Runtime using Python/3.5.2.
[Sun Oct 09 11:48:15.877537 2016] [mpm_prefork:notice] [pid 47964] AH00163: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[Sun Oct 09 11:48:15.877568 2016] [core:notice] [pid 47964] AH00094: Command line: '/usr/sbin/apache2'
[Sun Oct 09 11:48:18.767800 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod_wsgi (pid=47967): Target WSGI script '/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py' cannot be loaded as Python module.
[Sun Oct 09 11:48:18.767851 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod_wsgi (pid=47967): Exception occurred processing WSGI script '/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py'.
[Sun Oct 09 11:48:18.768339 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] Traceback (most recent call last):
[Sun Oct 09 11:48:18.768385 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/home/addohm/projects/rtservice/servicesite/servicesite/wsgi.py", line 16, in <module>
[Sun Oct 09 11:48:18.768389 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] application = get_wsgi_application()
[Sun Oct 09 11:48:18.768395 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Oct 09 11:48:18.768398 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] django.setup()
[Sun Oct 09 11:48:18.768405 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/__init__.py", line 17, in setup
[Sun Oct 09 11:48:18.768408 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Oct 09 11:48:18.768413 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 48, in __getattr__
[Sun Oct 09 11:48:18.768423 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] self._setup(name)
[Sun Oct 09 11:48:18.768430 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 44, in _setup
[Sun Oct 09 11:48:18.768433 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] self._wrapped = Settings(settings_module)
[Sun Oct 09 11:48:18.768438 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3/dist-packages/django/conf/__init__.py", line 92, in __init__
[Sun Oct 09 11:48:18.768441 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] mod = importlib.import_module(self.SETTINGS_MODULE)
[Sun Oct 09 11:48:18.768446 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Sun Oct 09 11:48:18.768449 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] return _bootstrap._gcd_import(name[level:], package, level)
[Sun Oct 09 11:48:18.768454 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Oct 09 11:48:18.768460 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Oct 09 11:48:18.768466 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Sun Oct 09 11:48:18.768471 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Sun Oct 09 11:48:18.768477 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Sun Oct 09 11:48:18.768483 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Sun Oct 09 11:48:18.768488 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Sun Oct 09 11:48:18.768505 2016] [wsgi:error] [pid 47967] [remote 192.168.2.249:22662] ImportError: No module named 'servicesite'
in your /etc/apache2/apache2.conf file add
<Directory /home/dimitris/mysite>
AllowOverride All
Require all granted
</Directory>
your site.conf file in the example i load mysite with virtualenv
WSGIDaemonProcess myp user=dimitris group=dimitris threads=5 python-path=/home/dimitris/myenv/lib/python2.7/site-packages
<VirtualHost *:80>
#ServerName mysite
#ServerAlias mysite
ServerAdmin webmaster#localhost
WSGIProcessGroup myp
WSGIScriptAlias / /home/dimitris/mysite/mysite/wsgi.py
Alias /favicon.ico /home/dimitris/mysite/favicon.ico
Alias /media/ /home/dimitris/mysite/media/
Alias /static/ /home/dimitris/mysite/project-static/
DocumentRoot /home/dimitris/mysite
<Directory /home/dimitris/mysite/>
Order allow,deny
allow from all
</Directory>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
ErrorLog /var/log/mysite-error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/mysite-access.log combined
</VirtualHost>
in django wsgi.py file add
import os
DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..')
sys.path.append(DJANGO_PATH)
in the above code replace dimitris with your username
Either you create your project at /home/user or change the apache conf file by appending an extra /myproject wherever you are specifying the path as below:
Alias /static /home/user/myproject/myproject/static
<Directory /home/user/myproject/myproject/static>
Require all granted
</Directory>
<Directory /home/user/myproject/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess myproject python-path=/home/user/myproject/myproject:/home/user/myproject/myproject/myprojectenv/lib/python2.7/site-packages
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/myproject/myproject/myproject/wsgi.py
It was confusing because you created your project inside a myproject folder(same as your project name), rather than creating it somewhere else.

Internal Server Error: Target WSGI script cannot be loaded as Python module AND IOError: [Errno 2] No such file or directory: 'client_secrets.json'

I am trying to run a Linux server configuration for a catalog project. I am using: Apache2, Flask, and SQLAlchemy and I had to install and configure PostgreSQL. The site I am trying to run is the Public IP Address: http://52.27.140.219/. I am happy to provide greater detail. I am new with Linux and many things discussed here, but I believe my work is strong, except for the end problem below.
Important steps in Terminal:
From:
(venv)grader#ip-10-20-8-44:/var/www/catalog/catalog$ sudo service apache2 restart
[sudo] password for grader:
* Restarting web server apache2 [ OK ]
Error Logs
(venv)grader#ip-10-20-8-44:/var/www/catalog/catalog$ sudo tail -20 /var/log/apache2/error.log
[Wed Jul 01 16:51:36.873041 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] Traceback (most recent call last):
[Wed Jul 01 16:51:36.873059 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] File "/var/www/catalog/catalog.wsgi", line 7, in <module>
[Wed Jul 01 16:51:36.873105 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] from catalog import app as application
[Wed Jul 01 16:51:36.873117 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] File "/var/www/catalog/catalog/__init__.py", line 35, in <module>
[Wed Jul 01 16:51:36.873257 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] open('client_secrets.json', 'r').read())['web']['client_id']
[Wed Jul 01 16:51:36.873279 2015] [:error] [pid 14600:tid 140294116747008] [client 73.221.39.5:60704] IOError: [Errno 2] No such file or directory: 'client_secrets.json'
[Wed Jul 01 16:53:36.405496 2015] [mpm_event:notice] [pid 14596:tid 140294245513088] AH00491: caught SIGTERM, shutting down
[Wed Jul 01 16:53:37.387879 2015] [mpm_event:notice] [pid 14715:tid 140705447798656] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Wed Jul 01 16:53:37.387939 2015] [core:notice] [pid 14715:tid 140705447798656] AH00094: Command line: '/usr/sbin/apache2'
[Wed Jul 01 17:35:23.312436 2015] [mpm_event:notice] [pid 14715:tid 140705447798656] AH00491: caught SIGTERM, shutting down
[Wed Jul 01 17:35:24.360945 2015] [mpm_event:notice] [pid 15120:tid 140719357745024] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Wed Jul 01 17:35:24.361007 2015] [core:notice] [pid 15120:tid 140719357745024] AH00094: Command line: '/usr/sbin/apache2'
[Wed Jul 01 17:35:35.741239 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] mod_wsgi (pid=15123): Target WSGI script '/var/www/catalog/catalog.wsgi' cannot be loaded as Python module.
[Wed Jul 01 17:35:35.741269 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] mod_wsgi (pid=15123): Exception occurred processing WSGI script '/var/www/catalog/catalog.wsgi'.
[Wed Jul 01 17:35:35.741302 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] Traceback (most recent call last):
[Wed Jul 01 17:35:35.741317 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] File "/var/www/catalog/catalog.wsgi", line 7, in <module>
[Wed Jul 01 17:35:35.741359 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] from catalog import app as application
[Wed Jul 01 17:35:35.741369 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] File "/var/www/catalog/catalog/__init__.py", line 35, in <module>
[Wed Jul 01 17:35:35.741483 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] open('client_secrets.json', 'r').read())['web']['client_id']
[Wed Jul 01 17:35:35.741501 2015] [:error] [pid 15123:tid 140719262549760] [client 73.221.39.5:61118] IOError: [Errno 2] No such file or directory: 'client_secrets.json'
When I sudo nano /etc/apache2/sites-available/catalog.conf, I have:
WSGIPythonPath /var/www/catalog/catalog/venv/:/var/www/catalog/catalog/v$
<VirtualHost *:80>
ServerName 52.27.140.219
ServerAdmin admin#52.27.140.219
ServerAlias c-73-221-39-5.hsd1.wa.comcast.net
WSGIScriptAlias / /var/www/catalog/catalog.wsgi
<Directory /var/www/catalog/catalog/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/catalog/catalog/static
<Directory /var/www/catalog/catalog/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Don't use a relative path name to the file. Instead construct an absolute path name. This is needed because the current working directory of the process will not be where your code is located. Read:
https://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory
So as Graham said, eventually, you end up with something like:
APP_PATH = '/var/www/catalog/
CLIENT_ID = json.loads(open(APP_PATH + 'client_secrets.json', 'r').read())['web']['client_id']

Categories