electronic brain surgery since 2001

Compiling a Portable Apache for Linux

There are several projects that offer precompiled and portable distributions of the Apache webserver for use on a USB stick. But I couldn't find any that would work on a Linux system.

Today I spent some time to figure out how to compile a relocatable Apache with PHP support. Yes, this might result in a Linux version of the DokuWiki on a Stick project. Read on for detailed instructions.

Downloading and Patching

I assume all of the following steps are done in your home directory, adjust paths if needed.

First download and unpack Apache and PHP.

$> wget http://de2.php.net/get/php-5.2.5.tar.bz2/from/this/mirror
$> tar -xjvf php-5.2.5.tar.bz2
$> wget http://apache.eu.lucid.dk/httpd/httpd-2.0.63.tar.bz2
$> tar -xjvf httpd-2.0.63.tar.bz2

To avoid any fixed paths in Apache, two files have to be slightly modified1). This will make Apache look for its config in a directory relative to the main binary. Here is patch: apache-2.0.63.patch

$> cd $HOME/httpd-2.0.63
$> patch < $HOME/apache-2.0.63.patch


Time to compile Apache and PHP. Apache goes first:

$> cd $HOME/httpd-2.0.63
$> ./configure --prefix=$HOME/apache --exec-prefix=$HOME/apache \\
   --enable-mods-shared=all --enable-so
$> make
$> make install

Next is PHP:

$> cd $HOME/php-5.2.5
$> ./configure  --prefix=$HOME/php --with-apxs2=$HOME/apache/bin/apxs
$> make
$> make install

Prepare Portable Distribution

The first part is pretty straight forward. Just create a dir and copy the files we're interested in.

$> mkdir $HOME/portable
$> cd $HOME/portable
$> mkdir conf
$> mkdir htdocs
$> mkdir logs
$> cp $HOME/apache/bin/httpd .
$> cp $HOME/apache/lib/libapr-0.so.0 .
$> cp $HOME/apache/lib/libaprutil-0.so.0 .
$> cp -r $HOME/apache/modules/ .
$> cp $HOME/conf/mime.types conf/
$> cp $HOME/php-5.2.5/php.ini-recommended conf/php.ini

The next thing took me a lot of time to figure out. The httpd binary makes use of some shared libraries. The location (rpath) of these libraries is compiled into the binary. That's bad of course for a portable environment. Luckily the dynamic loader of Linux supports a macro called $ORIGIN to reference a path relative to the binary itself. However I wasn't able to pass $ORIGIN correctly to the LDFLAGS environment to be used correctly by the Apache build process2).

The only way to fix this problem I found, was to change the rpath afterwards. This can be done by a tool called chrpath:

$> sudo apt-get install chrpath
$> chrpath -r '$ORIGIN' $HOME/portable/httpd

Finally a simple config file for Apache is needed, here is a very minimal version of $HOME/portable/conf/httpd.conf:

ServerName portableapache
Listen 8080
DocumentRoot htdocs
ServerAdmin webmaster@nowhere.com
LoadModule access_module modules/mod_access.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule mime_module modules/mod_mime.so
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php .php3
PHPIniDir conf
AccessFileName .htaccess
IndexIgnore readme .htaccess
KeepAlive on
KeepAliveTimeout 15
TimeOut 30
DirectoryIndex index.html index.htm index.php


You now can start and stop your Apache with

$> cd $HOME/portable
$> ./httpd -k start
$> ./httpd -k stop

Of course you might want to move the $HOME/portable dir to your USB stick. The webserver will be available at http://localhost:8080.

apache, portable, linux, usb, stick
Similar posts:
If you know a way to avoid this, let me know
Not even with this site's help