Feb 26 2009
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.
