Installing mod_wsgi on CentOS via YUM: Repository Setup and Apache Integration


22 views

For Python developers deploying web applications on CentOS, mod_wsgi serves as a critical bridge between Apache and Python. The module implements the WSGI specification, allowing Apache to host Python applications. While CentOS's default repositories don't always carry the latest versions, the EPEL (Extra Packages for Enterprise Linux) repository typically provides well-maintained packages.

Before installing mod_wsgi, ensure EPEL is enabled:


sudo yum install epel-release
sudo yum update

The standard installation command is straightforward:


sudo yum install mod_wsgi

For Python 3 specific installations (on CentOS 7+):


sudo yum install python3-mod_wsgi

Check successful installation with:


httpd -M | grep wsgi

Expected output should include wsgi_module in the loaded modules list.

Basic WSGI application configuration in Apache:


<VirtualHost *:80>
    WSGIScriptAlias / /var/www/html/myapp.wsgi
    <Directory /var/www/html>
        Require all granted
    </Directory>
</VirtualHost>

Sample WSGI application file (/var/www/html/myapp.wsgi):


def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'
    
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    
    return [output]

If encountering permission issues:


sudo chown apache:apache /var/www/html/myapp.wsgi
sudo chmod 755 /var/www/html/myapp.wsgi

For SELinux contexts:


sudo chcon -R -t httpd_sys_content_t /var/www/html/

For specific Python versions or custom requirements, consider pip installation:


sudo yum install python3-pip httpd-devel
sudo pip3 install mod_wsgi

mod_wsgi is an Apache module that provides a WSGI compliant interface for hosting Python web applications. Before installation, ensure your system meets these requirements:

  • CentOS 7 or 8 (this guide covers both)
  • Apache HTTP Server (httpd) installed
  • Python development packages
  • EPEL repository enabled

First, update your package manager and install essential dependencies:

sudo yum update -y
sudo yum install -y epel-release
sudo yum groupinstall -y "Development Tools"
sudo yum install -y python3-devel httpd-devel

For CentOS 7/8 with Python 3:

sudo yum install -y python3-mod_wsgi

For legacy Python 2.7 systems (not recommended):

sudo yum install -y mod_wsgi

Check if the module loaded successfully:

httpd -M | grep wsgi

Expected output if successful:

wsgi_module (shared)

Create a basic test application at /var/www/wsgi-scripts/test.wsgi:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World! WSGI is working!\n'
    
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    
    return [output]

Add this configuration to your Apache virtual host:

<VirtualHost *:80>
    ServerName yourdomain.com
    
    WSGIScriptAlias / /var/www/wsgi-scripts/test.wsgi
    
    <Directory /var/www/wsgi-scripts>
        Require all granted
    </Directory>
</VirtualHost>

Permission errors: Ensure SELinux contexts are correct:

sudo chcon -R -t httpd_sys_content_t /var/www/wsgi-scripts/
sudo chcon -R -t httpd_sys_rw_content_t /var/www/wsgi-scripts/

Module not loading: Check Apache error logs:

sudo tail -f /var/log/httpd/error_log

Python version mismatch: Verify the Python version mod_wsgi was compiled for:

strings /usr/lib64/httpd/modules/mod_wsgi.so | grep python

For production environments, consider these directives in your WSGI configuration:

WSGIDaemonProcess myapp processes=2 threads=15
WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}