I am a beginner at Apache and mod_wsgi. I understand the basic idea of an Apache server though. I need a life savior because I have virtually no one to ask for help.
My problem is as follows:
I can run python manage.py runserver 0.0.0.0:8000 and my application runs fine on the server
However, when I use Apache server to serve my site. Things doesn't work out well.
The two main errors returned are:
ImproperlyConfigured: Error loading psycopg2 module: /opt/payupjack/payupjack-env/lib/python2.7/site-packages/psycopg2/_psycopg.so: wrong ELF class: ELFCLASS32
and
ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
Folder Structure
payupjack
|-payupjack-env (virtualenv for payupjack)
|- (virtualenv stuffs...)
|- PayupJack (My application)
|- All the code required by this application
What I have tried
Changing the user ownership to 'www-data' for the folder django_openid in the virtualenv's site-package folder
Added 'www-data' user to the 'ubuntu' group (so that 'www-data' share same privilege as 'ubuntu')
Running 'wsgi.py' alone, which runs fine.
Running python manage.py runserver which is fine too
Trying to comment out the line which imports utils in the module consumer.py (in the django_openid module), but I still get same error. This is really weird and I don't know how to comprehend it.
Followed many Stackoverflow suggestions on different variant of this problem. Nothing is working so far.
Extra info
The full Apache error log returns the following.
[Tue Nov 10 20:02:12.411960 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
[Tue Nov 10 20:02:12.411987 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] Traceback (most recent call last):
[Tue Nov 10 20:02:12.412002 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
[Tue Nov 10 20:02:12.412091 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] self.load_middleware()
[Tue Nov 10 20:02:12.412105 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
[Tue Nov 10 20:02:12.412209 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] mod = import_module(mw_module)
[Tue Nov 10 20:02:12.412224 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Nov 10 20:02:12.412262 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] __import__(name)
[Tue Nov 10 20:02:12.412272 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django_openid/consumer.py", line 14, in <module>
[Tue Nov 10 20:02:12.412390 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] from django.shortcuts import render_to_response
[Tue Nov 10 20:02:12.412403 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 10, in <module>
[Tue Nov 10 20:02:12.412467 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] from django.db.models.manager import Manager
[Tue Nov 10 20:02:12.412477 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/__init__.py", line 78, in <module>
[Tue Nov 10 20:02:12.412525 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] connection = connections[DEFAULT_DB_ALIAS]
[Tue Nov 10 20:02:12.412535 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
[Tue Nov 10 20:02:12.412595 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] backend = load_backend(db['ENGINE'])
[Tue Nov 10 20:02:12.412615 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/utils.py", line 33, in load_backend
[Tue Nov 10 20:02:12.412630 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] return import_module('.base', backend_name)
[Tue Nov 10 20:02:12.412640 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Nov 10 20:02:12.412652 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] __import__(name)
[Tue Nov 10 20:02:12.412659 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 24, in <module>
[Tue Nov 10 20:02:12.412726 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
[Tue Nov 10 20:02:12.412743 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] ImproperlyConfigured: Error loading psycopg2 module: /opt/payupjack/payupjack-env/lib/python2.7/site-packages/psycopg2/_psycopg.so: wrong ELF class: ELFCLASS32
[Tue Nov 10 15:02:13.107340 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
[Tue Nov 10 15:02:13.107383 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
[Tue Nov 10 15:02:13.107402 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
[Tue Nov 10 15:02:13.107426 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] self.load_middleware()
[Tue Nov 10 15:02:13.107436 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
[Tue Nov 10 15:02:13.107449 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Tue Nov 10 15:02:13.107467 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
[Tue Nov 10 15:02:13.407277 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
[Tue Nov 10 15:02:13.407306 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
[Tue Nov 10 15:02:13.407322 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
[Tue Nov 10 15:02:13.407343 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] self.load_middleware()
[Tue Nov 10 15:02:13.407352 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
[Tue Nov 10 15:02:13.407364 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Tue Nov 10 15:02:13.407381 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
[Tue Nov 10 15:02:14.197060 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
[Tue Nov 10 15:02:14.197098 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
[Tue Nov 10 15:02:14.197114 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
[Tue Nov 10 15:02:14.197136 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] self.load_middleware()
[Tue Nov 10 15:02:14.197144 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
[Tue Nov 10 15:02:14.197157 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Tue Nov 10 15:02:14.197174 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
[Tue Nov 10 15:02:14.252928 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
[Tue Nov 10 15:02:14.252946 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
[Tue Nov 10 15:02:14.252958 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
[Tue Nov 10 15:02:14.252975 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] self.load_middleware()
[Tue Nov 10 15:02:14.252983 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
[Tue Nov 10 15:02:14.252994 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Tue Nov 10 15:02:14.253006 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
The wsgi.py is given as:
from django.core.handlers.wsgi import WSGIHandler
import os
import sys
import site
ALLDIRS = ['/opt/payupjack/payupjack-env/PayupJack']
# redirect sys.stdout to sys.stderr for bad libraries like geopy that uses
# print statements for optional import exceptions.
sys.stdout = sys.stderr
prev_sys_path = list(sys.path)
print prev_sys_path
site.addsitedir('/opt/payupjack/payupjack-env/lib/python2.7/site-packages')
sys.path.append('/opt')
sys.path.append('/opt/payupjack')
sys.path.append('/opt/payupjack/payupjack-env')
sys.path.append('/opt/payupjack/payupjack-env/PayupJack')
# Reorder sys.path so new directories at the front.
new_sys_path = []
for item in list(sys.path):
if item not in prev_sys_path:
new_sys_path.append(item)
sys.path.remove(item)
sys.path[:0] = new_sys_path
activate_this = '/opt/payupjack/payupjack-env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
from os.path import abspath, dirname, join
print sys.path
os.environ["DJANGO_SETTINGS_MODULE"] = "PayupJack.settings"
sys.path.insert(0, join("/opt/payupjack/payupjack-env/PayupJack", "apps"))
import djcelery
djcelery.setup_loader()
print 'Last step of WSGI'
application = WSGIHandler()
print 'Done final step. '
And here is my Apache config file:
WSGIPythonPath /opt/payupjack/payupjack_env/lib/python2.7
<VirtualHost *:80>
ServerAdmin webmaster#localhost
WSGIDaemonProcess payupjack python-path=/opt/payupjack/payupjack-env:/opt/payupjack/payupjack-env/lib/python2.7/site-packages processes=1 threads=1
WSGIProcessGroup payupjack
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIScriptAlias / /opt/payupjack/payupjack-env/PayupJack/wsgi.py
Alias /static/ /opt/payupjack/payupjack-env/PayupJack/static
<Directory /opt/payupjack/payupjack-env/PayupJack>
Order deny,allow
#Allow from all
Require all granted
</Directory>
ErrorLog /opt/payupjack/payupjack-env/logs/error.log
CustomLog /opt/payupjack/payupjack-env/logs/access.log combined
</VirtualHost>
New Info
The ldd of mod_wsgi.so returns:
linux-vdso.so.1 => (0x00007fff815f2000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5b3fec000)
libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007ff5b3a88000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5b36c2000)
/lib64/ld-linux-x86-64.so.2 (0x00007ff5b443c000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ff5b34a9000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5b32a5000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007ff5b30a1000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff5b2d9b000)
I also did a quick find on my whole hard drive and only found one python library that matches the above which is located at:
/usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
The problem was due to a path issue (thanks to Graham pointing out how to check the bitness and location of psycopg2.so). My application uses VirtualEnv. For some reasons, I did not set up VirtualEnv properly the first time. Therefore, all the package that was installed through pip stayed in the OS's python path and not VirtualEnv.
The solution was to remove all the packages installed on the OS's Python path. Then, recreate a new VirtualEnv and do the typical pip install. Make sure that all the packages are installed in the VirtualEnv path. Once I did that, everything worked beautifully.
Related
Strange one - I've tried and tried to get this Flask app going - something seems to be going wrong.
$ pwd inside the Flask app gives:
/home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos
$ ls inside the Flask app gives:
__init__.py __pycache__ vexxos.py windupflaskvexxos.wsgi
__init__.py is empty.
vexxos.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def api():
return 'Wind Up Lord Vexxos API'
if __name__ == "__main__":
app.run(host='0.0.0.0')
windupflaskvexxos.wsgi:
activate_this = '/home/winduplordvexxos/winduplordvexxos.com/winduplordvexxos_env/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
import os, sys
cwd = os.getcwd()
sys.path.insert(0, cwd)
try:
from .vexxos import app as application
except Exception: #ImportError
from vexxos import app as application
However, all I see on the front end is:
/etc/apache2/site-available/000-default.conf:
<VirtualHost *:80>
ServerAlias flask.winduplordvexxos.com
ServerAdmin admin#winduplordvexxos.com
WSGIDaemonProcess windupflaskvexxos.com user=winduplordvexxos group=winduplordvexxos threads=5
WSGIScriptAlias / /home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos/windupflaskvexxos.wsgi
<Directory /home/winduplordvexxos/winduplordvexxos.com/windupflaskvexxos/>
WSGIProcessGroup windupflaskvexxos.com
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>
Checking the apache error logs has given me:
[Fri Oct 13 23:05:14.650945 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] mod_wsgi (pid=15561): Target WSGI script '/home/winduplordve$
[Fri Oct 13 23:05:14.651088 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] mod_wsgi (pid=15561): Exception occurred processing WSGI scr$
[Fri Oct 13 23:05:14.657996 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] Traceback (most recent call last):
[Fri Oct 13 23:05:14.658060 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.658071 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] from .vexxos import app as application
[Fri Oct 13 23:05:14.658098 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] SystemError: Parent module '' not loaded, cannot perform rel$
[Fri Oct 13 23:05:14.658131 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]
[Fri Oct 13 23:05:14.658146 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] During handling of the above exception, another exception oc$
[Fri Oct 13 23:05:14.658156 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859]
[Fri Oct 13 23:05:14.658180 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] Traceback (most recent call last):
[Fri Oct 13 23:05:14.658229 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.658248 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] from vexxos import app as application
[Fri Oct 13 23:05:14.658286 2017] [wsgi:error] [pid 15561:tid 140103054321408] [remote 109.149.163.9:59859] ImportError: No module named 'vexxos'
[Fri Oct 13 23:05:14.690252 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] mod_wsgi (pid=15561): Target WSGI script '/home/winduplordve$
[Fri Oct 13 23:05:14.690318 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] mod_wsgi (pid=15561): Exception occurred processing WSGI scr$
[Fri Oct 13 23:05:14.690468 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] Traceback (most recent call last):
[Fri Oct 13 23:05:14.690515 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.690525 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] from .vexxos import app as application
[Fri Oct 13 23:05:14.690549 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] SystemError: Parent module '' not loaded, cannot perform rel$
[Fri Oct 13 23:05:14.690566 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]
[Fri Oct 13 23:05:14.690573 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] During handling of the above exception, another exception oc$
[Fri Oct 13 23:05:14.690577 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858]
[Fri Oct 13 23:05:14.690589 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] Traceback (most recent call last):
[Fri Oct 13 23:05:14.690610 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] File "/home/winduplordvexxos/winduplordvexxos.com/windupfl$
[Fri Oct 13 23:05:14.690619 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] from vexxos import app as application
[Fri Oct 13 23:05:14.690637 2017] [wsgi:error] [pid 15561:tid 140103087892224] [remote 109.149.163.9:59858] ImportError: No module named 'vexxos'
I've tried a number of things, try except on the module import etc but nothing seems to work...can anyone help with this?
You likely need to tell mod_wsgi where your code is using the python-path option to WSGIDaemonProcess. Your import in the WSGI script file also looks wrong.
For python-path you likely want:
python-path=/home/winduplordvexxos/winduplordvexxos.com
The import should then be:
from windupflaskvexxos import app as application
or if app not pulled in by __init__.py, use:
from windupflaskvexxos.vexxos import app as application
Also read up on the preferred way of setting up a virtual environment with mod_wsgi. The way you are using is not the recommended way.
http://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html
I am attempting to put my Flask application on Elastic Beanstalk (Amazon Web Services, EC2), but after uploading the files I receive a 500 error. Any thoughts on what could be the issue? The error log:
/opt/python/run/venv/bin/python3: can't open file 'mod_wsgi': [Errno 2] No such file or directory
[Fri Sep 08 03:23:05.754341 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] mod_wsgi (pid=13619): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Fri Sep 08 03:23:05.754577 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] mod_wsgi (pid=13619): SystemExit exception raised by WSGI script '/opt/python/current/app/application.py' ignored.
[Fri Sep 08 03:23:05.754729 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] Traceback (most recent call last):
[Fri Sep 08 03:23:05.754992 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/current/app/application.py", line 11, in <module>
[Fri Sep 08 03:23:05.755007 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] application.run(debug=True)
[Fri Sep 08 03:23:05.755116 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/run/venv/lib/python3.4/site-packages/flask/app.py", line 772, in run
[Fri Sep 08 03:23:05.755131 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] run_simple(host, port, self, **options)
[Fri Sep 08 03:23:05.755242 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/serving.py", line 737, in run_simple
[Fri Sep 08 03:23:05.755257 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] reloader_type)
[Fri Sep 08 03:23:05.755344 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/_reloader.py", line 265, in run_with_reloader
[Fri Sep 08 03:23:05.755358 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] sys.exit(reloader.restart_with_reloader())
[Fri Sep 08 03:23:05.755429 2017] [:error] [pid 13619] [remote 128.12.244.5:31824] SystemExit: 2
My file structure is as follows:
file hierarchy
My WSGI path is
config
Turns out I needed to wrap application.run() within a if __name__ == '__main__': block. This ensures that a local WSGI server is not loaded by default.
Source: http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/
Getting the following error with flask on elasticbeanstalk:
/opt/python/run/venv/bin/python3: can't open file 'mod_wsgi': [Errno 2] No such file or directory
[Sat Aug 08 23:43:35.994357 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] mod_wsgi (pid=7748): Target WSGI script '/opt/python/current/app/application.py' cannot be loaded as Python module.
[Sat Aug 08 23:43:35.994407 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] mod_wsgi (pid=7748): SystemExit exception raised by WSGI script '/opt/python/current/app/application.py' ignored.
[Sat Aug 08 23:43:35.994449 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] Traceback (most recent call last):
[Sat Aug 08 23:43:35.994511 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/current/app/application.py", line 3, in <module>
[Sat Aug 08 23:43:35.994518 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] app.run(debug=True)
[Sat Aug 08 23:43:35.994553 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/run/venv/lib/python3.4/site-packages/flask/app.py", line 772, in run
[Sat Aug 08 23:43:35.994560 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] run_simple(host, port, self, **options)
[Sat Aug 08 23:43:35.994590 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/serving.py", line 623, in run_simple
[Sat Aug 08 23:43:35.994596 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] reloader_type)
[Sat Aug 08 23:43:35.994624 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] File "/opt/python/run/venv/lib/python3.4/site-packages/werkzeug/_reloader.py", line 231, in run_with_reloader
[Sat Aug 08 23:43:35.994630 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] sys.exit(reloader.restart_with_reloader())
[Sat Aug 08 23:43:35.994652 2015] [:error] [pid 7748] [remote 127.0.0.1:45363] SystemExit: 2
I'm using elasticbeanstalk command line interface to deploy the flask app.
You're using app.run in production. Don't use the dev server in production.
mod_wsgi, like any WSGI app server, expects to find an application instance when importing, but you're running the dev server instead.
Guard against running the dev server on import. Depending on your mod_wsgi settings, it may expect the name application instead of app as well.
application = app
if __name__ == '__main__':
app.run()
i am trying to run RatticDB Passwordtool in a Docker Container on our VPN Server. Only reachable over a specific Port and VPN.
When i try to reach the Webpanel of my App, i get the following errors from apache.
I am no Python Developer, and thats why i cant figure out, why it cant import the settings from the manage.py
Thats how my manage.py looks
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ratticweb.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
and apache2 error.log >>
[Thu Jul 16 08:26:38.326535 2015] [:error] [pid 10082:tid 140394318771968] [remote 10.150.0.6:29714] % (self.SETTINGS_MODULE, e)
[Thu Jul 16 08:26:38.326560 2015] [:error] [pid 10082:tid 140394318771968] [remote 10.150.0.6:29714] ImportError: Could not import settings 'ratticweb.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named ratticweb.settings
[Thu Jul 16 08:26:39.320769 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] mod_wsgi (pid=10083): Exception occurred processing WSGI script '/opt/apps/ratticweb/wsgi.py'.
[Thu Jul 16 08:26:39.320843 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] Traceback (most recent call last):
[Thu Jul 16 08:26:39.320866 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Thu Jul 16 08:26:39.321025 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] self.load_middleware()
[Thu Jul 16 08:26:39.321044 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[Thu Jul 16 08:26:39.321175 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Thu Jul 16 08:26:39.321192 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Thu Jul 16 08:26:39.321296 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] self._setup(name)
[Thu Jul 16 08:26:39.321312 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Thu Jul 16 08:26:39.321340 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] self._wrapped = Settings(settings_module)
[Thu Jul 16 08:26:39.321354 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Thu Jul 16 08:26:39.321379 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] % (self.SETTINGS_MODULE, e)
[Thu Jul 16 08:26:39.321400 2015] [:error] [pid 10083:tid 140394276775680] [remote 10.150.0.6:10021] ImportError: Could not import settings 'ratticweb.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named ratticweb.settings
[Thu Jul 16 08:39:35.490918 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] mod_wsgi (pid=10083): Exception occurred processing WSGI script '/opt/apps/ratticweb/wsgi.py'.
[Thu Jul 16 08:39:35.491024 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] Traceback (most recent call last):
[Thu Jul 16 08:39:35.491100 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[Thu Jul 16 08:39:35.491271 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] self.load_middleware()
[Thu Jul 16 08:39:35.491294 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[Thu Jul 16 08:39:35.491352 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Thu Jul 16 08:39:35.491386 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[Thu Jul 16 08:39:35.491444 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] self._setup(name)
[Thu Jul 16 08:39:35.491469 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[Thu Jul 16 08:39:35.491506 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] self._wrapped = Settings(settings_module)
[Thu Jul 16 08:39:35.491522 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[Thu Jul 16 08:39:35.491556 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] % (self.SETTINGS_MODULE, e)
[Thu Jul 16 08:39:35.491620 2015] [:error] [pid 10083:tid 140394411173632] [remote 10.150.0.6:31762] ImportError: Could not import settings 'ratticweb.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named ratticweb.settings
Any idea on how to fix this?
I saw serveral Topics with this kind of Problems, but their issues had another source.
if i check the paths ... it seems fine
>>> import sys
>>> for path in sys.path: print path
...
/opt/apps
/opt/apps/manage.py
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
Ideally your project path should be in python sys path, check this out to fix this ImportError: Could not import settings
I am currently trying to publish via ZIP my django app to an AWS elasticbeanstalk.
As I have a local and online development enviroment I would like to make use of the environ variables, that are used anyways by AWS.
For my development environments I am already using virtualenv. Apache runs with wsgi enabled and is supposed to use such environments. Unfortunately, it does not know the environ that I have set in the bin/activate.
Django throws a 500 error, since it cannot connect to the database as it does not have access to the environ vars:
1:53.862779 2015] [wsgi:error] [pid 20639] [remote 127.0.0.1:51896] KeyError: 'RDS_DB_NAME'
If I activate my env and look into the env vars:
(myenv)[bs#debian-gohan:]/var/www/vhosts/mysite $ env | grep RDS
RDS_HOSTNAME=localhost
... and I get the others as well...
My Apache Vhost is:
<VirtualHost *:80>
WSGIDaemonProcess mysite python-path=/home/bs/envs/myenv/bin/python2.7:/home/bs/envs/myenv/lib/python2.7/site-packages
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/vhosts/main-page/mysite/wsgi.py
Does anyone have an idea why the environ vars are not seen by the apache ?
EDIT 1:
I have tried to use SetEnv in the Apache config, like this:
SetEnv RDS_HOSTNAME "localhost"
Unfortunately, this did not work. I then removed the ProcessGroup and only used a simple WSGIPythonPath. That did not work either.
The error in the apache log remains the same:
[Thu Mar 26 11:30:34.046807 2015] [wsgi:info] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012, process='', application='mysite.dbz.dev|'): Loading WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178717 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Target WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 26 11:30:34.178812 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod_wsgi (pid=23012): Exception occurred processing WSGI script '/var/www/vhosts/main-page/mysite/wsgi.py'.
[Thu Mar 26 11:30:34.178924 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] Traceback (most recent call last):
[Thu Mar 26 11:30:34.178947 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/var/www/vhosts/main-page/mysite/wsgi.py", line 17, in <module>
[Thu Mar 26 11:30:34.179111 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] application = get_wsgi_application()
[Thu Mar 26 11:30:34.179157 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Thu Mar 26 11:30:34.179319 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] django.setup()
[Thu Mar 26 11:30:34.179363 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 20, in setup
[Thu Mar 26 11:30:34.179606 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Thu Mar 26 11:30:34.179651 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
[Thu Mar 26 11:30:34.179774 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] self._setup(name)
[Thu Mar 26 11:30:34.179789 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[Thu Mar 26 11:30:34.179918 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] self._wrapped = Settings(settings_module)
[Thu Mar 26 11:30:34.179959 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 94, in __init__
[Thu Mar 26 11:30:34.180072 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] mod = importlib.import_module(self.SETTINGS_MODULE)
[Thu Mar 26 11:30:34.180112 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
[Thu Mar 26 11:30:34.180220 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] __import__(name)
[Thu Mar 26 11:30:34.180246 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/var/www/vhosts/main-page/mysite/../mysite/settings.py", line 94, in <module>
[Thu Mar 26 11:30:34.180400 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] 'NAME': os.environ['RDS_DB_NAME'],
[Thu Mar 26 11:30:34.180443 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
[Thu Mar 26 11:30:34.180613 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] raise KeyError(key)
[Thu Mar 26 11:30:34.180665 2015] [wsgi:error] [pid 23012] [client 127.0.0.1:55231] KeyError: 'RDS_DB_NAME'
Set like this
<VirtualHost hostname:80>
...
SetEnv VARIABLE_NAME variable_value
...
</VirtualHost>
In wsgi script.
os.environ.setdefault("variable", "value")
To override variable,
os.environ['var']="val"