Home > Enterprise >  httpd wont start with custom conf files and mod_wsgi built with python3.9
httpd wont start with custom conf files and mod_wsgi built with python3.9

Time:06-09

I am working on a RedHat Centos 7 box. I have installed python 3.9.2 into a folder under /opt/python3.9. I am in the midst of moving my dJango server to production, and have chosen to use Apache (I have installed httpd-devel) with mod_wsgi. I was in the midst of following their instructions to make sure it gets configured correctly.

I installed apache:

sudo yum install httpd
sudo yum install httpd-devel

then

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/refs/tags/4.9.2.tar.gz
mv 4.9.2.tar.gz ./mod_wsgi_4.9.2.tar.gz
tar xvfz mod_wsgi-4.9.2.tar.gz
cd mod_wsgi*
./configure --with-python=/opt/python3.9/bin/python39
make
sudo make install

all with no errors.

sudo systemctl enable httpd
sudo systemctl start httpd

But as soon as I try to use the demo here (which basically entails adding a conf file to /etc/httpd/conf.d/, called wsgi.conf, and a response file to /var/www/html/, called test_wsgi.py, then restarting apache), it thows an error and tells me to check journalctl -xe.

Jun 08 21:04:01 ip-172-31-18-8.ec2.internal httpd[11893]: AH00526: Syntax error on line 2 of /etc/httpd/conf.d/wsgi.conf:
Jun 08 21:04:01 ip-172-31-18-8.ec2.internal httpd[11893]: Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configuration
Jun 08 21:04:01 ip-172-31-18-8.ec2.internal systemd[1]: httpd.service: main process exited, code=exited, status=1/FAILURE
Jun 08 21:04:01 ip-172-31-18-8.ec2.internal systemd[1]: Failed to start The Apache HTTP Server.
-- Subject: Unit httpd.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit httpd.service has failed.
--
-- The result is failed.
Jun 08 21:04:01 ip-172-31-18-8.ec2.internal systemd[1]: Unit httpd.service entered failed state.
Jun 08 21:04:01 ip-172-31-18-8.ec2.internal systemd[1]: httpd.service failed.
Jun 08 21:04:01 ip-172-31-18-8.ec2.internal sudo[11888]: pam_unix(sudo:session): session closed for user root

I am 95% certain that if I did what was suggested here that it would compile mod_wsgi for python 2.7 and I don't want to use python2.7... that's why I compiled mod_wsgi for python 3.9.2.

If I try to use my django.conf file instead of the one I linked in the demo, I get a different error that might be more helpful for the slue:

httpd.conf: Syntax error on line 3 of /etc/httpd/conf.d/django.conf: Cannot load /usr/lib64/httpd/modules/mod_wsgi.so into server: libpython3.9.so.1.0: cannot open shared object file: No such file or directory

Line 3 is:

LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so

Output of ldd /usr/lib64/httpd/modules/mod_wsgi.so:

[ec2-user@ip-172-31-18-8 ~]$ ldd /usr/lib64/httpd/modules/mod_wsgi.so
        linux-vdso.so.1 (0x00007ffd7b50d000)
        libpython3.9.so.1.0 => /opt/python39/lib/libpython3.9.so.1.0 (0x00007ff0ebf85000)
        libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007ff0ebd4e000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007ff0ebb30000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007ff0eb92c000)
        libutil.so.1 => /lib64/libutil.so.1 (0x00007ff0eb729000)
        libm.so.6 => /lib64/libm.so.6 (0x00007ff0eb3e9000)
        libc.so.6 => /lib64/libc.so.6 (0x00007ff0eb03e000)
        /lib64/ld-linux-x86-64.so.2 (0x00007ff0ec781000)

I can verify that /opt/python39/lib/libpython3.9.so.1.0 exists and I have it in my LD_RUN_PATH and LD_LIBRARY_PATH variables

Output of sudo apachectl -V:

Server version: Apache/2.4.53 ()
Server built:   Apr 12 2022 12:00:44
Server's Module Magic Number: 20120211:124
Server loaded:  APR 1.7.0, APR-UTIL 1.6.1, PCRE 8.32 2012-11-30
Compiled using: APR 1.7.0, APR-UTIL 1.6.1, PCRE 8.32 2012-11-30
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_PROC_PTHREAD_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

Thank you for your help. I am a little busy, so I will try to get back with you ASAP.

CodePudding user response:

You are seeing this error...

libpython3.9.so.1.0: cannot open shared object file: No such file or directory

...because httpd has no idea it's supposed to look in /opt/python3.9/lib to find the necessary shared library. There are several ways of resolving this problem:

  1. Set -rpath when linking the module.

    This embeds a path to the library in the compiled binary. You would set it by running make like this inside the mod_wsgi-4.9.2 directory:

    make LDFLAGS='-L/opt/python3.9/lib -Wl,-rpath,/opt/python3.9/lib'
    
  2. Set LD_LIBRARY_PATH in httpd's environment. This provides httpd with an additional list of directories to search for shared libraries. We can test it like this:

    LD_LIBRARY_PATH=/opt/python3.9/lib httpd -DFOREGROUND
    

    To set it persistently, you'd want to customize the httpd service unit:

    • Run systemctl edit httpd

    • In the editor that comes up, add the following content:

      [Service]
      Environment=LD_LIBRARY_PATH=/opt/python3.9/lib
      

      This creates /etc/systemd/system/httpd.service.d/override.conf.

    • Run systemctl daemon-reload to refresh the cached version of the unit file.

    • Restart your httpd service.

  3. Edit the global library search path by creating /etc/ld.so.conf.d/python3.9.conf with the following content:

    /opt/python3.9/lib
    

    Then run:

    ldconfig
    

Any of the above options should get things running for you.

  • Related