There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to run Python Scripts with Apache and mod_wsgi on Ubuntu 20.04

mod_wsgi is an Apache module that provides an interface for hosting Python based web applications under Apache. It is suitable for use in hosting high-performance production web sites, as well as your average self managed personal sites running on web hosting services. You can easily deploy applications written with frameworks and tools like Django,, Werkzug,, TurboGears, and Flask using mod_wsgi.

In this tutorial, we will learn how to install and set up of mod_wsgi with the Apache server on Ubuntu 20.04 server.


  • A server running Ubuntu 20.04.
  • A root password is configured the server.

Getting Started

Before starting, it is a good idea to update your system with the latest version. You can update your system with the following command:

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

After updating the system, restart it to implement the changes.

Install Python

By default, Python comes pre-installed in Ubuntu 20.04 server. If not installed, you can install it with the following command:

apt-get install python3 libexpat1 -y

Once the installation has been completed, you can proceed to the next step.

Install Apache and mod_wsgi

Next, you will need to install the Apache web server and mod_wsgi Python module on your system. You can install them by running the following command:

apt-get install apache2 apache2-utils ssl-cert libapache2-mod-wsgi -y

Once all the packages are installed, you can proceed to the next step.

Configure Apache for mod_wsgi

Next, you need to create a new Python script inside Apache default web root directory and serve it via mod_wsgi Apache module.

You can create it with the following command:

nano /var/www/html/

Add the following lines:

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' \
    response_header = [('Content-type','text/html')]
    return [html]

Save and close the file. Then, change the ownership of the file to www-data with the following command:

chown www-data:www-data /var/www/html/

Next, create an Apache virtual host configuration file to serve this file over HTTP protocol.

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

Add the following line:

WSGIScriptAlias /wsgi /var/www/html/

Save and close the file. Then, enable mod-wsgi configuration and restart Apache service with the following command:

a2enconf wsgi
systemctl restart apache2

Test mod-wsgi

At this point, Apache web server is configured to serve your Python file over HTTP protocol.

To test it, open your web browser and type the URL http://your-server-ip/wsgi. You should see the mod-wsgi test page in the following screen:

Apache mod_wsgi


Congratulations! you have successfully deployed Python script over Apache web server using mod_wsgi module on Ubuntu 20.04 server. I hope you can now easily deploy any Python application with Apache and mod_wsgi in the production environment.

Share this page:

6 Comment(s)

Add comment

Please register in our forum first to comment.


By: Laksha

Does tis work for Debian 10 ?

By: Jose Laurentino III

Thanks a lot Hitesh,

It worked as expected!

My setup: Ubuntu 20.04 LTS

By: Martin Cendejas

Thanks a lot Hitesh, a good guide.

Just, I suggest addition 2 steps:

1.- After change ownership to, I changed file permission to 775 too

   $ sudo chmod -R 775 /var/www/html/

2.- After enable mod-wsgi configuration, I created a simlink of wsgi.conf to conf-enabled directory:

   $ sudo ln -s /etc/apache2/conf-available/wsgi.conf /etc/apache2/conf-enabled/wsgi.conf

Then, I restarted apache and voula, running perfect.

By: till

The symlink you added manually gets added by the command "a2enconf wsgi", you might have missed executing this command from the tutorial.

By: srinivasan

hi how to do this with my own flask application

i really strugling this would be really helpful!

By: Coding teacher


During the setup I was facing a little issue: Apache Server threw an error because of the type of response returned by the application function.

Here is the trace:

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

So I had to change the return statement by adding encode("utf-8") to the html variable.

- return [html]+ return [html.encode('utf-8')]