Posted on
Apache Web Server

Installing Apache with Python (mod_wsgi)

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Installing Apache with Python (mod_wsgi) on Linux

In today's interconnected world, setting up a robust, scalable web server is a crucial skill for many developers and system administrators. Apache HTTP Server, paired with Python via mod_wsgi, is a potent combination for serving Python-based web applications to the internet. This tutorial will walk you through the steps to install Apache with the mod_wsgi module on a Linux system.

Step 1: Update Your System

Before beginning any installation, it’s good practice to update your system's package list. In Debian-based distributions like Ubuntu, use:

sudo apt update && sudo apt upgrade

For Red Hat-based distributions like CentOS, use:

sudo yum update

For openSUSE or SUSE Linux Enterprise, update your system with:

sudo zypper update

Keeping your system updated ensures compatibility and security.

Step 2: Install Apache

Apache is one of the most popular web servers in the world. To install Apache, execute one of the following commands based on your Linux distribution.

On Ubuntu/Debian:

sudo apt install apache2

On CentOS/Red Hat:

sudo yum install httpd

On openSUSE or SUSE Linux Enterprise:

sudo zypper install apache2

Once the installation is complete, you can start the Apache service with:

sudo systemctl start apache2       # Ubuntu/Debian
sudo systemctl start httpd         # CentOS/Red Hat
sudo systemctl start apache2       # openSUSE/SLE

Ensure it is enabled to start on boot:

sudo systemctl enable apache2      # Ubuntu/Debian
sudo systemctl enable httpd        # CentOS/Red Hat
sudo systemctl enable apache2      # openSUSE/SLE

Step 3: Install Python and mod_wsgi

You must have Python installed on your system. You can install Python with:

sudo apt install python3           # Ubuntu/Debian
sudo yum install python3           # CentOS/Red Hat
sudo zypper install python3        # openSUSE/SLE

Next, install mod_wsgi, which is an Apache module that provides a WSGI compliant interface for hosting Python-based web applications under Apache.

For Ubuntu/Debian:

sudo apt install libapache2-mod-wsgi-py3

For CentOS/Red Hat:

sudo yum install mod_wsgi

For openSUSE/SLE:

sudo zypper install apache2-mod_wsgi

If you're using Python 3, make sure to install the module specific to Python 3, as some systems might still default to Python 2.

Step 4: Configure Apache to Use mod_wsgi

Create a configuration file for your application in /etc/apache2/sites-available/ (Ubuntu/Debian) or /etc/httpd/conf.d/ (CentOS/Red Hat) or /etc/apache2/vhosts.d/ (openSUSE/SLE).

Here's an example configuration:

<VirtualHost *:80>
    ServerName yourdomain.com
    WSGIScriptAlias / /path/to/your/application.wsgi

    <Directory /path/to/your/application>
        Require all granted
    </Directory>
</VirtualHost>

Replace /path/to/your/application.wsgi with the path to your WSGI file, and update the ServerName with your domain.

Enable the new site configuration:

sudo a2ensite yourdomain.conf    # Ubuntu/Debian
sudo a2enconf yourdomain.conf    # openSUSE/SLE

Reload Apache to apply the changes:

sudo systemctl reload apache2     # Ubuntu/Debian
sudo systemctl reload httpd       # CentOS/Red Hat
sudo systemctl reload apache2     # openSUSE/SLE

Step 5: Testing Your Setup

Create a simple WSGI application to test your setup. Here’s a basic example you can use:

Create a file named application.wsgi:

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    message = 'Hello, World!'
    return [message.encode('utf-8')]

Point to this file in your Apache configuration as illustrated earlier, then access your server’s IP or domain name from a browser.

Conclusion

Setting up Apache with mod_wsgi provides a scalable and efficient way to host Python applications. By following the steps above, you can create a robust environment for running your Python web applications, taking full advantage of Apache's proven stability and mod_wsgi's seamless Python integration. Ensure to test your setup thoroughly and consult your system's specific documentation for tweaks and optimizations particular to your environment. Happy hosting!

Further Reading

For readers interested in diving deeper into aspects related to this topic, here are several resources that provide additional information and context:

These resources cover a broad spectrum from simple tutorials to comprehensive and officially endorsed documentation, providing vital support for anyone starting or perfecting their Python applications on Apache servers.