mod_wsgi

Installing mod_wsgi on Ubuntu 24.04: A Step-by-Step Guide

Welcome to the Greenhost.Cloud blog! In today’s post, we’ll go through the installation process of mod_wsgi, an essential Apache module that allows you to host Python web applications via the popular Apache HTTP Server. As Ubuntu 24.04 is the latest LTS release, we’ll ensure that our guide is up-to-date and straightforward for all users.

What is mod_wsgi?

mod_wsgi is a Python module that provides a simple and efficient way to run Python applications on an Apache server. It acts as a connector between Apache and Python, handling requests and enabling the seamless execution of Python scripts. This makes it an excellent choice for deploying web frameworks like Django, Flask, or Pyramid.

Prerequisites

Before we dive into the installation steps, make sure you have the following:

  1. A server running Ubuntu 24.04.
  2. Apache installed on your system.
  3. Basic knowledge of the terminal and an understanding of how to work with Python and virtual environments.

Step 1: Update Your System

First, open your terminal and update your package list to ensure you have the latest repositories:

sudo apt update
sudo apt upgrade -y

Step 2: Install Apache

If you haven’t installed Apache yet, you can do so with the following command:

sudo apt install apache2 -y

Once installed, you can start and enable Apache to run on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

To verify Apache is running, open your web browser and navigate to http://your_server_ip. You should see the default Apache welcome page.

Step 3: Install Required Packages

To install mod_wsgi, you will need to install the development tools required to build the module:

sudo apt install apache2-dev python3-dev -y

Additionally, if you wish to use pip to install mod_wsgi, you’ll need to install it as well:

sudo apt install python3-pip -y

Step 4: Install mod_wsgi

You can install mod_wsgi using pip which is straightforward and ensures you get the latest version:

pip3 install mod_wsgi

Step 5: Configure Apache to Use mod_wsgi

After installation, you’ll need to configure Apache to load the mod_wsgi module. You can do this by creating a configuration file for your WSGI application.

First, we need to get the path where mod_wsgi is installed. You can find it by running:

mod_wsgi-express module-config

This command will output a configuration line that looks like:

LoadModule wsgi_module "/path/to/mod_wsgi-*.so"
WSGIPythonHome "/path/to/your/virtual/env"

Add the above output to the Apache configuration by either editing the global Apache config file or creating a new .conf file in the /etc/apache2/sites-available/ directory.

For example:

sudo nano /etc/apache2/sites-available/myapp.conf

Now, add the following sample configuration, replacing paths accordingly:

<VirtualHost *:80>
    ServerName your_domain_or_IP

    WSGIDaemonProcess myapp python-home=/path/to/your/virtual/env
    WSGIScriptAlias / /path/to/your/wsgi/app.wsgi

    <Directory /path/to/your/app/>
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/myapp-error.log
    CustomLog ${APACHE_LOG_DIR}/myapp-access.log common
</VirtualHost>

Step 6: Enable Your New Site and Restart Apache

Enable your new site configuration and restart Apache to apply the changes:

sudo a2ensite myapp.conf
sudo systemctl restart apache2

Step 7: Create Your WSGI Application File

Finally, you need to create your WSGI application file (e.g., app.wsgi). This file will contain the entry point for your application. Here’s a simple example for a Flask application:

import sys
import os

# Add the path to your application folder
sys.path.insert(0, '/path/to/your/app')

from yourapp import app as application  # Assuming 'app' is your Flask instance.

Testing Your Setup

Now, open your web browser and navigate to http://your_domain_or_IP. If everything is set up correctly, you should see your Python web application running!

Conclusion

Congratulations! You’ve successfully installed mod_wsgi on your Ubuntu 24.04 server. With this integration, you can now deploy powerful Python web applications using Apache.

If you have any questions or run into any issues during installation, feel free to comment below, and we’ll be happy to help. Stay tuned for more tutorials and tips on web hosting and development!

Happy coding!