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
#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:
Create the file httpd.conf inside the /usr/local/www/myapp folder. It will contain virtual host configuration for our application:
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:
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.