Configure Apache with mod_wsgi in Linux server

lamp-linux-apacheThis tutorial provides step by step guide to configure mod_wsgi along with Apache in Linux server. I am using Debian 6 Squeeze and Apache2. mod_wsgi is fast, more actively maintained and better performing that mod_python. Thus, it would be the best idea to choose mod_wsgi to run your python web apps. However, if you have some obligation to use mod_python see the step by step tutorial to configure Linux server with Apache2 and mod_python: configure apache with mod_python.

Install Apache:

For Debain/Ubuntu run:

sudo apt-get install apache2 apache2-doc apache2-utils

Install mod_wsgi:

For Debian/Ubuntu user:

sudo apt-get install libapache2-mod-wsgi

There are two types of mode in mod_wsgi:

1) Embedded mode:

This is also the default mode. Whenever embedded mode is used you have to restart the whole server after changing the code. Embedded mode is highly inappropriate.

2) Daemon Mode:

Daemon mode avoids necessity to restart the whole server when code are changed. Thus, we will always want to set up the Daemon mode when configuring apache with mod_wsgi.

Configure Apache:

Edit the virtualhost(vhost) configuration file:

Go to terminal and open the file as:

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

Edit the file to look like this:

<VirtualHost *:80>

ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster@example.com

WSGIDaemonProcess myapp processes=2 threads=3 display-name=%{GROUP}
WSGIProcessGroup myapp

DocumentRoot /var/www/wsgi-scripts

<Directory /var/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi

<Directory /var/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

Note: You should substitute the paths and the hostnames appropriate to your system.

Here’s a sample app taken from Google code:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

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

    return [output]

Save this file as myapp.wsgi. I would save in /var/www/wsgi-scripts.
Run the apache server:

sudo /etc/init.d/apache2 start

Run the application is your web browser:

http://127.0.1.1/myapp

That’s it. You will see the ouput as:

"Hello world!"

Final Conclusion: This tutorial is tested with Debian 6 however, it should work  on any Linux distribution.  The .wsgi file must have read permission so that the file can be executed and also don’t forget to substitute the paths and hostnames appropriate to your system in the virtual host configuration file.

0saves
Posted in Debian, Linux, Ubuntu, Ubuntu 11.10, Ubuntu 12.04, Ubuntu One | Tagged , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>