CentOS Stream 8
Sponsored Link

Apache httpd : Configure mod_wsgi2021/03/15

 
Install [mod_wsgi (WSGI : Web Server Gateway Interface)] to make Python scripts be fast.
[1]
[2] Install [mod_wsgi].
[root@www ~]#
dnf -y install python3-mod_wsgi
[3] For example, For example, configure WSGI to be able to access to [/test_wsgi] from [/var/www/html/test_wsgi.py].
[root@www ~]#
vi /etc/httpd/conf.d/python3_wsgi.conf
# create new

WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py
[root@www ~]#
systemctl restart httpd

[4] Create a test script which you set above.
[root@www ~]#
vi /var/www/html/test_wsgi.py
# create new

def application(environ, start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
           'WSGI Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'.encode("utf-8")
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]
[5] To use Django, Configure like follows. ( for Django settings, refer to here )
For example, Configure [test_app] under the [/home/cent/venv/testproject] which is owned by [cent] user.
[root@www ~]#
vi /etc/httpd/conf.d/django.conf
# create new

WSGIDaemonProcess test_app python-path=/home/cent/venv/testproject:/home/cent/venv/lib/python3.6/site-packages
WSGIProcessGroup test_app
WSGIScriptAlias /django /home/cent/venv/testproject/testproject/wsgi.py

<Directory /home/cent/venv/testproject>
    Require all granted
</Directory>

[root@www ~]#
systemctl restart httpd

# if SELinux is enabled and also use user directory like the example, it needs to change policy

[root@www ~]#
ll -d /home/cent

drwx------. 4 cent cent 125 Mar 12 14:20 /home/cent
[root@www ~]#
chmod 711 /home/cent

[root@www ~]#
setsebool -P httpd_read_user_content on

Matched Content