从MySQL切换到Django应用程序Postgres数据库时ApacheMOD_WSGI错误

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (40)

我的django应用程序在我的ubuntu ec2实例,Apache2 + mod_wsgi和在同一实例上运行的Mysql数据库上运行良好。我决定切换到一个单独的postgres RDS数据库,但是当我尝试使用这个数据库时,我的Apache error.log文件中出现了Apache'内部服务器错误'和以下wsgi错误。

如果我在settings.py中切换到Mysql数据库,该网站仍然有效。只有在我独立的postgres数据库时才会出现错误。我正在使用Django 2.2和python3.7

Apache配置:

<IfModule mod_ssl.c>
<VirtualHost *:443>
...
Alias /static /home/ubuntu/ratemanager/Projects/static
        <Directory /home/ubuntu/ratemanager/Projects/static>
                Require all granted
        </Directory>

        <Directory /home/ubuntu/ratemanager/Projects/Projects>
                <Files wsgi.py>
                        Require all granted
                </Files>
        </Directory>

        WSGIScriptAlias / /home/ubuntu/ratemanager/Projects/Projects/wsgi.py
        WSGIDaemonProcess django_app python-path=/home/ubuntu/ratemanager/Projects python-home=/home/ubuntu/ratemanager/Projects/venv
        WSGIProcessGroup django_app
SSLCertificateFile /etc/letsencrypt/live/...
SSLCertificateKeyFile /etc/letsencrypt/...
Include /etc/letsencrypt/options-ssl-apache.conf 

error.log中

[core:notice] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] mod_wsgi (pid=27601): Target WSGI script '/home/ubuntu/ratemanager/Projects/Projects/wsgi.py' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=27601): Exception occurred processing WSGI script '/home/ubuntu/ratemanager/Projects/Projects/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py", line 20, in <module>
[wsgi:error]     import psycopg2 as Database
[wsgi:error] ModuleNotFoundError: No module named 'psycopg2'
[wsgi:error]
[wsgi:error] During handling of the above exception, another exception occurred:
[wsgi:error]
[wsgi:error] Traceback (most recent call last):
[wsgi:error]   File "/home/ubuntu/ratemanager/Projects/Projects/wsgi.py", line 15, in <module>
[wsgi:error]     application = get_wsgi_application()
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[wsgi:error]     django.setup(set_prefix=False)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup
[wsgi:error]     apps.populate(settings.INSTALLED_APPS)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 112, in populate
[wsgi:error]     app_config.import_models()
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/apps/config.py", line 198, in import_models
[wsgi:error]     self.models_module = import_module(models_module_name)
[wsgi:error]   File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
[wsgi:error]     return _bootstrap._gcd_import(name[level:], package, level)
[wsgi:error]   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
[wsgi:error]   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
[wsgi:error]   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
[wsgi:error]   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
[wsgi:error]   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
[wsgi:error]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[wsgi:error]   File "/home/ubuntu/ratemanager/Projects/RateManager/models.py", line 2, in <module>
[wsgi:error]     from django.contrib.auth.models import User
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/models.py", line 2, in <module>
[wsgi:error]     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/contrib/auth/base_user.py", line 47, in <module>
[wsgi:error]     class AbstractBaseUser(models.Model):
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 101, in __new__
[wsgi:error]     new_class.add_to_class('_meta', Options(meta, app_label))
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/models/base.py", line 305, in add_to_class
[wsgi:error]     value.contribute_to_class(cls, name)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/models/options.py", line 203, in contribute_to_class
[wsgi:error]     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/__init__.py", line 33, in __getattr__
[wsgi:error]     return getattr(connections[DEFAULT_DB_ALIAS], item)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 202, in __getitem__
[wsgi:error]     backend = load_backend(db['ENGINE'])
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/utils.py", line 110, in load_backend
[wsgi:error]     return import_module('%s.base' % backend_name)
[wsgi:error]   File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
[wsgi:error]     return _bootstrap._gcd_import(name[level:], package, level)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/db/backends/postgresql/base.py", line 24, in <module>
[wsgi:error]     raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
[wsgi:error] django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
[wsgi:error] mod_wsgi (pid=27601): Target WSGI script '/home/ubuntu/ratemanager/Projects/Projects/wsgi.py' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=27601): Exception occurred processing WSGI script '/home/ubuntu/ratemanager/Projects/Projects/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error]   File "/home/ubuntu/ratemanager/Projects/Projects/wsgi.py", line 15, in <module>
[wsgi:error]     application = get_wsgi_application()
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[wsgi:error]     django.setup(set_prefix=False)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup
[wsgi:error]     apps.populate(settings.INSTALLED_APPS)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 81, in populate
[wsgi:error]     raise RuntimeError("populate() isn't reentrant")
[wsgi:error] RuntimeError: populate() isn't reentrant
[wsgi:error] mod_wsgi (pid=27601): Target WSGI script '/home/ubuntu/ratemanager/Projects/Projects/wsgi.py' cannot be loaded as Python module.
[wsgi:error] mod_wsgi (pid=27601): Exception occurred processing WSGI script '/home/ubuntu/ratemanager/Projects/Projects/wsgi.py'.
[wsgi:error] Traceback (most recent call last):
[wsgi:error]   File "/home/ubuntu/ratemanager/Projects/Projects/wsgi.py", line 15, in <module>
[wsgi:error]     application = get_wsgi_application()
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[wsgi:error]     django.setup(set_prefix=False)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup
[wsgi:error]     apps.populate(settings.INSTALLED_APPS)
[wsgi:error]   File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 81, in populate
[wsgi:error]     raise RuntimeError("populate() isn't reentrant")
[wsgi:error] RuntimeError: populate() isn't reentrant
[mpm_event:notice] AH00491: caught SIGTERM, shutting down
[mpm_event:notice] AH00489: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.0g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[core:notice] AH00094: Command line: '/usr/sbin/apache2'

任何帮助,将不胜感激。我已经尝试了很多我在网上找到的解决方案,但却无法实现这一点。 sudo apt-get install python3.7-dev允许我安装psycopg2,但我仍然遇到上述错误

提问于
用户回答回答于

而不是pip3安装psycopg2,使用sudo apt-get install python3-psycopg2解决了我的问题。我不知道为什么这个工作和pip3安装psycopg2没有。

如果您知道原因,请在下面评论。

扫码关注云+社区

领取腾讯云代金券