从Mysql切换到Django应用程序的Postgres数据库时,Apache mod_wsgi错误

时间:2019-05-16 20:42:57

标签: python django postgresql apache mod-wsgi

我的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,但仍然出现上述错误

1 个答案:

答案 0 :(得分:1)

使用sudo apt-get install python3-psycopg2代替pip3安装psycopg2为我解决了这个问题。我不知道为什么这行得通,而pip3 install psycopg2不行。

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