Feb 26
Configure web2py to run behind apache with WSGI (mod_wsgi)
Web2py
Install web2py
First, what you need is to install web2py. This could be as simple as download/unzip (in this case do it yourself), but I think a better option is to make revision control system do the “update job” for us
Since I’m a bazaar user and web2py has a branch on launchpad let’s use it. The branch is a development branch, so check the log to see a revision which is marked as release. At the time of writing revision 555 is marked as release 1.56.4 in the logs:
555. By Massimo Di Pierro <email address hidden> on 2009-02-23
1.56.4
So cd in the directory where you wish to store you web2py applications and get the code (remember the revision we checked above)
$ cd /var/www/
$ bzr branch -r 555 lp:~mdipierro/web2py/devel web2py
Next time you want to upgrade web2py to the next release simply check the new revision and pull your changes.
$ cd /var/www/web2py/
$ bzr pull -r XYZ
Configure web2py
You need to chown the directory so that the webserver user can write *.pyc files.
You can override the user in the configuration, so you basically have to choices:
- The user running apache (apache on gentoo, www-data on ubuntu)
- Use your user (useful only for development, not suitable in production)
Since here we are talking about production, let’s take the first option
$ groupadd web2py
$ chown -R apache:web2py /var/www/web2py
$ chmod -R g+w /var/www/web2py
The web2py group is optional, of course.
Now start web2py “normally” so that it create required files. Insert a password for the admin page, and remember it. Stop web2py by hitting CTRL+C
$ python web2py
...
...
CTRL+C
Now, it should be present two files in the web2py directory:
- options_std.py
- parameters_8000.py
Edit options_std.py and ensure that the line
password = '<recycle>'
is present (it really should)
Create some symlinks:
ln -s options_std.py options.py
ln -s parameters_8000.py parameters_80.py
ln -s parameters_8000.py parameters_443.py
Ok, we’re done with web2py configuration, let’s concentrate on apache and mod_wsgi.
Apache
Installation
Apache install it’s not covered here. Apache and apache ssl is assumed to work before following these istructions
Install required modules
Install mod_wsgi and enable it
On Ubuntu:
$ sudo apt-get install libapache2-mod-wsgi
$ sudo a2enmod wsgi
On Gentoo:
sudo paludis -i www-apache/mod_wsgi
sudo vim /etc/conf.d/apache2
--> add -D WSGI to APACHE2_OPTS variable
Configure vhost
Web2py with wsgi require that you run it in it’s own vhost.
For this to work you have to use a different domain from “localhost”
A dirty hack is to add a new domain name for localhost in /etc/hosts and use it for web2py
open /etc/hosts and add an alias for 127.0.0.1. Obviously on production system you should not have this problem
and you can use a “real” domain name
127.0.0.1 ... ... web2py.localhost
“… …” is not really there in the file, it’s just a placeholder for things already written on that row.
Next, create a new vhost file:
Ubuntu
vim /etc/apache2/sites-available/web2py
Gentoo
vim /etc/apache2/vhost.d/web2py.conf
Vhost file
In the editor, write the following in this file
** CHECK ALL PATHS, SPECIALLY FOR CERTIFICATES **
sample_vhost.conf
OK, almost done. On ubuntu enable the vhost
sudo a2ensite web2py
next, (on ubuntu) add
NameVirtualHost *:443
after the line
<IfModule mod_ssl.c>
in file /etc/apache2/sites-enabled/default-ssl
then restart apache (do not reload, need restart)
sudo /etc/init.d/apache2 restart
go to http://web2py.localhost/ and you should see the welcome page.
https://web2py.localhost/ should work too.
Next article will introduce a bit of mod_rewrite work to manage default application on a per-domain base.

April 16th, 2009 at 10:55 pm
Thanks for the great write-up, exactly what I was looking for.
I’m looking forward to the next entry regarding mod_rewrite to manage web2py applications on a per domain basis. If you’re not planning to write the next one anytime soon, could you point to resources on how to go about this?
Cheers,
-Seth
April 17th, 2009 at 8:07 am
Well, I completely forgot to update this post :p
The fact is that the rewrite rules I’ve written are sub-optimal, but maybe can be still of help for someone…
I’ll try to find the time to write something about that after lunch
April 22nd, 2009 at 9:17 am
Thanks for this great guide. It helped me set up web2py with apache, mod_wsgi and ssl support.
April 22nd, 2009 at 11:37 am
Thank you for taking the time to write this up! I was banging my head against a wall for hours trying to get web2py up and running with Apache and WSGI.
The crucial change I had to make to the configuration was in removing the Alias directive and replacing it with DocumentRoot as in your setup:
### alias the location of applications (for static files)
#Alias / /home/web2py/applications/
DocumentRoot /home/web2py
Thanks again, and happy hacking!
June 4th, 2009 at 10:29 pm
I really liked this post. Can I copy it to my site? Thank you in advance.
July 6th, 2009 at 2:37 am
Awesome blog! Subscribed on rss. Regular will read it. Good job.
July 6th, 2009 at 1:58 pm
Excellent blog! Very interesting themes. I will regularly read it.