如何在带有 Python 3.4 和 Apache2 的 CentOS 7 上将 PostgreSQL 与 Django 应用程序一起使用

How To Use PostgreSQL with a Django Application on CentOS 7 with Python 3.4 and Apache2

我是 运行 CentOS 7 服务器,我设法建立了一个 Django 站点并 运行 通过执行以下操作:

前面的步骤成功安装了一个新的 Django 1.8 项目,我能够在浏览器中查看 It worked! 页面。

当我修改 settings.py 文件以使用 PostgreSQL 9.4 作为我的项目数据库时,问题就开始了。我将默认 DATABASES 字典更改为以下内容:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'myproject',
        'USER': 'myprojectuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

当我在浏览器中按下刷新时,我从 Apache 得到一个 Internal Server Error 并且 error_log 说:

[] mod_wsgi (pid=1049): Target WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[] mod_wsgi (pid=1049): Exception occurred processing WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py'.
[] Traceback (most recent call last):
[]  File "/var/www/django/projects/mysite/mysite/wsgi.py", line 16, in <module>
[]     application = get_wsgi_application()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[]     django.setup()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
[]     apps.populate(settings.INSTALLED_APPS)
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/apps/registry.py", line 78, in populate
[]     raise RuntimeError("populate() isn't reentrant")
[] RuntimeError: populate() isn't reentrant
[] mod_wsgi (pid=1049): Target WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[] mod_wsgi (pid=1049): Exception occurred processing WSGI script '/var/www/django/projects/mysite/mysite/wsgi.py'.
[] Traceback (most recent call last):
[]   File "/var/www/django/projects/mysite/mysite/wsgi.py", line 16, in <module>
[]     application = get_wsgi_application()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[]     django.setup()
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
[]     apps.populate(settings.INSTALLED_APPS)
[]   File "/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages/django/apps/registry.py", line 78, in populate
[]     raise RuntimeError("populate() isn't reentrant")
[] RuntimeError: populate() isn't reentrant

我已经尝试了所有我能想到的方法来解决它。我认为问题是特定于 Python 3.4 的使用,并且可能没有正确版本的 mod_wsgi 来使用它。

我已经尝试通过 pip 安装 mod_wsgi,但我不确定如何让 Apache 使用该版本而不是系统范围的版本。这甚至可能不是导致问题的原因。

我几乎尝试了此网站上的所有问答,但都没有成功,因此我们将不胜感激,

谢谢。

编辑

这是我的 VirtualHost Apache2 配置:

<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com

DocumentRoot /var/www/django/projects/mysite/mysite

WSGIScriptAlias / /var/www/django/projects/mysite/mysite/wsgi.py

WSGIDaemonProcess example.com python-path=/var/www/django/projects/mysite:/var/www/django/virtual_environments/mysite/lib/python3.4/site-packages

WSGIProcessGroup example.com

  <Directory /var/www/django/projects/mysite/mysite>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>
</VirtualHost>

问题是 yum install mod_wsgi 安装的 mod_wsgi 版本与 Python 3.4 不兼容。

解决方案是通过 pip3.4 install mod_wsgi 安装 mod_wsgi,然后 运行 命令 mod_wsgi-express install-module。确保前面的两个命令都在您的虚拟环境之外 运行。

然后您必须编辑文件 /etc/httpd/conf.d/wsgi.conf 以反映您的设置。

我的 wsgi.conf 文件看起来类似于:

LoadModule wsgi_module /etc/httpd/modules/mod_wsgi-py34.cpython-34m.so

WSGIPythonHome /var/www/venv
WSGIDaemonProcess apache user=apache group=apache
WSGIProcessGroup apache
WSGISocketPrefix /var/run/wsgi

Alias /apps /var/www/apps/

<Directory /var/www/apps/>

  Options ExecCGI MultiViews Indexes
  MultiViewsMatch Handlers

  AddHandler wsgi-script .py
  AddHandler wsgi-script .wsgi

  DirectoryIndex index.html index.php index.py app.wsgi

  Order allow,deny
  Allow from all

</Directory>

参考:https://gist.github.com/jmorton/558f7079ed2159156277 (Thanks Paulo Scardine)