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

4 Testing

Now we create a small PHP test file, for example in the www.example1.com web site...

vi /var/www/web1/web/info.php

<?php
phpinfo();
?>

... and call that file in a browser (http://www.example1.com/info.php). If all goes well, the output should look similar to this, and you should see CGI/FastCGI in the Server API line:

 

5 Custom php.ini for Each Web Site

Because each web site has its own php-fcgi-starter wrapper script, it is possible to define different php.ini files for different web sites. To demonstrate this, I will copy the default php.ini (/etc/php5/cgi/php.ini) to the /var/www/web2/ directory and make www.example2.com use the php.ini from the /var/www/web2/ directory:

cp /etc/php5/cgi/php.ini /var/www/web2/
chown web2:web2 /var/www/web2/php.ini

(You can now modify /var/www/web2/php.ini to your likings.)

Then we open /var/www/php-fcgi-scripts/web2/php-fcgi-starter...

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

... and put /var/www/web2/ in the PHPRC line:

#!/bin/sh
PHPRC=/var/www/web2/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/lib/cgi-bin/php

Reload Apache afterwards:

/etc/init.d/apache2 reload

Create a new phpinfo(); file for www.example2.com...

vi /var/www/web2/web/info.php

<?php
phpinfo();
?>

... and call it in a browser (http://www.example2.com/info.php). The Loaded Configuration File line should now show /var/www/web2/php.ini:

 

6 Changing Single PHP Configuration Settings

Instead of passing a whole new php.ini file to your web site, you can as well change single PHP configuration settings in the php-fcgi-starter wrapper script (or use a combination of both) by adding the -d switch to the PHP executable. For example, if I want to disable magic_quotes_gpc for the web site www.example2.com, I'd do it as follows:

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

#!/bin/sh
PHPRC=/etc/php5/cgi/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/lib/cgi-bin/php -d magic_quotes_gpc=off

Reload Apache afterwards:

/etc/init.d/apache2 reload

Then call the info.php script again in a browser (http://www.example2.com/info.php) and search for the magic_quotes_gpc line - it should show Off now:

 

7 Links

Share this page:

5 Comment(s)

Add comment

Comments

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.