In this post, I cover my experiences in getting Rapache working in the latest version of Ubuntu (11, Natty Narwhal). I was unable to find an example that managed to get the whole thing working, so here is what I did. I’m not going to pretend to be a power user of Apache or Linux here. Most of my experience comes from playing with Apache and breaking it horribly. Some people might say that’s the best way to learn. If anyone has comments on how to improve how I did it, please share, as I’m really keen to hear!
For all of the code snippets below, I’ll assume you’re logged into the terminal as root. If you’re not, don’t forget to prefix stuff with sudo to allow you to run the commands as the root user.
This is the easy part. Add a CRAN source to your sources list:
Naturally, replace [CRAN-SERVER] with your CRAN server of choice. For this, you’ll need to install both r-base and r-base-dev packages.
Next you need to install the server itself. This is straightforward:
apt-get install apache2
Installing Rapache and extra packages for Apache2
This one is easy again – it’s in the manual!
apt-get install r-base-dev apache2-mpm-prefork apache2-prefork-dev
rapachedir=`tar tzf rapache-latest.tar.gz | head -1`
tar xzvf rapache-latest.tar.gz
Configuring the Rapache Module
Now we have the fun bit. Begin by firing up a text editor of your choice:
I added the following lines:
But, what do they do? As I understand, the first Location tells Apache to treat any file in the R directory (which I created within the web root directory) as an R script and run it through R. The second Location directive, with /RApacheInfo, is again from the manual. If you use that, and head to 127.0.0.1/RApacheInfo you’ll get a testing page to show that your setup is working just fine.
Next we create the r.load file.
And then add the following:
LoadModule R_module /usr/lib/apache2/modules/mod_R.so
Great! One last step. Just tell apache to load the module for r:
And that’s it!
Now restart your apache server:
Assuming there are no errors, you can now test your server. If you head to 127.0.0.1/RApacheInfo, you should hopefully see a test page, which means you can do what I did and punch the air with success (embarrasing I know).
First Rapache Script
Let’s just do something dead simple for now:
x = rnorm(100, mean=5, sd=3) print(x)
Then, if you save it in the R directory, you should see the output you’d expect if you had typed these commands into the terminal.
The next steps are the bread and butter of making interesting web apps – gets and posts, passing data to scripts, and doing interesting stuff.
I currently don’t seem to be able to get the server to load newly installed libraries – not sure why as they are installed. The mission continues…!