Trouble with WSGI configuration, few days of troubleshooting already

Hello,

I am having a heck of a time getting this psycopg2 to work. I have been trying with AI and old searches for a couple days now but I think I need some professional advice.

I am running RedHat 9.4, the venv is django 5.0.6 and python 3.12.1.

The dev server work flawlessly so I am pretty much certain that it is my WSGI setup.
Here is my wsgi.py

"""
WSGI config for myproject project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.DJANGO5project.com/en/5.0/howto/deployment/wsgi/
"""

import os
import sys
print("SYSTEM PATH:", sys.path)


path_to_your_project = '/var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject'
path_to_virtualenv_site_packages = '/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib/python3.12/site-packages'

sys.path.append(path_to_your_project)
sys.path.append(path_to_virtualenv_site_packages)
print("SYSTEM PATH:", sys.path)

from DJANGO5.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO5_SETTINGS_MODULE', 'myproject.settings')

application = get_wsgi_application()

Here is my vhost configuration

NameVirtualHost *:443

# WSGIPythonHome "/var/www/html/DJANGO5/python3.12.1/my-venv-312"

 LoadModule wsgi_module "/usr/lib64/httpd/modules/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so"
# LoadModule wsgi_module "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/html/DJANGO5/python3.12.1/my-venv-312"



<VirtualHost *:443>

ServerName www.my.domain
ServerAlias my.domain

Alias /staticfiles "/var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject/staticfiles"
<Directory /var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject/staticfiles>
        Require all granted
</Directory>

<Directory /var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject/myproject>
        <Files wsgi.py>
                Require all granted
        </Files>
</Directory>

WSGIDaemonProcess my.domain python-home=/var/www/html/DJANGO5/python3.12.1/my-venv-312 python-path=/var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject

WSGIScriptAlias / /var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject/myproject/wsgi.py process-group=my.domain

WSGIProcessGroup my.domain

WSGIApplicationGroup %{GLOBAL}

# Other directives here


</VirtualHost>
# mod_wsgi-express module-config
LoadModule wsgi_module "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/mod_wsgi/server/mod_wsgi-py312.cpython-312-x86_64-linux-gnu.so"
WSGIPythonHome "/var/www/html/DJANGO5/python3.12.1/my-venv-312"

Location of site packages

psycopg:
(my-venv-312) [root@serv1 myproject]# cd ../../../python3.12.1/my-venv-312/lib/python3.12/site-packages/
(my-venv-312) [root@serv1 site-packages]# ls
asgiref                     lxml                      pip-23.2.1.dist-info             psycopg2_binary.libs       rlextra-4.2.2.dist-info             typing_extensions.py
asgiref-3.8.1.dist-info     lxml-5.2.2.dist-info      Pmw                              psycopg-3.1.19.dist-info   sqlparse                            webencodings
chardet                     mod_wsgi                  Pmw_py3-2.1.dist-info            __pycache__                sqlparse-0.5.0.dist-info            webencodings-0.5.1.dist-info
chardet-4.0.0.dist-info     mod_wsgi-5.0.0.dist-info  preppy-4.2.3.dist-info           pypdf                      svglib
cssselect2                  PIL                       preppy.py                        pypdf-4.2.0.dist-info      svglib-1.4.1.dist-info
cssselect2-0.7.0.dist-info  pillow-10.3.0.dist-info   psycopg                          reportlab                  tinycss2
DJANGO5                      pillow.libs               psycopg2                         reportlab-4.2.2.dist-info  tinycss2-1.3.0.dist-info
DJANGO5-5.0.6.dist-info      pip                       psycopg2_binary-2.9.9.dist-info  rlextra                    typing_extensions-4.12.2.dist-info

This error popped up when I ran the localhost server which is crazy because I was creating this and another venv with 3.9 and 11 today to troubleshoot but didn’t think it was the issue so just continued on with my 12 venv. So these make no sense and might just erase them too.


[Tue Jun 25 20:53:01.300917 2024] [wsgi:error] [pid 683666:tid 683800] [remote :24374] RuntimeError: populate() isn't reentrant
[Tue Jun 25 21:01:18.257985 2024] [mime_magic:error] [pid 683669:tid 683840] [client :55310] AH01512: mod_mime_magic: can't read `/var/www/html/DJANGO5/python3.9-venv/myprojectvenv309/lib/python3.9/site-packages/Pmw_py3-2.1-py3.9.egg-info/PKG-INFO'
[Tue Jun 25 21:01:53.865613 2024] [mime_magic:error] [pid 683669:tid 683853] [client :55310] AH01512: mod_mime_magic: can't read `/var/www/html/DJANGO5/python3.9-venv/myprojectvenv309/lib/python3.9/site-packages/svglib-1.4.1-py3.9.egg-info/PKG-INFO'
[Tue Jun 25 21:01:58.234287 2024] [mime_magic:error] [pid 683669:tid 683858] [client :55310] AH01512: mod_mime_magic: can't read `/var/www/html/DJANGO5/python3.9-venv/myprojectvenv309/lib64/python3.9/site-packages/Pmw_py3-2.1-py3.9.egg-info/PKG-INFO'
[Tue Jun 25 21:02:29.481775 2024] [mime_magic:error] [pid 683669:tid 683849] [client :54924] AH01512: mod_mime_magic: can't read `/var/www/html/DJANGO5/python3.9-venv/myprojectvenv309/lib64/python3.9/site-packages/svglib-1.4.1-py3.9.egg-info/PKG-INFO'

And here are the errors I’m getting

[Tue Jun 25 20:43:55.044250 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] mod_wsgi (pid=683666): Failed to exec Python script file '/var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject/myproject/wsgi.py'.
[Tue Jun 25 20:43:55.044275 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] mod_wsgi (pid=683666): Exception occurred processing WSGI script '/var/www/html/DJANGO5/python3.12.1/my-venv-312/myproject/myproject/wsgi.py'.
[Tue Jun 25 20:43:55.045947 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] Traceback (most recent call last):
[Tue Jun 25 20:43:55.046063 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]   File "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/DJANGO5/db/backends/postgresql/base.py", line 25, in <module>
[Tue Jun 25 20:43:55.046070 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     import psycopg as Database
[Tue Jun 25 20:43:55.046077 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]   File "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/psycopg/__init__.py", line 9, in <module>
[Tue Jun 25 20:43:55.046084 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     from . import pq  # noqa: F401 import early to stabilize side effects
[Tue Jun 25 20:43:55.046086 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     ^^^^^^^^^^^^^^^^
[Tue Jun 25 20:43:55.046090 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]   File "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/psycopg/pq/__init__.py", line 114, in <module>
[Tue Jun 25 20:43:55.046092 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     import_from_libpq()
[Tue Jun 25 20:43:55.046096 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]   File "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/psycopg/pq/__init__.py", line 106, in import_from_libpq
[Tue Jun 25 20:43:55.046098 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     raise ImportError(
[Tue Jun 25 20:43:55.046106 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] ImportError: no pq wrapper available.
[Tue Jun 25 20:43:55.046108 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] Attempts made:
[Tue Jun 25 20:43:55.046110 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] - couldn't import psycopg 'c' implementation: No module named 'psycopg_c'
[Tue Jun 25 20:43:55.046111 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] - couldn't import psycopg 'binary' implementation: No module named 'psycopg_binary'
[Tue Jun 25 20:43:55.046113 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] - couldn't import psycopg 'python' implementation: 
[Tue Jun 25 20:43:55.046122 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] 
[Tue Jun 25 20:43:55.046124 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] During handling of the above exception, another exception occurred:
[Tue Jun 25 20:43:55.046126 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] 
[Tue Jun 25 20:43:55.046130 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] Traceback (most recent call last):
[Tue Jun 25 20:43:55.046157 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]   File "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/DJANGO5/db/backends/postgresql/base.py", line 27, in <module>
[Tue Jun 25 20:43:55.046161 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     import psycopg2 as Database
[Tue Jun 25 20:43:55.046164 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]   File "/var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/psycopg2/__init__.py", line 51, in <module>
[Tue Jun 25 20:43:55.046166 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776]     from psycopg2._psycopg import (                     # noqa
[Tue Jun 25 20:43:55.046172 2024] [wsgi:error] [pid 683666:tid 683767] [remote :58776] ImportError: /var/www/html/DJANGO5/python3.12.1/my-venv-312/lib64/python3.12/site-packages/psycopg2/_psycopg.cpython-312-x86_64-linux-gnu.so: failed to map segment from shared object

This is a lot of information and maybe not even enough but I thought I should stop here. I have tried all I can so I am at the mercy of someone who knows what they’re doing. I can’t figure this out myself.

Thank you for your time.

Hello,

Just a little update so no one wastes their time.

I was able to partially solve the issue by temporarily setting SELinux to Permissive

setenforce Permissive

which allowed the app to run without issue.

so now I am troubleshooting a permissions issue.

If anybody has any ideas of a good steps to take.

So far I have done

chown root:apache -R DJANGO5
find . -type f -exec chmod 644 -- {} +
find . -type d -exec chmod 755 -- {} +
# semanage fcontext -a -t httpd_sys_rw_content_t '/path/to/your/app(/.*)?'
# restorecon -R -v /path/to/your/app