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

mod_wsgi is an Apache module that can be used for serving Python scripts over HTTP via Apache web server. You can easily deploy applications written with frameworks and tools like Django, Web.py, Werkzug, Chery.py, 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 18.04 LTS (Bionic Beaver) server.

Requirements

  • A server running Ubuntu 18.04 server.
  • A non-root user with sudo privileges.
  • A static IP address 192.168.43.229 configure on your server.

Install Apache and mod_wsgi

Before starting, you will need to install some required packages to your system.

You can install all of them by running the following command:

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

Once all the required packages are installed, you can proceed to install mod_wsgi with the following command:

sudo apt-get install libapache2-mod-wsgi -y

Configure Apache for mod_wsgi

Next, you will need to create a python script inside the Apache web root directory to serve via mod_wsgi Apache module.

You can do this with the following command:

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

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

Save and close the file. Then, give proper permissions to wsgi.py file:

sudo chown www-data:www-data /var/www/html/wsgy.py
sudo chmod 755 /var/www/html/wsgy.py

Next, you will need to configure Apache to serve this file over HTTP protocol. You can do this by creating wsgi.conf file:

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

Add the following lines:

WSGIScriptAlias /wsgi /var/www/html/wsgy.py

Save and close the file. Then, give proper permissions to wsgi.py file:

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

sudo a2enconf wsgi
sudo systemctl restart apache2

Test Python scripts in Apache with mod-wsgi

Now, open your web browser and type the URL http://example.com/wsgi. You will be redirected to the following page:

Python Test page

Share this page:

Suggested articles

11 Comment(s)

Add comment

Comments

By: besmir

replace: WSGIScriptAlias /wsgi /var/www/html/wsgi.py

with

WSGIScriptAlias /wsgi /var/www/html/wsgy.py

By: Simon Lee

 If wsgi configuration file name is "wsgi.conf", shouldn't "sudo a2enconf mod-wsgi" be "sudo a2enconf wsgi"?

By: till

Yes, it should be wsgi and not mod-wsgi. I've updated the tutorial.

By: mando

sudo a2enconf wsgi --> a2enmod

I initialy failed with this, receiving a 'TypeError: sequence of byte string values expected, value of type str found' in the error log

expressing the string as bytes, prefixing the string with html = b'the output' works but needs every line as the next error was

SyntaxError: cannot mix bytes and nonbytes literals

typecasting the whole output to bytes before output worked beautifly though

html = bytes(html, encoding= 'utf-8')

Thanks for a nice cleare turorial

I was using python3 and ibapache2-mod-wsgi-py3 so I initialy failed with this, receiving a

TypeError: sequence of byte string values expected, value of type str found' in the error log

expressing the string as bytes, prefixing the string with html = b'the output' works but needs every line as the next error was

SyntaxError: cannot mix bytes and nonbytes literals

typecasting the whole output to bytes before output worked beautifly though

html = bytes(html, encoding= 'utf-8')

Thanks for a nice clear turorial

By: noob

didnt work for me error: 

Internal Server Error

why its like that?

By: till

Take a look into the error.log file, there you should be able to find out why.

By: Jiri

Thanks,

works perfect with Ubuntu 18

By: HasH

Hey Im a total noob here, So im following the tutorial you showed here and when i do nano /var/www/html/wsgy.py it opens the file and then when i write something in it and close it, it shows that the file isnt writeable. Ive given it permissions as well but its not working.. any suggestion would be really helpful

 

By: Dennis G Allard

I also get an Internal Server Error.

My apache error log shows nothing.

My apache access log shows nothing.

I have banged my head against the wall and am unable to get this to work.

In addition to your site  (which neglects to mention the need to run a2enmod wsgi) I also tried to follow, verbatim, the steps in:

https://www.youtube.com/watch?v=q__Nn0RRBvE

Nothing works.  At one point, instead of "Internal Server Error" I got a different  error:

Not Found -- The  requested URL was not found on  this server

Yet, again, no output to my apache error log or access log files which are located on  my Ubuntu 18 server at:

/var/log/apache2/error.log

/var/log/apache2/access.log

 

Any ideas?

Dennis Allard