I have a EC2 server installed with Apache2, mod_wsgi and Django. The server has both python 2.7 and 3.4 installed which comes with the server os Ubuntu 14.04.
I have the correctly configured virtualenvwrapper to point to Python 3.4 which is the target python versino to use for the webserver.
The problem is when trying to access my django project, the browser displays 500 internal server error. The error log also indicates that is is using Python 2.7 instead of 3.4
The Django project wsgi.py
import sys
import os
import site
site.addsitedir('home/userid/.virtualenvs/myproject/lib/python3.4/site-packages')
sys.path.append('/home/userid/myproject/myproject')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
# Activate your virtual env
activate_env=os.path.expanduser("~/.virtualenvs/myproject/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
#with open(activate_env) as f:
# code = compile (f.read(), activate_env, 'exec')
# exec(code, dict(__file__=activate_env))
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
The /etc/apache2/sites-enabled/000-default.conf:
WSGIScriptAlias /domain1 /home/userid/myproject/myproject/myproject/wsgi.py
WSGIDaemonProcess domain1 user=userid
<Directory /home/userid/myproject/myproject/myproject>
WSGIProcessGroup domain1
Options All
AllowOverride All
Require all granted
</Directory>
Output of: (env1) $ ~/myproject/myproject$ pip3 freeze
Django==1.6.5
djangorestframework==2.3.13
The apache2 error log:
[Tue Jun 17 19:16:34.990478 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] mod_wsgi (pid=789): Exception occurred processing WSGI script '/home/userid/myproject/myproject/myproject/wsgi.py'.
[Tue Jun 17 19:16:34.990567 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] Traceback (most recent call last):
[Tue Jun 17 19:16:34.990601 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 206, in call
[Tue Jun 17 19:16:34.990650 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] response = self.get_response(request)
[Tue Jun 17 19:16:34.990674 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 194, in get_response
[Tue Jun 17 19:16:34.990711 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Tue Jun 17 19:16:34.990734 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 229, in handle_uncaught_exception
[Tue Jun 17 19:16:34.990769 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return debug.technical_500_response(request, *exc_info) [Tue Jun 17 19:16:34.990792 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 69, in technical_500_response
[Tue Jun 17 19:16:34.990827 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] html = reporter.get_traceback_html()
[Tue Jun 17 19:16:34.990850 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 324, in get_traceback_html
[Tue Jun 17 19:16:34.990883 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return t.render(c)
[Tue Jun 17 19:16:34.990906 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 140, in render
[Tue Jun 17 19:16:34.990941 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return self._render(context)
[Tue Jun 17 19:16:34.990963 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 134, in _render
[Tue Jun 17 19:16:34.991018 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return self.nodelist.render(context)
[Tue Jun 17 19:16:34.991042 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 840, in render
[Tue Jun 17 19:16:34.991076 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] bit = self.render_node(node, context)
[Tue Jun 17 19:16:34.991099 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 78, in render_node
[Tue Jun 17 19:16:34.991133 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return node.render(context) [Tue Jun 17 19:16:34.991155 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 88, in render
[Tue Jun 17 19:16:34.991189 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] output = self.filter_expression.resolve(context) [Tue Jun 17 19:16:34.991211 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 613, in resolve
[Tue Jun 17 19:16:34.991245 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] new_obj = func(obj, *arg_vals)
[Tue Jun 17 19:16:34.991267 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 705, in date
[Tue Jun 17 19:16:34.991301 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return format(value, arg)
[Tue Jun 17 19:16:34.991323 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 312, in format
[Tue Jun 17 19:16:34.991357 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return df.format(format_string)
[Tue Jun 17 19:16:34.991380 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format
[Tue Jun 17 19:16:34.991413 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] pieces.append(force_text(getattr(self, piece)())) [Tue Jun 17 19:16:34.991435 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 216, in r
[Tue Jun 17 19:16:34.991469 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return self.format('D, j M Y H:i:s O') [Tue Jun 17 19:16:34.991491 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format
[Tue Jun 17 19:16:34.991523 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] pieces.append(force_text(getattr(self, piece)())) [Tue Jun 17 19:16:34.991546 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 100, in force_text
[Tue Jun 17 19:16:34.991580 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] s = s.__unicode__() [Tue Jun 17 19:16:34.991602 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 138, in __text_cast
[Tue Jun 17 19:16:34.991635 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return func(*self.__args, **self.__kw)
[Tue Jun 17 19:16:34.991658 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 76, in ugettext
[Tue Jun 17 19:16:34.991705 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return _trans.ugettext(message)
[Tue Jun 17 19:16:34.991729 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 281, in ugettext
[Tue Jun 17 19:16:34.991764 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] return do_translate(message, 'ugettext')
[Tue Jun 17 19:16:34.991787 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 263, in do_translate
[Tue Jun 17 19:16:34.991820 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] _default = translation(settings.LANGUAGE_CODE)
[Tue Jun 17 19:16:34.991843 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
[Tue Jun 17 19:16:34.991877 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] default_translation = _fetch(settings.LANGUAGE_CODE) [Tue Jun 17 19:16:34.991899 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
[Tue Jun 17 19:16:34.991932 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] app = import_module(appname)
[Tue Jun 17 19:16:34.991955 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module
[Tue Jun 17 19:16:34.991988 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] /import(name)
[Tue Jun 17 19:16:34.992020 2014] [:error] [pid 789:tid 139997025199872] [remote 205.250.126.130:28049] ImportError: No module named rest_framework
It is my first time trying to set up a server with these tools, so I am not sure where the problem is even after searching through the Internet and looking into different tutorials online.
UPDATE: After setting the WSGIPythonHome (thanks to yuvl) I have found that my mod_wsgi is prbably configured to python 2.7.
[Wed Jun 18 05:08:31.960637 2014] [mpm_event:notice] [pid 1828:tid 140269602740096] AH00491: caught SIGTERM, shutting down
[Wed Jun 18 05:08:33.243412 2014] [mpm_event:notice] [pid 2168:tid 140466175211392] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
[Wed Jun 18 05:08:33.243753 2014] [core:notice] [pid 2168:tid 140466175211392] AH00094: Command line: '/usr/sbin/apache2'
ImportError: No module named site
So I went ahead reinstall the mod_wsgi using
sudo apt-get build-dep libapache2-mod-wsgi
sudo a2enmod wsgi
But now it is saying that apache is not able to load wsgi:
$ sudo /etc/init.d/apache2 restart * Restarting web server apache2 [fail]
* The apache2 configtest failed.
Output of config test was:
apache2: Syntax error on line 140 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/wsgi.load: Cannot load /usr/lib/apache2/modules/mod_wsgi.so into server: /usr/lib/apache2/modules/mod_wsgi.so: cannot open shared object file: No such file or directory
Action 'configtest' failed.
The Apache error log may have more information.
Apache Error Log:
$ tail -f /var/log/apache2/error.log
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
[Wed Jun 18 05:25:14.759140 2014] [mpm_event:notice] [pid 2168:tid 140466175211392] AH00491: caught SIGTERM, shutting down
Is there any suggestion on how I could fix this?
After I post my update, I also tried reinstall mod_wsgi by following this tutorial and this tutorial, but I still get the same failure. Apache does not know about mod_wsgi.
Thank you for the help in advance!
site.addsitedir('home/userid/.virtualenvs/myproject/lib/python3.4/site-packages')
at the beginning of the route try changing it for this line:site.addsitedir('/home/userid/.virtualenvs/myproject/lib/python3.4/site-packages')
– Victor Castillo Torres