Code snippets, tech tricks and other bits and bobs

« Back to blog

cgi_wrapper: Per-domain php.ini under php-fastcgi (Plesk 9.5.2 + CentOS 5)

I recently discussed per-directory php directives utilising htscanner, allowing setting values with php_flag and php_value in .htaccess files.

While this is an enormous step forward in term of granular control for the webmaster, it doesn't allow complete control as some things don't work when set in .htaccess as I discovered when trying to set apc config values.

The problem stems from being unable to use vhosts.conf to specify php_admin_value as you would running PHP as an Apache module, and having to resort to other methods. I did attempt http://kb.parallels.com/9059, but it was a complete failure, because Paralells neglect to mention that you must be running Plesk 9.5.2+ in order to utilise cgi_wrapper as directed.

I had been avoiding updating Plesk, as last time I'd done so it had mangled quite a few things, but I bit the bullet, and this time the process seems to have been entirely successful.

From http://kb.parallels.com/9059

The hotfix is as follows:

It is possible to use custom php.ini for domains if php as FastCGI mode is used on it. 
It is necessary to apply hotfix. Fix is compatible with Parallels Plesk Panel versions 9.x.

Use the following instruction for applying hotfix.

1. Download hotfix from the attachment

# wget http://kb.parallels.com/Attachments/13985/Attachments/cgi_wrapper

2. Locate file cgi_wrapper

# cat /etc/psa/psa.conf |grep CGI_PHP CGI_PHP_BIN /var/www/cgi-bin/cgi_wrapper/cgi_wrapper

3. Create a backup of this file

# cp /var/www/cgi-bin/cgi_wrapper/cgi_wrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper_orig

4. Replace cgi_wrapper with the downloaded one

# cp cgi_wrapper /var/www/cgi-bin/cgi_wrapper/cgi_wrapper

5. Copy php.ini to the configuration directory of domain

# cp php.ini /var/www/vhosts/domain.tld/conf/

6. Change permissions on the configuration directory of the domain

# chmod 0755 /var/www/vhosts/domain.tld/conf

7. Restart apache

# /etc/init.d/httpd restart
Should the download become unavailable, I have included the contents of the updated cgi_wrapper here for convenience:
#!/bin/sh

domain=`fgrep -m 1 "$UID" /etc/passwd| awk -F\: '{print $6}' |awk -F"/" '{print $5}'`
PHPRC=/var/www/vhosts/$domain/conf/php.ini

[ -f ${PHPRC} ] || PHPRC="/etc/php.ini"

export PHPRC
exec /usr/bin/php-cgi -c "$PHPRC"

Posted July 6, 2011
Aug 27, 2011
Flame said...
As of php 5.3 there are per-host & per-path php.ini sections that can be used with php-fastcgi.
see http://us3.php.net/manual/en/ini.sections.php

There are also .user.ini files
see http://uk2.php.net/manual/en/configuration.file.per-user.php

So after your server auto-updates to php 5.3.6 and screws up all of your sites, you can use this new functionality ;)

Sep 01, 2011
Dave Disaster said...
LMAO! Don't even MENTION 5.3 to me for another week - I'm still in pain...
May 10, 2012
JoelLeo said...
The back tick is missing at the end of the first/longest line in your cgi_wrapper text.
Thanks for the tutorial :-)
May 11, 2012
Dave Disaster said...
Well spotted - thanks!