Example:
http://www.lll.lu/~alain/myscript.cgi would be executed as
user alain rather than wwwrun. Hence, the script
could not spy on private data of
http://www.lll.lu/~transfai/petition.cgi belonging to a
different user.
<VirtualHost *>
Servername alain.hitchhiker.org.lu
SuexecUserGroup alain users
DocumentRoot "/srv/www/htdocs/alain"
</VirtualHost>
It does not usually work for sites kept under a user's private root:
<VirtualHost *>
Servername alain.hitchhiker.org.lu
SuexecUserGroup alain users
DocumentRoot "/home/alain/public_html"
</VirtualHost>
In order to allow putting virtual hosts into a user's personal directory, apply the mod_suexec.patch, and then use the SuexecUserdir directive to set the user name:
<VirtualHost *>
Servername alain.hitchhiker.org.lu
SuexecUserdir alain users
DocumentRoot "/home/alain/public_html"
</VirtualHost>
N.B. the patch has been submitted to the Apache bugzilla as a feature
request
(number 43652), and may be downloaded as attachment 22641.
Alternatively, if you don't feel like patching existing Apache code, you can download the mod_mysuexec module together with mod_suexec.h, which can be compiled using:
apxs2 -c mod_mysuexec.c
After compiling this, copy the resulting
.libs/mod_mysuexec.so
file to your Apache modules
directory (/usr/lib/apache2/modules/ on Ubuntu), and use it
using the SuexecUserdir
directive.Moreover, the mod_mysuexec module supports its SuexecUserdir also in a <Directory> context, in addition to <VirtualHost>.
In order to allow users to safely execute PHP scripts, this paradigm must be changed: rather than using mod_php, user's PHP scripts should be executed via php-cgi. There are two steps necessary to make this happen:
<Directory /home>
php_admin_flag engine off
</Directory>
Using php_admin_flag rather than simply php_flag
makes sure that users can't re-enable php using their
.htaccess file.
Unpack this and compile it:
tar xfzv suphp-SNAPSHOT-2008-03-31.tar.gz
cd suphp-SNAPSHOT-2008-03-31
./configure --with-apxs=/usr/bin/apxs2 --with-setid-mode=owner
make
make install
and activate it by putting the following into Apache's config:
LoadModule suphp_module /usr/lib/apache2/modules/mod_suphp.so
<Directory /home>
AddHandler application/x-httpd-php .php .php3 .php4 .php5 .phtml
suPHP_AddHandler application/x-httpd-php
suPHP_Engine on
</Directory>
and the following in /usr/local/etc/suphp.conf:
[global]
webserver_user=www-data
docroot=/var/www:${HOME}/public_html
check_vhost_docroot=false
[handlers]
;Handler for php-scripts
application/x-httpd-php="php:/usr/bin/php-cgi"
<Directory /home>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler cgi-script
SetEnv REDIRECT_STATUS 1
</FilesMatch>
</Directory>
In this mode, all PHP script need to have their execute bit set:
find /home -name '*.php' -print0 | xargs -0 chmod u+x
The suexec binary usually only handles executables or scripts
specifying their interpreter using a hash-bang line. Such a line is
usually not specified in PHP scripts. Fortunately, on Linux, you can
specify the interpreter using /proc/sys/fs/binfmt_misc
echo ':PHP:E::php::/usr/bin/php-cgi:' > /proc/sys/fs/binfmt_misc/register
The REDIRECT_STATUS variable is needed as a security measure
to prevent php-cgi from being called directly accidentally,
with parameters supplied by a potentially malicious web site
visitor. So make sure to never set this variable on a directory tree
that contains the php-cgi executable itself. But do set it on
a directory that contains scripts that are interpreted by
php-cgi.
<?
print "hello world<p>\n";
system("id");
?>