Tutorials

Install and Configure mod_wsgi on Ubuntu 16.04

Table of Contents

Introduction

mod_wsgi is an Apache module that provides a standard and efficient method for dynamic web applications to communicate with Apache web servers. mod_wsgi is a simple to use module that can be used to host any Python web application which supports the Python WSGI specification. It is used to deploy applications written with different tools such as Django, TurboGears, and Flask.

In this tutorial, we will demonstrate the installation and set up of mod_wsgi with the Apache web server on Ubuntu 16.04.

Requirements

  • Ubuntu 16.04 server installed on your system.
  • Non-root user account with sudo privilege set up on your system.

Getting Started

Let's start making sure that your Ubuntu 16.04 server is fully up to date. You can update your server by running the following command:

sudo apt-get update -y
sudo apt-get upgrade -y

Installing mod_wsgi

Before starting, you will need to install some prerequisite Apache components in order to work with mod_wsgi. You can install all required components by simply running the following command:

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

Once all of the Apache components have installed, use the curl command to verify the Apache server is responding.

curl http://localhost

You should see the default Ubuntu Apache page.

Now, install mod_wsgi by running the following command:

sudo apt-get install libapache2-mod-wsgi

Restart Apache service to get mod_wsgi to work.

sudo /etc/init.d/apache2 restart

Creating WSGI website

To serve the python application, it is important that Apache forward certain types of requests to mod_wsgi. It is also important to create a python file that tells mod_wsgi how to handle these requests.

You can do this by creating a website for WSGI that will tell Apache the location of python file and setup the file accordingly.

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

Add the following line:

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

Next, create a python test script which you set above.

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

Add the following line:

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

When you are finished save and close the file.

Once you are done with it, you will need to enable the WSGI configuration and restart Apache.

sudo a2enconf wsgi

sudo /etc/init.d/apache2 restart

If everything goes as expected, open your favorite web browser and type the URL http://your-server-ip/test_wsgi and hit Enter, You will get the newly created application:

mod_wsgi Test Page

Summary

You have installed and configured mod_wsgi with Apache and verified a WSGI Python test page. Feel free to comment below if you have any questions or run into any issues following the tutorial.

 
  • Thanks Man , nice one<br/> A small correction<br/> sudo a2ensite wsgi.conf it should be a2enconf wsgi.conf

  • Hello! I tried it but it doesn't work sudo /etc/init.d/apache2 restart[ ok ] Restarting apache2 (via systemctl): apache2.service. $ sudo nano /etc/apache2/conf-available/wsgi.conf $ sudo nano /var/www/html/test_wsgi.py $ sudo a2ensite wsgi.conf ERROR: Site wsgi does not exist!

  • You will just need to add following two line at the end of /etc/apache2/apache2.conf file:

    IncludeOptional conf-enabled/.conf

    IncludeOptional conf-available/.conf

    Next, restart apache service.

  • does not work, do not follow this tutorial.

    You will be stuck at "sudo a2ensite wsgi.conf", and his answer is wrong, it does not solve anything. Don't waste your time. Google other tutorial

  • The tutorial has been updated - the issue with sudo a2ensite wsgi.conf is that it should be using a2enconf as you are enabling a config file, not a site.

    It should work now, as I just finished testing it.

  • TypeError: sequence of byte string values expected, value of type str found

  • Awesome tutorial. have installed from source before and it hurt. This worked without a hitch (Zorin OS 13). Respect and thanks.

  • Great tutorial, thanks.

  • Very useful, work perfectly, thanks for this!

Log In, Add a Comment