How to Install mod_wsgi on Ubuntu


install mod_wsgi ubuntu

mod_wsgi is a useful Apache module that allows you to host Python applications in Apache web server. It supports Python WSGI interface making it suitable for running high-performance production sites in Apache. Here’s how to install mod_wsgi on Ubuntu.

 

How to Install mod_wsgi on Ubuntu

Here are the steps to install mod_wsgi on Ubuntu.

 

1. Install prerequisites & update packages

Log into your Ubuntu server via SSH or open a terminal window and paste the following commands to update system packages, and install python


$ sudo apt-get update
$ sudo apt-get install python libexpat1

 

2. Install mod_wsgi module

You need to install some dependencies before you can install mod_wsgi. Here are the commands to install them


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

If they are already installed on your system, Ubuntu will simply skip their installation.

Install mod_wsgi


$ sudo apt-get install libapache2-mod-wsgi

 

3.  Restart Apache Server

Restart Apache server to apply changes


$ sudo systemctl restart apache2

 

4. Configure mod_wsgi for Apache

Let’s create a sample python script to run within Apache via mod_wsgi. Create a blank new python file


$ sudo vi /var/www/html/wsgi_test_script.py

 

Paste the following lines. Please ensure they are properly indented as shown. Otherwise, python will give en error.


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

 

Save and close the file

 

Next, we need to create a config file to serve this python script over mod_wsgi. Open mod_wsgi’s config file in a text editor.


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

 

Add the following line:


WSGIScriptAlias /test_wsgi /var/www/html/wsgi_test_script.py

Save and close the config file

 

Run the following commands to enable mod_wsgi


$ sudo a2enconf mod-wsgi
$ sudo systemctl restart apache2

 

 

5. Test mod_wsgi

Open your web browser and go to http://your_server_ip/test_wsgi. Replace your_server_ip with your server’s ip or domain name

install mod_wsgi on ubuntu

 

That’s it! You have installed mod_wsgi on Ubuntu.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!