# Notes to Self: Getting Rapache working in Natty Narwhal (Ubuntu/Linux)

May 27, 2011
By

(This article was first published on Psychwire » R, and kindly contributed to R-bloggers)

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!

My method for getting it working was based on a mix of two other guides. First, the official help guide. Second, this post here. I had to try different bits from each of them.

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.

If you’re an R user who is interested in getting into linux, there’s a great post here by Jeromy Anglim. Also, remember that Narwhals love donuts.

## Installing R

This is the easy part. Add a CRAN source to your sources list:

[CRAN-SERVER]/R/bin/linux/ubuntu natty/

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.

## Installing Apache2

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 wget http://biostat.mc.vanderbilt.edu/rapache/files/rapache-latest.tar.gz rapachedir=tar tzf rapache-latest.tar.gz | head -1 tar xzvf rapache-latest.tar.gz cd \$rapachedir ./configure make make install

## Configuring the Rapache Module

Now we have the fun bit. Begin by firing up a text editor of your choice:

gedit /etc/apache2/mods-available/r.conf

<Location /R>

ROutputErrors SetHandler r-script RHandler sys.source </Location>

<Location /RApacheInfo> SetHandler r-info </Location>

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.

gedit /etc/apache2/mods-available/r.load

LoadModule R_module /usr/lib/apache2/modules/mod_R.so

Great! One last step. Just tell apache to load the module for r:

a2enmod r

And that’s it!

/etc/init.d/apache2 reload

## Testing Time

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.

## Next Steps

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…!