Skip to content

How to install and configure mod_wsgi on FreeBSD 8.2

2012 January 21
tags: ,
by Richard Knop

I will cover installation and configuration process of mod_wsgi on a fresh installation of FreeBSD 8.2. After that I will show you how to create a Hello World Python web application using the just installed and configured mod_wsgi.

There are multiple ways to use Python for web development (for example, mod_python) but mod_wsgi is recommended as the best approach in the Python documentation.

I have set up a fresh installation of FreeBSD 8.2 for i386 platform in VirtualBox on my MacBook. You can get FreeBSD here.

Enough talking, let’s start. First, let’s update the port collection. Run these commands:

#portsnap fetch
#portsnap extract
#portsnap update

Ok. Let’s install Apache:

#cd /usr/ports/www/apache22
#make config

Make sure THREADS option is selected.

#make
#make install

Make sure Apache is started with every reboot:

#ee /etc/rc.conf
apache22_enable="YES"

You will also edit one more file:

#ee /boot/loader.conf
accf_http_load="YES"

Now the important part. Let’s install mod_wsgi:

#cd /usr/ports/www/mod_wsgi3
make
make install

Cool. Let’s create a directory for our hello world application (and a public directory inside it – that will be internet accessible directory where you can put your stylesheets or javascript files):

#mkdir /usr/local/www/myapp
#mkdir /usr/local/www/myapp/public
#cd /usr/local/www/myapp

Let’s create a virtual host in the directory. Before editing httpd.conf file, make sure to make a backup in case something goes wrong:

#cp /usr/local/etc/apache22/httpd.conf /usr/local/etc/apache22/httpd.conf.backup

After that, remove or comment out ServerRoot, DocumentRoot and their corresponding Directory Apache directives and add this line on the bottom of the the /usr/local/etc/apache22/httpd.conf file:

Include /usr/local/www/myapp/httpd.conf

Create the file httpd.conf inside the /usr/local/www/myapp folder. It will contain virtual host configuration for our application:

#ee /usr/local/www/myapp/httpd.conf

Add this inside the just created httpd.conf:

WSGIPythonPath /usr/local/www/myapp

<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost

  DocumentRoot /usr/local/www/myapp/public

  <Directory /usr/local/www/myapp/public>
  Order allow,deny
  Allow from all
  </Directory>

  WSGIScriptAlias / /usr/local/www/myapp/myapp.wsgi

  <Directory /usr/local/www/myapp>
  Order allow,deny
  Allow from all
  </Directory>
</VirtualHost>

There are few important lines:

  • WSGIPythonPath will be added to sys.path. Make sure to set this variable in httpd.conf so you can load modules and packages in Python.
  • WSGIScriptAlias is an URLI alias that will tell Apache to call the /usr/local/www/myapp/myapp.wsgi. I have put there just a forward slash so http://localhost/ will be the URI which will set off the myapp.wsgi script. If I were to put /myapp there, http://localhost/myapp would be the URI which would set off the wsgi script.

You might be wondering what should the /usr/local/www/myapp/myapp.wsgi script look like. Here is a simplest example:

import wsgiref

def application(environ, start_response):
    response_status = '200 OK'
    response_body = 'Hello!'
    response_headers = []
    content_type = ('Content-type', 'text-plain')
    content_length = ('Content-Length', str(len(response_body)))
    response_headers.append(content_type)
    response_headers.append(content_length)
    start_response(response_status, response_headers)
    return [response_body]

Now restart Apache:

#/etc/local/sbin/apachectl restart

Go to http://localhost/ and enjoy your first working Python WSGI app ;)

Of course, substitute localhost for your IP address if you are not working on your local development machine. If you are using VirtualBox, find out the VirtualBox instance IP address with ifconfig and use that.

5 Responses leave one →
  1. Bill permalink
    February 14, 2012

    Thanks, this is very useful!

    I installed my website into a Jail created using Ezjail. This meant that I was not able to add the line
    accf_http_load=”YES”
    to the file /boot/loader.conf because this file is just a link to the file in the basejail (this is part of the Ezjail configuration). The Python WSGI app works, though, even across a machine reboot.

    Bill

Trackbacks and Pingbacks

  1. How to install and configure Django with mod_wsgi on FreeBSD 8.2 | Zend Framework Blog
  2. Install PostgreSQL on FreeBSD 8.2 and make it work with Django | Zend Framework Blog
  3. Install PostgreSQL on FreeBSD 8.2 and make it work with Django … | Programmer Solution
  4. Django missing admin CSS stylesheets | Zend Framework Blog

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS