0

My django application has been running fine on my ubuntu ec2 instance, Apache2 + mod_wsgi, and a Mysql database running on the same instance. I decided to switch to a separate postgres RDS database, but when I try to use this database I get an Apache 'Internal server error' and the following wsgi errors in my Apache error.log file.

If I switch to the Mysql database in my settings.py, the website still works. The error only occurs when I just the separate postgres database. I am using Django 2.2 with python3.7

Apache configuration:

<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'

Any help would be appreciated. I have tried many of the solutions I found online and can't get this to work.
sudo apt-get install python3.7-dev allowed me to install psycopg2 but I am still getting the above errors

5
  • That error is telling you that you did not install psycopg2. Did you? Commented May 16, 2019 at 20:45
  • I was able to install psycopg2 by running (sudo apt-get install python3.7-dev) first. It didnt solve my issue though as I am getting the same Apache wsgi errors Commented May 16, 2019 at 21:44
  • did you restart Apache afterwards? Commented May 16, 2019 at 22:03
  • Yes. I posted the solution to my problem. I still don't know why pip3 install psycopg2 didn't work though Commented May 16, 2019 at 23:36
  • 2
    error shows that Apache uses Python3.6, not 3.7. Maybe you installed psycopg2 for Python 3.7 and apt-get installed for Python 3.6. Commented May 17, 2019 at 2:54

1 Answer 1

1

Instead of pip3 install psycopg2, using sudo apt-get install python3-psycopg2 solved the problem for me. I don't know why this works and pip3 install psycopg2 does not.

Please comment below if you know why.

Sign up to request clarification or add additional context in comments.

2 Comments

It's because mod_wsgi is installed against Python 3.6, and psycopg2 was installed for Python 3.7. Each version of Python installed has a separate location for system-wide installed packages, so the mod_wsgi running 3.6 can't see the psycopg2 installed for 3.7.
Thank you for clarifying

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.