Configuring Apache with mod_ruid2

  • What is mod_ruid2?

    Traditionally there have been 2 basic ways to run PHP under Apache:


    This has traditionally been the fastest (and easiest) way to run PHP - a copy of the PHP interpreter is "embedded" right in the Apache process - this lets Apache handle PHP scripts right inside the same process and memory it is already using, which is fast.

    The downside is that users can easily run into permission issues since the web server is not running under the same user account as the user.

    CGI / FastCGI

    CGI stands for "Common Gateway Interface" and it functions as its name implies - this is a "common" way to get Apache talking to external language interpreters (PHP, Perl, Python, etc.) - it does this over a "gateway interface" - i.e. when Apache receives the request it sees if it has a registered handler for the file type - if it does it passes the script over to the interpreter, gathers the results and sends the output back out to the client.

    The problem is that this is SLOW - every single request causes a new "interpreter" process to be spawned on the server, which slows things down.

    Many hosting providers however use this method since it allows for better isolation of user processes, and generally is a better setup when running different user scripts on the same server (think big shared hosting server with hundreds of users on it), as it uses the underlying Linux file system permissions to keep the user scripts nicely separated.

    There is however another way to run PHP under Apache that gives you the best of both worlds - the speed of mod_php and the security of CGI:


    and as they say (rather humbly):

    "mod_ruid2 is a suexec module for apache which takes advantage of POSIX.1e capabilities to increase performance."

    Note: for those of you wondering why I didn't mention FPM, just hold your horses - I'll be covering FPM setup under the (hopefully coming soon) guide to running Jamroom 5 under Nginx.
  • Download and extract the latest mod_ruid2

    So to get started, download the latest version of mod_ruid2 by doing the following:

    1) click on the green "download" button on this page:

    2) when the download page appears, you can cancel the automatic download - instead, right click on the "Direct Link" URL with your browser and choose "Copy Link Location".

    3) head back to your terminal window (with URL in your clipboard) and run the following as root:


    By just entering "cd" and pressing enter, you will be taken back to the root user's home directory.

    wget -O mod_ruid2.tar.bz2 'the long URL you copied'

    Cut and paste the URL you copied in after the wget command - this will download the latest version of mod_ruid2 and save it as "mod_ruid2.tar.bz2" in the root user's home directory.

    Next, we need to unpack the download ("bz2" is bzip2 compression, and is "unzipped" using bunzip2):

    bunzip2 mod_ruid2.tar.bz2

    tar xvf mod_ruid2.tar

    This will create a new sub directory in the root home directory (i.e. /root/mod_ruid2) that contains the source code for the Apache ruid2 module:

    ls -l

    You should see something like this:
  • Compile mod_ruid2 into Apache

    The next step is to "build" the downloaded ruid2 as an Apache module - this is done using the "apxs2" tool, which is a utility that is part of the Apache software that we previously installed.

    First up - let's cd (change directory) into the directory of the mod_ruid2 we just extracted:

    cd mod_ruid2-0.9.8

    (replace "mod_ruid2-0.9.8" with the actual directory name that was created when you extracted the archive).

    Next comes the "magic" - we're going to use Apache's apxs2 utility to "build" the mod_ruid2 module:

    /usr/bin/apxs2 -a -i -l cap -c mod_ruid2.c

    You will see a quick bunch of lines scroll on your screen as the small module is compiled for Apache:
  • Oh how I love the sweet smell of success
  • That's it - mod_ruid2 has now been compiled as an Apache2 module, and is ready to do it's magic.

    Next up - we're going to configure Apache for virtual hosting which will allow you to run more than one domain on the same server.