首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为Django应用程序从Mysql切换到Postgres数据库时出现Apache mod_wsgi错误。

为Django应用程序从Mysql切换到Postgres数据库时出现Apache mod_wsgi错误。
EN

Stack Overflow用户
提问于 2019-05-17 04:42:57
回答 1查看 529关注 0票数 0

我的django应用程序在我的ubuntu实例Apache2 + mod_wsgi和运行在同一实例上的Mysql数据库上运行得很好。我决定切换到一个单独的postgres RDS数据库,但是当我尝试使用这个数据库时,在我的Apache error.log文件中得到一个Apache 'Internal server error‘和下面的wsgi错误。

如果我在我的settings.py中切换到Mysql数据库,网站仍然可以工作。只有当我只使用单独的postgres数据库时,才会出现错误。我使用的是Django 2.2和python3.7

Apache配置:

代码语言:javascript
复制
<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

代码语言:javascript
复制
[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,但我仍然收到上面的错误

EN

回答 1

Stack Overflow用户

发布于 2019-05-17 05:59:55

用sudo apt-get install python3-psycopg2代替pip3 install python2解决了这个问题。我不知道为什么这个能用,而pip3 install psycopg2却不行。

如果你知道原因,请在下面发表评论。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56176334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档