Chrooting Apache2 With mod_chroot On Ubuntu 10.04

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Fri, 2010-08-13 16:25. :: Ubuntu | Apache | Security

Chrooting Apache2 With mod_chroot On Ubuntu 10.04

Version 1.0
Author: Falko Timme <ft [at] falkotimme [dot] com>
Follow me on Twitter
Last edited 06/15/2010

This guide explains how to set up mod_chroot with Apache2 on an Ubuntu 10.04 system. With mod_chroot, you can run Apache2 in a secure chroot environment and make your server less vulnerable to break-in attempts that try to exploit vulnerabilities in Apache2 or your installed web applications.

I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I'm assuming that you have a running Ubuntu 10.04 system with a working Apache2, e.g. as shown in this tutorial: The Perfect Server - Ubuntu Lucid Lynx (Ubuntu 10.04) [ISPConfig 2]. In addition to that I assume that you have one or more web sites set up within the /var/www directory (e.g. if you use ISPConfig).

 

2 Installing mod_chroot

To install mod_chroot, we simply run:

aptitude install libapache2-mod-chroot

Then we enable mod_chroot and restart Apache:

a2enmod mod_chroot
/etc/init.d/apache2 restart

 

3 Configuring Apache

I want to use the /var/www directory as the directory containing the chroot jail. Ubuntu's Apache uses the PID file /var/run/apache2.pid; when Apache is chrooted to /var/www, /var/run/apache2.pid translates to /var/www/var/run/apache2.pid. Therefore we create that directory now:

mkdir -p /var/www/var/run
chown -R root:root /var/www/var/run

Now we must tell Apache that we want to use /var/www as our chroot directory. We open /etc/apache2/apache2.conf, and right below the PidFile line, we add a ChrootDir line:

vi /etc/apache2/apache2.conf

[...]
#
# PidFile: The file in which the server should record its process
# identification number when it starts.
# This needs to be set in /etc/apache2/envvars
#
PidFile ${APACHE_PID_FILE}
ChrootDir /var/www
[...]

Next we must tell our vhosts that the document root has changed (for example, a DocumentRoot /var/www translates now to DocumentRoot /). We can do this either by changing the DocumentRoot directive of each vhost, or more easier, by creating a symlink in the file system.


3.1 First Method: Changing The DocumentRoot

Let's assume we have a vhost with DocumentRoot /var/www. We must now open the vhost configuration of that vhost and change DocumentRoot /var/www to DocumentRoot /. Accordingly, DocumentRoot /var/www/web1/web would now translate to DocumentRoot /web1/web, and so on. If you want to use this method, you must change the DocumentRoot for every single vhost.

 

3.2 Second Method: Creating A Symlink In the File System

This method is easier, because you have to do it only once and don't have to modify any vhost configuration. We create a symlink pointing from /var/www/var/www to /var/www:

mkdir -p /var/www/var
cd /var/www/var
ln -s ../ www


Finally, we have to stop Apache, create a symlink from /var/run/apache2.pid to /var/www/var/run/apache2.pid, and start it again:

/etc/init.d/apache2 stop

ln -s /var/www/var/run/apache2.pid /var/run/apache2.pid
/etc/init.d/apache2 start

That's it. You can now call your web pages as before, and they should be served without problems, as long as they are static HTML files or using mod_php.

If you are using CGI, e.g. Perl, suPHP, Ruby, etc., then you must copy the interpreter (e.g. /usr/bin/perl, /usr/lib/suphp/suphp, etc.) to the chroot jail together with all libraries needed by the interpreter. You can find out about the required libraries with the ldd command, e.g.

ldd /usr/lib/suphp/suphp

root@server1:~# ldd /usr/lib/suphp/suphp
        linux-vdso.so.1 =>  (0x00007fff66dff000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007fa9f4031000)
        libm.so.6 => /lib/libm.so.6 (0x00007fa9f3dae000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007fa9f3b96000)
        libc.so.6 => /lib/libc.so.6 (0x00007fa9f3814000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa9f434b000)
root@server1:~#

If you've copied all required files, but the page still isn't working, you should take a look at the Apache error log. Usually it tells you where the problem is. Also read http://core.segfault.pl/~hobbit/mod_chroot/caveats.html for known problems and solutions.

 

4 Links


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Kramse (not registered) on Tue, 2010-10-26 07:31.

Falko, love your guides!

No need to get rude if there might be some info missing

NOTE: apache-2.2.10 and higher has built in chroot handling and the mod_chroot module should not be used.

Got the above from: https://wiki.ubuntu.com/ModChroot

Submitted by Anonymous (not registered) on Wed, 2011-10-19 16:45.

https://wiki.ubuntu.com/ModChroot:

...And yet another guide that does not work.  The server gives a denied error.

Submitted by Anonymous (not registered) on Tue, 2010-10-19 08:18.
If your apache version is >= 2.2.10 this guide will put you in a sea of sh... troubles ;)

read this and stop cursing: http://www.uno-code.com/?q=node/157

Falko, just a word of advice, FFS INDICATE THE VERSION OF THE SOFTWARE YOU ARE INSTALLING!!!!!!!!!111!!111

Submitted by Anonymous (not registered) on Wed, 2011-09-21 15:39.
My version is Apache/2.2.14 and the guide worked great