How To Set Up Apache2 With mod_fcgid And PHP5 On Ubuntu 10.04

Version 1.0
Author: Falko Timme
Follow me on Twitter
Last edited 08/18/2010

This tutorial describes how you can install Apache2 with mod_fcgid and PHP5 on Ubuntu 10.04. mod_fcgid is a compatible alternative to the older mod_fastcgi. It lets you execute PHP scripts with the permissions of their owners instead of the Apache user.

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


1 Preliminary Note

I'm using an Ubuntu 10.04 server in this tutorial with the hostname and the IP address

I will create two Apache vhosts in this tutorial, and, to demonstrate the usage of mod_fcgid.

Make sure you're logged in as root:

sudo su

/bin/sh is a symlink to /bin/dash, however we need /bin/bash, not /bin/dash. Therefore we do this:

dpkg-reconfigure dash

Install dash as /bin/sh? <-- No

In addition to that, we must disable AppArmor:

/etc/init.d/apparmor stop
update-rc.d -f apparmor remove
aptitude remove apparmor apparmor-utils


2 Installing Apache2/mod_fcgi/PHP5

In order to install Apache2, mod_fcgid, and PHP5, run

aptitude install apache2 apache2-suexec libapache2-mod-fcgid php5-cgi

If Apache2 was already installed with PHP5 as an Apache module, disable the PHP5 module now:

a2dismod php5

Then enable the following modules...

a2enmod rewrite
a2enmod suexec
a2enmod include
a2enmod fcgid

... and open /etc/php5/cgi/php.ini:

vi /etc/php5/cgi/php.ini

Uncomment the line cgi.fix_pathinfo = 1:

; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts

Open /etc/apache2/mods-available/fcgid.conf...

vi /etc/apache2/mods-available/fcgid.conf

... and add the line PHP_Fix_Pathinfo_Enable 1 (unlike in previous Ubuntu versions, on 10.04 this line must not go into a <VirtualHost> section because then you would get this error: PHP_Fix_Pathinfo_Enable cannot occur within <VirtualHost> section):

<IfModule mod_fcgid.c>
  AddHandler    fcgid-script .fcgi
  FcgidConnectTimeout 20
  PHP_Fix_Pathinfo_Enable 1

Then restart Apache:

/etc/init.d/apache2 restart


3 Creating Vhosts For And

I will now create two vhosts, (with the document root /var/www/web1/web) and (with the document root /var/www/web2/web). will be owned by the user and group web1, and by the user and group web2.

First we create the users and groups:

groupadd web1
groupadd web2
useradd -s /bin/false -d /var/www/web1 -m -g web1 web1
useradd -s /bin/false -d /var/www/web2 -m -g web2 web2

Then we create the document roots and make them owned by the users/groups web1 resp. web2:

mkdir -p /var/www/web1/web
chown web1:web1 /var/www/web1/web
mkdir -p /var/www/web2/web
chown web2:web2 /var/www/web2/web

We will run PHP using suExec; suExec's document root is /var/www, as the following command shows:

/usr/lib/apache2/suexec -V

root@server1:~# /usr/lib/apache2/suexec -V
 -D AP_DOC_ROOT="/var/www"
 -D AP_GID_MIN=100
 -D AP_HTTPD_USER="www-data"
 -D AP_LOG_EXEC="/var/log/apache2/suexec.log"
 -D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
 -D AP_UID_MIN=100
 -D AP_USERDIR_SUFFIX="public_html"

Therefore we cannot call the PHP binary (/usr/lib/cgi-bin/php) directly because it is located outside suExec's document root. As suExec does not allow symlinks, the only way to solve the problem is to create a wrapper script for each web site in a subdirectory of /var/www; the wrapper script will then call the PHP binary /usr/lib/cgi-bin/php. The wrapper script must be owned by the user and group of each web site, therefore we need one wrapper script for each web site. I'm going to create the wrapper scripts in subdirectories of /var/www/php-fcgi-scripts, e.g. /var/www/php-fcgi-scripts/web1 and /var/www/php-fcgi-scripts/web2.

mkdir -p /var/www/php-fcgi-scripts/web1
mkdir -p /var/www/php-fcgi-scripts/web2

vi /var/www/php-fcgi-scripts/web1/php-fcgi-starter

export PHPRC
exec /usr/lib/cgi-bin/php

vi /var/www/php-fcgi-scripts/web2/php-fcgi-starter

export PHPRC
exec /usr/lib/cgi-bin/php

The PHPRC line contains the directory where the php.ini file is located (i.e., /etc/php5/cgi/ translates to /etc/php5/cgi/php.ini). PHP_FCGI_MAX_REQUESTS is the maximum number of requests before an fcgid process is stopped and a new one is launched. PHP_FCGI_CHILDREN defines the number of PHP children that will be launched.

The php-fcgi-starter scripts must be executable, and they (and the directories they are in) must be owned by the web site's user and group:

chmod 755 /var/www/php-fcgi-scripts/web1/php-fcgi-starter
chmod 755 /var/www/php-fcgi-scripts/web2/php-fcgi-starter
chown -R web1:web1 /var/www/php-fcgi-scripts/web1
chown -R web2:web2 /var/www/php-fcgi-scripts/web2

Now we create the Apache vhosts for and

vi /etc/apache2/sites-available/web1

<VirtualHost *:80>
  DocumentRoot /var/www/web1/web/

  <IfModule mod_fcgid.c>
    SuexecUserGroup web1 web1
    <Directory /var/www/web1/web/>
      Options +ExecCGI
      AllowOverride All
      AddHandler fcgid-script .php
      FCGIWrapper /var/www/php-fcgi-scripts/web1/php-fcgi-starter .php
      Order allow,deny
      Allow from all

  # ErrorLog /var/log/apache2/error.log
  # CustomLog /var/log/apache2/access.log combined
  ServerSignature Off


a2ensite web1

vi /etc/apache2/sites-available/web2

<VirtualHost *:80>
  DocumentRoot /var/www/web2/web/

  <IfModule mod_fcgid.c>
    SuexecUserGroup web2 web2
    <Directory /var/www/web2/web/>
      Options +ExecCGI
      AllowOverride All
      AddHandler fcgid-script .php
      FCGIWrapper /var/www/php-fcgi-scripts/web2/php-fcgi-starter .php
      Order allow,deny
      Allow from all

  # ErrorLog /var/log/apache2/error.log
  # CustomLog /var/log/apache2/access.log combined
  ServerSignature Off


a2ensite web2

Make sure you fill in the right paths (and the correct user and group in the SuexecUserGroup line).

Reload Apache afterwards:

/etc/init.d/apache2 reload

Share this page:

5 Comment(s)

Add comment


From: Anonymous at: 2010-09-11 21:17:28

In case you want to serve static files alongside your PHP files and want to keep web1 and web2 separated from each other, you can add the www-data user (Apache server) to the web1 and web2 groups.

As long as files from either web1 or web2 have group read rights, the Apache server can read and serve the files while keeping web1 and web2 unable to read each others files.

Do not forget to restart the Apache webserver.

Hope this helps someone.

From: Loggy at: 2012-06-12 18:16:25

This seems the best way to do it so then a single script owned by www-data:www-data can be installed in each document root to php with the appropriate changes to the apache2 config file.

I will try this with fcgi because I really don't want to have to change all the sites-enabled virtual host configuration files.  And it *should* improve the memory usage as well.

[All my users have group www-data with permissions 0640, 0660, 2751 or 2771 as appropriate.  This way there is no visibility across users.]





From: Anonymous at: 2011-01-03 13:40:01

Why is this article about mod_fastcgi, when the title states that it is about mod_fcgid?.  Either change the tite, or the content to reflect what the title says - otherwise, you will be wasting peoples time

From: Chris Cameron at: 2010-10-31 16:49:32

just wanted to say this helped me tremendously! thank you!

From: Anonymous at: 2011-05-14 20:02:55

thanks a lot for this tutorial!

a thing I would not recommend is to place php.ini files into /var/www

this is usually the default folder when the server is hit by its ip address. so people could guess the location of the php.ini file and read settings they might should not be able to see.

at least deny access to php.ini files (similar to .ht*) or move the folder into /etc/php5/ or the users home folder.