CGI wrapper mode - base path problems

I have a perplexing problem with CGI wrapper mode (Virtualmin 3.92.gpl running on Ubuntu 10.04 on a VPS). It worked fine previously on Ubuntu 8.04, but after hours of research I just can’t make it work since the OS upgrade. FastCGI works fine for a while but gradually exhausts all available memory (seems to be a known problem) and mod_php also works fine but causes file permission problems with some scripts (also a known problem).

The main symptom with CGI wrapper is that pages are displayed without any styling. Essentially the problem seems to be that scripts think they are running in /cgi-bin instead of /public_html. Generally I do NOT see warnings in any log files but when I do, they are looking for a non-existent file in /cgi-bin. In Drupal I can work around the problem to some extent by setting $base_url in settings.php. In Joomla I can fix it to some extent by setting $live_site in configuration.php. But I still have unresolved problems in Wordpress (permission denied in admin) and Magento, for example.

Virtualmin was installed using install.sh and seems to be configured correctly - for example, /etc/apache2/suexec/www-data has the expected contents. I’ve tried setting all site file permissions to 755 (or 777). I’ve tried setting and unsetting cgi.fix_pathinfo and cgi.force_redirect (and checked that they really do take effect, 'cos it’s easy to edit them in the wrong place or forget to apply the change). I’ve tried a “clean” installation of a virtual server. Nothing seems to make any difference.

I found many reports of similar problems but the threads all end with no resolution or a solution that doesn’t work for me. It should be easy to reproduce - clean install Virtualmin on Ubuntu Lucid, install Drupal or Joomla or Wordpress in CGI wrapper mode and you’ll immediately see problems.

Howdy,

We’ve been seeing some rather bizarre behaviors from PHP 5.3 when running in CGI mode.

Using mod_php and FCGID don’t have those same issues.

Essentially, PHP’s SCRIPT_NAME and PHP_SELF variables are set to what we feel are incorrect value’s in certain circumstances – which causes issues like what you’er seeing.

Editing $HOME/etc/php.ini, and setting cgi.fix_pathinfo to “0” does fix that issue for a lot of people, but it’s not a complete fix – and it also introduces new problems to do so.

After weeks of digging into this, we did finally find a workaround that will be implemented in the next Virtualmin version.

In the meantime, I’ve been using this on some of my servers:

  • Create a file “/etc/php5/conf.d/cgi_fix.ini” with the following contents:

auto_prepend_file = /etc/php5/cgi_fix.php

  • Create a file “/etc/php5/cgi_fix.php” with the following contents:
<?php if (ini_get("cgi.fix_pathinfo") != 1) { $_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF']; $_ENV["SCRIPT_NAME"] = $_SERVER['PHP_SELF']; } elseif (! isset($_SERVER['PATH_INFO'])) {
    $current_url = "";

    if (isset($_SERVER['REDIRECT_URL'])) {
            $current_url = $_SERVER['REDIRECT_URL'];
    }
    else {  
            $current_url = $_SERVER['REQUEST_URI'];
    }

    $_SERVER['SCRIPT_NAME'] = $current_url;
    $_ENV["SCRIPT_NAME"] = $current_url;

    $_SERVER['PHP_SELF'] = $current_url;
    $_ENV["PHP_SELF"] = $current_url;

}
?>

I thought it must be something like that. I’ll give the patch a try, many thanks (or use the Virtualmin update, whichever happens soonest!).