Code snippets, tech tricks and other bits and bobs

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"

Filed under  //   CentOS 5   PHP   Plesk 9   Plesk 9.5.2+   cgi_wrapper   php-fastcgi   php.ini  

htscanner: Enabling php_admin directives in .htaccess under php-fastcgi (Plesk 9.x + CentOS 5)

[update 201107061858]
For per-domain php.ini under php-fastcgi see here
[/update]

Under Plesk running php-fastcgi  it is not possible to use php_admin_flag and php_admin_value directives in .htaccess files on a per-directory basis. Luckily this can be easily enabled with the htscanner extension.

All the information to get this working is out there, but as per usual, you have to piece it together from a variety of sources to get a complete picture. Here I shall provide a single complete set of instructions that have been proven to work on a CentOS5 box with Plesk 9 - although Plesk should be an irrelevance here, I mention it for completeness.

I am using PuTTY as my SSH client.

In order to install and use htscanner v1.0,0 , you must have the following things (aka dependancies):

PHP Version: PHP 5.1.0 or newer
PEAR Package: PEAR Installer 1.4.8 or newer

Odds of having >=PHP 5.1 without PEAR are slim to my knowledge, and installing it is beyond the scope of this post.

Got those? Ready to go!

To provide absolute clarity for inexperience bods, # is the SSH command prompt and shouldn't be included in typed commands! And if you REALLY need your hand held, hit [enter] after each line. Just so you don't sit there wondering why nothing is happening...

Download and extract htscanner

  1. SSH to your server with root access.
  2. Download the latest version of htaccess from http://pecl.php.net/package/htscanner currently 1.0.0
    #  wget http://pecl.php.net/get/htscanner-1.0.0.tgz
  3. Untar the file:
    # tar -xvsf htscanner-1.0.0.tgz

Compile and run installation

  1. Change to the newly created directory:
    # cd htscanner-1.0.0
  2. Run phpize:
    # phpize
  3. Run the configuration script with --enable-htscanner switch
    # ./configure --enable-htscanner
  4. Run make to build the installer:
    # make
  5. Run make install to actually install the extension
    # make install

Add htscanner settings

Either:

For systems that support it, you can simply copy htscanner.ini to your php conf.d directory. This is probably somwhere along the lines of /etc/php.d on CentOS 5 or perhaps for others /etc/httpd/conf.d but if you are having trouble finding it, run

# locate conf.d

You will find a bunch of them though, so make sure it's the right one. You can also try simply googling your *nix distribution name and conf.d - for example "centos 5 conf.d"

Or:

If you are running SuSE or another flavour of Linux which doesn't support this, you can add the contents of htscanner.ini to your php.ini file. To locate yours try the tips above for finding the php conf.d directory
The htscanner.ini file currently looks like this:

[htscanner]
extension="htscanner.so"

; The configuration file htscanner needs to scan for php_* directives
config_file=".htaccess"

; The fallback docroot when htscanner can't determine the current docroot
default_docroot="/"
default_ttl=300

; Stop when an error occured in RINIT (no document root, cannot get path_translated,...)
stop_on_error = 0

; Warn when an option cannot be set
verbose = 0

You should check the contents of your htscanner.ini file though, to be safe.
This is simple enough, but for the sake of completeness and for any poor n00bs out there, to do this, and then add to php.ini using 'vi' text editor: 

  1. Open htscanner.ini:
    # vi docs/htscanner.ini
  2. Highlight all text to copy to the clipboard
  3. Exit vi by simply typing
    :q

    and hitting [Enter]
  4. Open php.ini:
    # vi /etc/php.ini
  5. Scroll to the bottom of the file with [pgdn] key
  6. Enter editing mode by hitting [i] key
  7. Press [->] Left arrow key to ensure you are at the end of the last line
  8. Hit [Enter] to start a new line
  9. Right-click the mouse to paste the htscanner.ini contents
  10. Exit editing mode by hitting [Esc] key
  11. Save and quit vi:
    :wq
     
    and hit [Enter] 

Restart Apache

On CentOS 5 and similar distros:

#  service httpd restart

For other *nix versions, y'know, google it. Why not, sounds like fun to me?

And voila. You should now be up and running with per directory php_admin directives in your .htaccess files.

Filed under  //   *nix   .htaccess   CentOS 5   PHP   Plesk 9   php-fastcgi