Subscribe to RSS feed

splitbrain.org - 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

Compiling

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

Running

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.

Tags:
apache,
portable,
linux,
usb,
stick
Similar posts:
1) If you know a way to avoid this, let me know
2) Not even with this site's help
Posted on Friday, March the 28th 2008 (15 months ago).

Comments?

1
Good job! I'm looking forward to seeing a Linux version of DokuWiki on a Stick.
2008-03-29 00:55:58
Dmitri Popov
2
Don't work here for me. the patch don't work.
isn't it possible to make the ready patched code available?
2008-06-20 13:32:06
markuman
3
Thanks for great HOWTO!
I've found two errors in "Prepare Portable Distribution":
1) "cp $HOME/conf/mime.types conf/" should be "cp $HOME/apache/conf/mime.types conf/"
2) I also had to add "cp $HOME/apache/lib/libexpat.so.0 ." to make server running.
Cheers!
2008-06-29 18:21:24
4
Yes! Thanks! I needed that!
If you are also able to get MySQL in that package; I will be very happy! :)

And by the way, since this is a portable version of Apache with PHP on Linux..
Do you have a download of it, so I only need to unpack in on a USB stick :)
2008-07-27 22:03:46
DJ Wice
5
If your goal is to use this portable apache to run DokuWiki on stick, change :
> ./configure --prefix=$HOME/apache --exec-prefix=$HOME/apache \\
  --enable-mods-shared=all --enable-so

by
> ./configure --prefix=$HOME/apache --exec-prefix=$HOME/apache \\
  --enable-mods-shared=all --enable-so --enable-module=rewrite

Also, since the config file must be different, I renamed the linux conf file httpd_linux.conf and created start/stop scripts :

# START
#!/bin/bash
./httpd -f ./conf/httpd_linux.conf -k start

# STOP
#!/bin/bash
./httpd -f ./conf/httpd_linux.conf -k stop
2008-10-12 00:04:42
Chevdor
6
Is there any way that I can download a ready-to-use version instead of having to compile it?  Thanks.
2008-11-29 12:41:33
mrown
7
If the php config process fails, complaining about "libxml2", you may need to install the libxml2-dev package.
2009-01-22 14:28:23
Fifo
8
Hi I have compiled as it is wrote here but php is not working only apache. No error durring start. Could you help me?
2009-02-19 23:21:53
kani
9
When I try to start Apache by the ./httpd -k start
command, I get the following error message: "Segmentation Fault". Any ideas how to resolve this?
2009-03-09 14:02:23
KV
10
Nice howto, but since the binary of httpd is not linked static, this works only if all needed .so files are installed on the destination machine. E.g. on my ubuntu system there's libexpat.so.1 and on my rhel5 system only libexpat.so.0 is available. To get it really portable you might consider to build a static version of httpd.
2009-04-01 11:11:30
df
CAPTCHA

No HTML allowed. URLs will be linked with nofollow attribute. Whitespace is preserved.