Rocky_Linux_8
Sponsored Link

Apache httpd : Configure mod_wsgi
2021/08/03
 
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/rocky/venv/testproject:/home/rocky/venv/lib/python3.6/site-packages
WSGIProcessGroup test_app
WSGIScriptAlias /django /home/rocky/venv/testproject/testproject/wsgi.py

<Directory /home/rocky/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/rocky

drwx------. 5 rocky rocky 144 Aug  3 11:11 /home/rocky
[root@www ~]#
chmod 711 /home/rocky

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

Matched Content