Fix the Problem with $_SERVER["SCRIPT_FILENAME"] when using Apache + PHP-FPM + mod_fastcgi

By BXTra |

I was trying to run Boost module with Drupal on one of my website and I got the problem that Boost doesn't work as it should be no matter what I did. I don't have this problem when using [NginX + PHP-FPM] or [Apache + mod_fcgid]. So, I checked error log and found below :

file_exists(): open_basedir restriction in effect. File(/usr/local/bin/.htaccess) is not within the allowed path(s): (/home/testdomain/:/tmp/:/var/tmp/:/var/www/html/) in /home/testdomain/domains/testdomain.com/public_html/sites/all/modules/boost/boost.install on line 101.

Look into the detail and found that Boost use some _SERVER["SCRIPT_FILENAME"] variable in code such as :

$htaccess = (stristr($_SERVER["SERVER_SOFTWARE"], 'apache') && file_exists(dirname($_SERVER["SCRIPT_FILENAME"]) . '/.htaccess')) ? file_get_contents(dirname($_SERVER["SCRIPT_FILENAME"]) . '/.htaccess') : FALSE;

which cause the problem with a website that is running Apache + PHP-FPM + mod_fastcgi. Since SCRIPT_FILENAME is set to /usr/local/bin which was not the correct path. The correct path should be /home/testdomain/domains/testdomain.com/public_html/index.php . So, I googled around for the method to fix it. Most of them are for NginX but not Apache. Here is what I found :
1. Create a file name php_prepend.inc in your public_html directory :

nano -w /home/testdomain/public_html/php_prepend.inc

2. Paste the script below and save it :

<?php
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['PATH_TRANSLATED'];
unset($_SERVER['PATH_TRANSLATED']);
$_SERVER['SCRIPT_NAME'] = $_SERVER['PATH_INFO'];
unset($_SERVER['PATH_INFO']);

3. Now, you have 2 options to choose. You can set auto_prepend_file in php.ini or in your php-fpm configuration file. I choose to do it in php-fpm so that I can use this script on specific domain. What you have to put into your PHP-FPM configuration file is :

php_admin_value[auto_prepend_file] = /home/testdomain/public_html/php_prepend.inc

Then, restart php-fpm and this should do the trick. The php_prepend.inc script will run before any php script and set the path to the correct one.

Server Information - Software
- CentOS 5.6 - 64 bits
- Apache 2.2.19
- PHP 5.3.6 with PHP-FPM
- mod_fastcgi
- Drupal 6.22
- Boost 6.x-1.x-dev (2011-Apr-13)

Source :
- http://serverfault.com/questions/263589/how-to-fix-script-name-with-php-fpm-and-apaches-mod-fastcgi

UPDATE (2011.10.07)
Instead of using php_prepend.inc, I decided to modify configuration in Apache to use the correct path. Below is what I do :

FROM :

<IfModule mod_fastcgi.c>
   FastCgiExternalServer /usr/local/bin/testdomain -socket /fcgi/admin/public_html/testdomain.sock
   Alias /fastcgiphp /usr/local/bin/testdomain
</IfModule>

TO :

<IfModule mod_fastcgi.c>
   FastCgiExternalServer /home/testdomain/domains/testdomain.com/public_html/index.php -socket /fcgi/testdomain/public_html/testdomain.sock
   Alias /fastcgiphp /home/testdomain/domains/testdomain.com/public_html/index.php
</IfModule>

Now, it works without having to use "php_prepend.inc" anymore. However, the method above fix the issue for "SCRIPT_FILENAME", not "SCRIPT_NAME". "SCRIPT_NAME" here will still be "/fastcgiphp". So, you may still need php_prepend.inc to fix "SCRIPT_NAME" path.

For me the issue was SCRIPT_NAME which was giving a wrong directory after I chrooted my PHP-FPM. Although this has fixed my issue im not sure if having it match PATH_INFO is a long term fix or a good idea.

Add new comment

  • No HTML tags allowed.
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.