Running Python Scripts on Apache with mod_wsgi: A Guide for Ubuntu Users

mod_wsgi is a powerful Apache module designed to facilitate the deployment of Python applications on the web through the Apache server. By leveraging mod_wsgi, developers can efficiently serve web applications built with popular frameworks such as Django, Web.py, Werkzeug, CherryPy, TurboGears, and Flask.

In this guide, we will explore the steps required to install and configure mod_wsgi on an Ubuntu 18.04 LTS (Bionic Beaver) server running Apache.

Requirements

  • A server running Ubuntu 18.04 LTS.
  • A non-root user with sudo privileges.
  • A static IP address configured on your server, for example, 192.168.43.229.

Install Apache and mod_wsgi

To get started, you need to install some essential packages. Run the following command to install them:

sudo apt-get install python libexpat1 apache2 apache2-utils ssl-cert -y

Once the necessary packages are in place, proceed with installing mod_wsgi using this command:

sudo apt-get install libapache2-mod-wsgi -y

Configure Apache for mod_wsgi

We will now create a Python script within Apache’s web root directory to be served by the mod_wsgi Apache module. Execute the following command:

sudo nano /var/www/html/wsgy.py

Add the following script content:

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' \
           'Welcome to mod_wsgi Test Page\n' \
           '</div>\n' \
           '</body>\n' \
           '</html>\n'
    response_header = [('Content-type', 'text/html')]
    start_response(status, response_header)
    return [html]

Save and close the file. Next, ensure the correct permissions are applied to the file:

sudo chown www-data:www-data /var/www/html/wsgy.py
sudo chmod 755 /var/www/html/wsgy.py

Now, configure Apache to serve this Python script over HTTP by creating a configuration file:

sudo nano /etc/apache2/conf-available/wsgi.conf

Insert the following configuration:

WSGIScriptAlias /wsgi /var/www/html/wsgy.py

After saving and closing the file, enable the mod_wsgi configuration and restart the Apache service:

sudo a2enconf wsgi
sudo systemctl restart apache2

Test Python Scripts in Apache with mod_wsgi

To verify your setup, open a web browser and enter the URL http://example.com/wsgi. A successful configuration will display the python test page.

Links

FAQ

What is mod_wsgi?

mod_wsgi is an Apache HTTP Server module that enables hosting of Python-based web applications, integrating seamlessly with Apache to serve Python scripts efficiently.

Why should I use mod_wsgi?

Mod_wsgi offers ease of deployment for Python applications on web servers, provides high performance and scalability, and integrates with existing Apache infrastructure easily.

Is there an alternative to mod_wsgi for serving Python applications?

Yes, other options include Gunicorn, uWSGI, or using a platform like Docker to containerize applications along with their web servers, such as Nginx or Apache.

Can I use mod_wsgi with Python 3?

Yes, mod_wsgi supports Python 3. Ensure that the relevant Python 3 packages and libraries are installed on your server system.

How can I troubleshoot issues with mod_wsgi?

You can check the Apache error logs for any output related to mod_wsgi errors, look at the configuration files for mistakes, ensure all necessary Python packages are installed, and verify permissions on script files.