Install mod_wsgi (Web Server Gateway Interface) for efficient execution of Python scripts in Apache httpd.
First, ensure Python 3 is installed on your system.
mod_wsgiInstall the mod_wsgi module for Python 3.
# Install mod_wsgi for Python 3
[root@www ~]# dnf -y install python3-mod_wsgi
mod_wsgi for Python ScriptSet up mod_wsgi to serve a Python script from a specified URL.
# Create and edit the mod_wsgi configuration
[root@www ~]# vi /etc/httpd/conf.d/python3_wsgi.conf
# Add the following configuration
WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py
# Restart httpd to apply changes
[root@www ~]# systemctl restart httpd
Create a Python WSGI script for testing the configuration.
# Create a test WSGI script
[root@www ~]# vi /var/www/html/test_wsgi.py
# Add the following Python code
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]
mod_wsgi for DjangoTo use Django, configure mod_wsgi accordingly. For example, set up a Django project called test_app in /home/cent/testproject.
# Configure mod_wsgi for Django
[root@www ~]# vi /etc/httpd/conf.d/django.conf
# Add the following configuration
WSGIDaemonProcess test_app python-path=/home/cent/testproject:/home/cent/django/lib/python3.9/site-packages
WSGIProcessGroup test_app
WSGIScriptAlias /django /home/cent/testproject/testproject/wsgi.py
<Directory /home/cent/testproject>
Require all granted
</Directory>
# Reload httpd to apply changes
[root@www ~]# systemctl reload httpd
# Adjust SELinux policy if necessary
[root@www ~]# chmod 711 /home/cent
[root@www ~]# setsebool -P httpd_read_user_content on
This setup allows Apache to serve Python applications efficiently, either as standalone scripts or as part of a Django project. Remember to adjust SELinux policies accordingly if you're using user directories for your applications.