install mod_wsgi ubuntu

How to Install mod_wsgi in Apache

mod_wsgi is an Apache server module that provides a WSGI interface to host python applications & sites in Apache. mod_wsgi allows you to run python scripts in Apache server. It is useful for running python websites & applications using Apache web server. In this article, we will look at how to install mod_wsgi in Apache.

How to Install mod_wsgi in Apache

Here are the steps to install mod_wsgi in Apache to run python scripts.

1. Install Prerequistes

Open terminal and run the following command to install prerequisites for mod_wsgi

$ sudo apt-get update 
$ sudo apt-get install apache2 apache2-utils libexpat1 ssl-cert python

2. Install mod_wsgi

Run the following command to install mod_wsgi

$ sudo apt-get install libapache2-mod-wsgi

3. Restart Apache Server

Restart Apache web server to apply changes.

$ sudo systemctl restart apache2

4. Configure mod_wsgi for Apache

For our example, we will create a simple python script to run mod_wsgi. Open terminal and run the following command to create new python file.

$ sudo vi /var/www/html/

Add the following lines to this file.

def application(environ,start_response):
    status = '200 OK'
    html = '\n' \
           '\n' \
           ' mod_wsgi is working\n' \
           '\n' \
    response_header = [('Content-type','text/html')]
    return [html]

In the above code, we have defined a python function that returns a 200 status response code along with a message “mod_wsgi is working”.

Save and close the file.

5. Configure Apache

We need to configure Apache server to serve python script over HTTP.

$ sudo vi /etc/apache2/conf-available/wsgi.conf

Add the following line to wsgi.conf file. We basically mention location of our python script and the URL at which it needs to be served. Please provide full path to your python script.

WSGIScriptAlias /test_wsgi /var/www/html/

With the help of WSGIScriptAlias above, when a user requests /test_wsgi URL on their browser Apache will run the script and return the response. This works like a URL router.

Enable your wsgi script with the following commands.

$ sudo a2enconf mod-wsgi 
$ sudo systemctl restart apache2

6. Test mod_wsgi configuration

Open your web browser and visit http://your_server_or_domain/test_wsgi. Replace your_server_or_domain with your server’s IP address or domain name.

You should see the response message “mod_wsgi is working” indicating that our python script is correctly served at the requested URL.

In this article, we have learnt how to install & configure mod_wsgi to run python scripts in Apache. mod_wsgi is a useful module for running python-based websites & applications from within Apache server. It is a great alternative to mod_python and fastcgi libraries.

We have covered the most commonly used installation steps for mod_wsgi. In this case, we install it as an Apache module. However, please note, since v4, mod_wsgi supports a new type of installation called ‘mod_wsgi express installation’. In this case, you can install mod_wsgi as a python module using or pip utility, instead of installing it as an Apache module. It does not require you to make changes to Apache configuration and is also helpful for python developers who are more familiar with python than Apache.

Also read:

How to Install Memcached in Ubuntu
NGINX Block URL Access
How to Resize Linux Partition Without Data Loss
How to List Directories & Subdirectories in Linux
How to Prevent Cross-Side Scripting in PHP/Apache

Leave a Reply

Your email address will not be published. Required fields are marked *