Python WebDev from scratch – WSGI

Firstly, using following commands, install mod_wsgi for apache2, and enable it:

$ sudo apt-get install libapache2-mod-wsgi
$ sudo a2enmod mod-wsgi

We installed it, but we need to make apache to recognize “.wsgi” files in the server.

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

Change the “/var/www/” Directory directive, and ExecCGI to OPTIONS, and add a handler for .wsgi, like below:

AddHandler wsgi-script .wsgi

The directive should look like this:

      Options Indexes FollowSymLinks MultiViews ExecCGI
      AddHandler wsgi-script .wsgi
      .....
      .....

And then, add .wsgi index identifier to “/etc/apache2/mods-enabled/dir.conf”. The file should be like this after editing:

DirectoryIndex index.html index.pl index.php index.xhtml index.htm home.wsgi - make it index.wsgi if you want ;) 

And voila. Restart the server, and add a home.wsgi file to the root of your server, with following lines:

def application(environ, start_response):
    # environ - A dictionary, holding the clients info, filled by server - e.g REQUEST_METHOD
    # start_response - A function, which is used to send HTTP headers to the server
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

That’s all folks. Later, I will post more about wsgi applications ;)

part2