Configuring WSGI on Ubuntu/Centos

Posted on by John Long | Updated:
Reading Time: 3 minutes

This article outlines the process of configuring a Dedicated server for Python 3 web applications with Apache 2.4 using mod_wsgi. 

What is mod_wsgi?

Mod_wsgi is an Apache module that allows Python web applications to function on a server. This module provides a web framework for Flask, Django, and other Python based frameworks to operate within a clustered server environment on a group of servers.


If you do not have the above prerequisites installed, this build will not work as expected. The following commands can be used to install the prerequisites

apt install apache2 apache2-dev python3 python3-dev python3-pip

pip3 install virtualenv

Step 1

Begin by downloading the source files for mod_wsgi.i

root@host [~]# wget

Next, unpack the archive

root@host [~]# tar -xzvf 4.6.5.tar.gz

Enter the source files directory

root@host [~]# cd mod_wsgi-4.6.5

Next, determine the path to the Python interpreter.

root@host [~]# which python3
root@host [~]# 

Configure the installation and define the Python interpreter for mod_wsgi.

root@host [~]# ./configure --with-python=/usr/bin/python3
checking for apxs2... no
checking for apxs... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/usr/src/mod_wsgi-4.6.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

Now, run the following commands to compile the package.

root@host [~]# make && make install

The path to where the module was installed will be at the end of the output from the last command.

root@host [~]# /usr/lib/apache2/modules/

Use the path noted above to load the module into Apache by adding the following snippet to the bottom of the apache.conf or httpd.conf

LoadModule wsgi_module /usr/lib/apache2/modules/

Now test the syntax and restart the Apache service on the cloud server.

Debian/Ubuntu based systems:

root@host [~]# apache2 -t
root@host [~]# service apache2 restart

RHEL/CentOS based systems:

root@host [~]# httpd -t
root@host [~]# service httpd restart

Step 2

Now that mod_wsgi has been installed, let’s take a look at the configuration. We will begin by creating a basic Python web application to detail the steps to configure mod_wsgi.

First, let's create a virtual environment using the Python version that mod_wsgi was configured for. For this article, the example application will be placed in /var/www/. Next, we will run this command to create the directory wsgi_test, which will contain the virtual environment.

root@host [~]# virtualenv --python=python3 wsgi_test

Next, enter this directory and activate the virtualenv.

root@host [~]# cd wsgi_test
root@host [~]# source bin/activate
root@host [~]# pip install flask
root@host [~]# touch
root@host [~]# vim

Place the following configuration inside of the file

from flask import Flask
 app = Flask(__name__)
 def index():
     return "Hello, from WSGI"

Now, let's create the wsgi file to load the application.

root@host [~]# touch
root@host [~]# vim

Next, add the following information to the file. This data will activate the virtual environment when a request is received and execute the application.

activate_this = '/var/www/wsgi_test/bin/'
 with open(activate_this) as file_:
         exec(, dict(__file__=activate_this))
 import sys
 sys.path.insert(0, '/var/www/wsgi_test/')
 from wsgi_test import app as application

Now that the example application has been created, let’s configure Apache.

We will begin by creating a virtual host for this application. This file will be placed inside of either
/etc/httpd/conf.d/ for RHEL based systems, or
/etc/apache2/sites-enabled/for Debian based.


For RHEL based systems, write the file to the following path replacing with your domain.



For Debian based systems, write the file to the following path, replacing with your domain.


The contents of the file will be as follows, replacing with your domain.

<VirtualHost *:80>
     WSGIDaemonProcess wsgi_test user=www-data group=www-data threads=2
     WSGIScriptAlias / /var/www/wsgi_test/
     <Directory /var/www/wsgi_test>
         Require all granted

For RHEL based systems, replace the user and group with apache as follows:

WSGIDaemonProcess wsgi_test user=apache group=apache threads=2

Now test and restart Apache.

Debian based systems:

root@host [~]# apache2 -t
root@host [~]# service apache2 restart

RHEL based systems:

root@host [~]# httpd -t
root@host [~]#  Service httpd restart

You can also use the curl command via the CLI

root@host [~]# curl

or navigate directly to the domain in a browser to see your application. Any errors will be logged to Apache’s default error log file usually found in /var/log/apache2/error_log

Ready to get started?

Liquidweb offers multiple hosted private cloud server options for setting up and configuring this type of environment to run your apps. Reach out to one of our Solutions Team members today, and they can put together a hosting package that will definitely meet your specific needs.

You can also give us a call 24 hours a day 1.800.580.4985 or open a chat or ticket with us.
Series Navigation
<< Previous ArticleNext Article >>
Avatar for John Long

About the Author: John Long

Latest Articles

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article