Python “Hello World” web application in Ubuntu

This post shows how to setup a python web development environment under Ubuntu and make a hello world example.

So Python has become a hot word in recent years. But before you can do anything about it, you need a “hello world” sample application. In this post, I will build a hello world Python web application under Ubuntu.

1. Install Apache server. This is the web server that will handle HTTP requests.

Try the following command and and open a browser and type “localhost”, if server is up go to step 2.

sudo /etc/init.d/apache2 start

If server is not running, use the following command to install:

sudo apt-get install apache2

2. Install mod_python. This is the module that is used to process .py files.

sudo apt-get install libapache2-mod-python

3. Configure Apache server. (If you configure PHP on Apache server, this is almost the same)

Even now you have installed mod_python in step 2, Apache server does not know how to handle .py files. We need to add a handler to part and make it like the following:

sudo vi /etc/apache2/sites-available/default

(you need to be in sudo mode to be able to edit “default” file!)

4. Write your “hello world” program.

sudo vi /var/www/index.py

def index(req):
  return "Hello World";

It looks like the following, and be careful about indentation.

5. Restart Apache server and run your first Python web application.

sudo /etc/init.d/apache2 restart

You should be able to get your “hello world” web app correctly running.

Congratulations!
You can now start exploring Python world, either by reading a book or google search!

2 thoughts on “Python “Hello World” web application in Ubuntu”

  1. Probably you created a file named index.py in step :
    sudo vi /var/www/index.py

    And in web browser you tried to use test.py.
    Try to use index.py or rename in to test.py 🙂

  2. Hey.. I follow the procedure on 12.04…. But after following the procedure.. when I write localhost/test.py in browser… it says to me open the file or download it

Leave a Comment