0

I'm deploying a django project and getting that 500 error (see Server log error).

Where am I doing wrong?

some notes:

  • centOS
  • mod_wsgi installed
  • same python (2.7) and django (1.9.6) version in both develop and deploy environement
  • using virtualenv in deploy environement

Server log error

[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]  SyntaxError: invalid syntax
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Target WSGI script '/new_esmart/esmart2/esmart2/wsgi.py' cannot be loaded as Python module., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] mod_wsgi (pid=6570): Exception occurred processing WSGI script '/new_esmart/esmart2/esmart2/wsgi.py'., referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93] Traceback (most recent call last):, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart2/esmart2/wsgi.py", line 13, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     import django.core.handlers.wsgi, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/__init__.py", line 1, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     from django.utils.version import get_version, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/version.py", line 7, in <module>, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]     from django.utils.lru_cache import lru_cache, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]   File "/new_esmart/esmart_env/lib/python2.7/site-packages/django/utils/lru_cache.py", line 28, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]      fasttypes = {int, str, frozenset, type(None)},, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]                      ^, referer: http://192.168.30.17/logistics/alarms/
[Wed Sep 21 17:07:54 2016] [error] [client 192.168.30.93]  SyntaxError: invalid syntax, referer: http://192.168.30.17/logistics/alarms/

wsgi.py

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/new_esmart/esmart_env/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/new_esmart/esmart2')
sys.path.append('/new_esmart/esmart2/esmart2')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "esmart2.settings")

# Activate your virtual env
activate_env = os.path.expanduser("/new_esmart/esmart_env/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

application = django.core.handlers.wsgi.WSGIHandler()

httpd.conf

<VirtualHost *:80>
    ServerAdmin blahblah@blah.it
    DocumentRoot /new_esmart/esmart2
    ServerName logistica.org
    ServerAlias www.logistica.org
    WSGIScriptAlias / /new_esmart/esmart2/esmart2/wsgi.py
    ErrorLog logs/logistica.org-error_log
    CustomLog logs/logistica.org-access_log common
</VirtualHost>

WSGIPythonPath /new_esmart/esmart2:/new_esmart/esmart_env/lib/python2.7/site-packages

/etc/httpd/conf.d/wsgi.conf

LoadModule wsgi_module modules/mod_wsgi.so
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Massimo Variolo
  • 4,669
  • 6
  • 38
  • 64
  • Although you have added a 2.7 library directory to the Python path, you are not actually using version 2.7 of Python. There are many questions here (most of them answered by the author of mod_wsgi) that tell you how to point it to a different version. – Daniel Roseman Sep 21 '16 at 15:37
  • Unrelated to your current problem, but in newer Django versions you need to [use `get_wsgi_application()`](https://docs.djangoproject.com/en/1.10/releases/1.7/#wsgi-scripts) instead of `WSGIHandler()`. – knbk Sep 21 '16 at 16:12
  • @DanielRoseman on my server I have the same Python version (2.7.8) in both env and virtualenv – Massimo Variolo Sep 21 '16 at 16:15
  • I don't know what "env" is in this context but it doesn't matter; your mod_wsgi is clearly running against Python 2.6. – Daniel Roseman Sep 21 '16 at 16:28
  • @DanielRoseman I'll explain better: simply typing 'python' opens python 2.7 shell. The same after activating virtualenv. So what is the evidence of mod_wsgi running against Python 2.6? – Massimo Variolo Sep 21 '16 at 21:29
  • 2
    Because the set literal syntax highlighted in the error is invalid in Python 2.6 but valid in 2.7. – Daniel Roseman Sep 21 '16 at 21:58

2 Answers2

1

As pointed out by Daniel, the error indicates your mod_wsgi is compiled for Python 2.6. In order to use Python 2.7 you will need to have mod_wsgi installed with it compiled against Python 2.7. You cannot try and force it to use Python 2.7 by simply referring to your Python 2.7 virtual environment, that isn't how it works.

You can verify what Python version mod_wsgi was compiled with using test application described in:

You will need to uninstall mod_wsgi module and install a version of it built for Python 2.7, either from system packages if available, or built from source code if no system packages available for mod_wsgi for Python 2.7.

I would also recommend that you check the Django documentation on using mod_wsgi and ensure that you use daemon mode as it explains.

Just be aware that the Django documentation still doesn't follow all best practices. Rather than add the site-packages explicitly in python-path, you should use python-home to refer to the virtual environment. See:

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
1

You're not using the right python version, but you can specify which one to use in your apache conf by using the WSGIPythonHome directive.

Add

WSGIPythonHome /path/to/your/virtualenv

to your Apache configuration,

This way you can use the interpreter from your virtualenv.

EDIT :

Since you may want to define the python home specifically to your VirtualHost (WSGIPythonHome can't be used in a VirtualHost scope), you could use the WSGIDaemonProcess directive :

<VirtualHost *:80>
  ServerName example.com
  [...]

  WSGIDaemonProcess example.com python-home=/path/to/venv python-path=<python-path>
  WSGIProcessGroup example.com

</VirtualHost>
vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • 1
    No that isn't strictly correct. You can only point at a Python installation which is the same X.Y version and then it should still be an installation or virtual environment corresponding to, or related to, the specific Python shared library that mod_wsgi is linked to. You cannot force a mod_wsgi compiled for and linked with Python 2.6, to use a Python 2.7 installation for example. – Graham Dumpleton Sep 22 '16 at 01:53
  • @GrahamDumpleton Not that I'm doubting, but I'm actually testing it, I never thought of that since I never had to face this problem. :) – vmonteco Sep 22 '16 at 02:32