How to Configure mod_wsgi on AlmaLinux 9

This guide walks you through each step in setting up mod_wsgi on AlmaLinux 9, from installation to configuration, allowing you to serve Python applications securely and effectively.

Python-based web applications have become increasingly common, and deploying them efficiently is essential for performance and reliability. One robust solution is using mod_wsgi, an Apache HTTP Server module designed to host Python applications via the WSGI interface. In this article, we’ll walk you through the process of installing and configuring mod_wsgi on AlmaLinux 9, a stable, RHEL-compatible Linux distribution that’s ideal for server environments.

This guide is suitable for system administrators, developers, and DevOps engineers who are comfortable with basic Linux system administration and want to deploy Python applications using Apache.


What is mod_wsgi?

mod_wsgi is an Apache module that allows you to run Python web applications within Apache itself using the WSGI (Web Server Gateway Interface) specification. It is often used to serve Flask, Django, and other Python web frameworks in a production environment.

Key benefits of mod_wsgi include:

  • Tight integration with Apache for stability and performance
  • Suitable for high-traffic production deployments
  • Security benefits through user/group separation
  • Flexibility in application deployment

Prerequisites

Before we dive into configuration, make sure the following prerequisites are met:

  • A clean AlmaLinux 9 installation
  • Access to a user with sudo privileges
  • Python 3.x installed (AlmaLinux 9 includes Python 3.9+ by default)
  • Apache HTTP Server installed
  • A sample Python WSGI application (for demonstration purposes)

Let’s begin step by step.


Step 1: Update Your System

Before installing anything, it’s a good idea to update your system’s package index and installed packages to the latest versions.

sudo dnf update -y

Once updated, you may want to install some basic tools:

sudo dnf install -y wget curl vim

Step 2: Install Apache HTTP Server (httpd)

If Apache is not already installed on your system, install it with the following command:

sudo dnf install httpd -y

Enable and start the Apache service:

sudo systemctl enable httpd
sudo systemctl start httpd

Verify that Apache is running:

sudo systemctl status httpd

You should see a message indicating that the service is active (running).


Step 3: Install Python and Development Tools

Although AlmaLinux 9 comes with Python 3.x, we also need development tools to compile mod_wsgi and other dependencies.

Install the required packages:

sudo dnf install python3 python3-devel gcc httpd-devel -y

The httpd-devel package provides the development files for Apache needed to compile mod_wsgi.


Step 4: Install mod_wsgi Using pip

While you can install mod_wsgi from system repositories, the version may be outdated. A better practice is to install it via pip.

First, install pip if it’s not already available:

sudo dnf install python3-pip -y

Now, install mod_wsgi:

pip3 install mod_wsgi

Once installed, verify it:

mod_wsgi-express --version

You should see the installed version printed in the terminal.


Step 5: Generate Apache Configuration with mod_wsgi-express

mod_wsgi-express simplifies the configuration process by generating the necessary Apache directives for running a WSGI application.

You can run this command to see how it generates an Apache configuration snippet:

mod_wsgi-express module-config

Sample output:

LoadModule wsgi_module "/usr/local/lib/python3.9/site-packages/mod_wsgi/server/mod_wsgi-py39.cpython-39-x86_64-linux-gnu.so"
WSGIPythonHome "/usr"

Add Configuration to Apache

Copy the output and paste it into Apache’s main configuration file:

sudo vim /etc/httpd/conf.modules.d/02-wsgi.conf

Paste the lines there and save the file.


Step 6: Create a Sample WSGI Application

Let’s create a minimal Python WSGI application for testing.

Create a new directory to store the application:

sudo mkdir -p /var/www/wsgi-sample
cd /var/www/wsgi-sample

Now, create the WSGI application file:

sudo vim app.wsgi

Paste the following content:

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello from mod_wsgi on AlmaLinux 9!'

    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

Change ownership to the Apache user:

sudo chown -R apache:apache /var/www/wsgi-sample

Step 7: Configure Apache to Serve the WSGI App

Create a new Apache virtual host file:

sudo vim /etc/httpd/conf.d/wsgi-sample.conf

Add the following configuration:

<VirtualHost *:80>
    ServerName wsgi.local

    WSGIScriptAlias / /var/www/wsgi-sample/app.wsgi

    <Directory /var/www/wsgi-sample>
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/wsgi-error.log
    CustomLog /var/log/httpd/wsgi-access.log combined
</VirtualHost>

Ensure the domain wsgi.local resolves properly. For testing, you can add it to your local /etc/hosts file:

echo "127.0.0.1 wsgi.local" | sudo tee -a /etc/hosts

Step 8: Restart Apache

After making changes, restart Apache:

sudo systemctl restart httpd

Check if Apache is listening on port 80:

sudo ss -tuln | grep :80

Step 9: Test the WSGI Application

Open a browser and navigate to:

http://wsgi.local

You should see:

Hello from mod_wsgi on AlmaLinux 9!

This means your WSGI application is correctly hosted using Apache and mod_wsgi.


Step 10: SELinux and Firewall Considerations

Firewall

If your server is behind a firewall (which it should be), ensure port 80 is open:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

SELinux

If SELinux is enforcing, you might encounter permission issues. You can allow Apache to access the directory like this:

sudo chcon -R --type=httpd_sys_content_t /var/www/wsgi-sample

Troubleshooting Tips

If your WSGI app doesn’t work as expected, here are a few things to check:

  • Apache logs (/var/log/httpd/wsgi-error.log) are your best friend for diagnosing issues.
  • Confirm correct file paths in the WSGIScriptAlias directive.
  • Make sure mod_wsgi is correctly loaded by Apache (apachectl -M | grep wsgi).
  • Ensure file permissions are readable by the Apache user (apache).
  • Use mod_wsgi-express start-server as a quick test environment.

Conclusion

You’ve now successfully installed and configured mod_wsgi on AlmaLinux 9. With this setup, you can serve Python web applications like Flask or Django securely and efficiently using Apache’s robust features.

Key takeaways:

  • mod_wsgi tightly integrates with Apache for reliable Python web app deployment.
  • AlmaLinux 9, being stable and RHEL-based, is an excellent platform for web servers.
  • Using mod_wsgi-express simplifies configuration and debugging.

This setup is suitable for small to medium production environments. For large-scale deployments, you might also consider alternatives like uWSGI + Nginx or Gunicorn + Nginx depending on your needs.