How to fix SCRIPT_NAME with PHP-FPM and Apache’s mod_fastcgi

apache-2.2php-fpm

I have the following in my Apache conf to get PHP-FPM working:

FastCgiExternalServer /srv/www/fast-cgi-fake-handler -host 127.0.0.1:9000
AddHandler php-fastcgi .php
AddType text/html .php
Action php-fastcgi /var/www/cgi-bin
Alias /var/www/cgi-bin /srv/www/fast-cgi-fake-handler
DirectoryIndex index.php

This works fine except that SCRIPT_NAME is always /var/www/cgi-bin and some scripts use SCRIPT_NAME to work out the location of the current script (vBulletin).

Google has plenty of solutions for Nginx but not a word for Apache.

Best Answer

If you use RewriteRule in place of "Action" method, it will work out just fine

Given below is an example I used to in Apache to connect to FCGI::Daemon external fastcgi daemon process. This daemon can be considered as a perl's version of php-fpm.

<VirtualHost 127.0.0.1:80>
    ServerAdmin info@example.com
    DocumentRoot "/var/www/example"
    ServerName www.example.com

    <IfModule mod_fastcgi.c>
    FastCGIExternalServer /var/www/fcgi-daemon/perl.fcgi -socket /var/run/fcgi-daemon.sock
    ScriptAlias /perl.fcgi /var/www/fcgi-daemon/perl.fcgi
    </IfModule>
</VirtualHost>

<Directory /var/www/example/cgi-bin>
    Options ExecCGI
    RewriteRule ^(.*)$ /perl.fcgi [L]
</Directory>

Note: "/var/www/fcgi-daemon/" has to exist! as blank directory. Actual the method you used for php does not work out of box for perl daemon. SCRIPT_FILENAME was always /var/www/fcgi-daemon/perl.fcgi which never existed, leading to error. Using RewriteRule kept all the required environment variables in place and passed them along to perl fastcgi daemon.