我正在努力使Django与mod_wsgi运行。 我试图按照这里的build议,但没有奏效。
服务器是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已经安装了–ensable-shared。 virtualenv venv使用这个Python,因为用户.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 ,正如解释的那样,它适用于Python3.X。 那么为什么我在这里有一个“No module named'psycopg2._psycopg'”错误?
mod_wsgi已经安装在virtualenv venv中。
这是虚拟主机:
<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>
这里是我的apache error.log文件的内容:
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'
“mod_wsgi:使用Python / 3.4.2的运行时”。 意味着必须使用我的Python 3.5进行编译?
答案是在这个问题上。 感谢mod_wsgi Google Group ,已经证实mod_wsgi应该使用Python 3.5(我的基础)来全局安装: https ://groups.google.com/forum/#!topic/modwsgi/UCgZG9amvv0
所以我下载了最新的mod_wsgi源文件 ,并使用我的Python 3.5进行了配置。 繁荣,问题解决了!
注意:这意味着每个基于Python的站点必须使用基于Python 3.5的virtualenv,例如,用于配置mod_wsgi的Python。 如果你必须用不同的Python版本来提供网站,那么我想你会需要多个Apache安装/实例。