I am struggling in making Django run with mod_wsgi. I tried to follow recommendations here but it did not work.
The server is a Debian :
$ lsb_release -da
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.3 (jessie)
Release: 8.3
Codename: jessie
Python 3.5.1 has been installed with --ensable-shared. The virtualenv venv uses this Python because of the following in the user .bash_profile :
LD_LIBRARY_PATH=/usr/local/lib
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.5
WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
libapache2-mod-wsgi-py3 has been installed, as it is explained, it works for Python3.X. So why do I have a "No module named 'psycopg2._psycopg'" error here ?
mod_wsgi has been installed in the virtualenv venv.
Here is the virtualhost:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias mysite.com
ServerAdmin webmaster@mysite.com
DocumentRoot /var/www/mysite.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIDaemonProcess api python-path=/var/www/mysite.com:/home/user/.virtualenvs/venv/lib/python3.5/site-packages
WSGIProcessGroup api
WSGIScriptAlias / /var/www/mysite.com/wsgi.py
<Directory /var/www/mysite.com>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
Here is the content of my apache error.log file:
mod_wsgi: Compiled for Python/3.4.2rc1+.
mod_wsgi: Runtime using Python/3.4.2.
AH00489: Apache/2.4.10 (Debian) mod_wsgi/4.3.0 Python/3.4.2 configured -- resuming normal operations
AH00094: Command line: '/usr/sbin/apache2'
mod_wsgi (pid=12796): Target WSGI script '/var/www/mysite.com/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=12796): Exception occurred processing WSGI script '/var/www/mysite.com/wsgi.py'.
Traceback (most recent call last):
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: No module named 'psycopg2._psycopg'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/www/mysite.com/wsgi.py", line 18, in <module>
application = get_wsgi_application()
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
django.setup()
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/base.py", line 299, in add_to_class
value.contribute_to_class(cls, name)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 2, in <module>
from django.db.backends.postgresql.base import \\
File "/home/user/.virtualenvs/venv/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2._psycopg'
Does "mod_wsgi: Runtime using Python/3.4.2." means something has to be compiled with my Python 3.5?
The answer was in the question. Thanks to the mod_wsgi Google Group, it's been confirmed that mod_wsgi should be installed globally using Python 3.5 (which my venv is based on): https://groups.google.com/forum/#!topic/modwsgi/UCgZG9amvv0
So I downloaded lastest mod_wsgi sources and configured them with my Python 3.5. Boom, problem solved!
Note: It means that every Python based site must use a virtualenv based on Python 3.5, e.g. the same Python used to configure mod_wsgi. If you have to serve sites with different Python versions then I guess you would need multiple Apache installations/instances.