How to use getenv() in php and SetEnv in a .htaccess with a compiled php-cgi on a shared host

.htaccess, apache, php, shared-hosting

Solution

Ok I finally got it. On dreamhost, it is possible to use fastcgi and therefore declare environment variables with it. It consists of just adding this simple script

#!/bin/sh
export PHP_FCGI_CHILDREN=2
exec /home/USERNAME/YOURDOMAIN/cgi-bin/php.cgi

Which is where my compiled PHP5.3.1 was located. chmod 744 on that file called dispatch.fcgi which will be allowed more memory by dreamhost's watchdog.

After that I added to my domain's .htaccess the following:

Options +ExecCGI
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php5-fastcgi .php
Action php5-fastcgi /dispatch.fcgi

now in the application's root I have another .htaccess with:

SetEnv APPLICATION_ENVIRONMENT staging

In a php script is is retrievable via getenv('REDIRECT_APPLICATION_ENVIRONMENT');

Problem

Just putting in context to clarify the main question: On my development machine, PHP5.3.1 is installed on Apache as a module, I use SetEnv APPLICATION_ENVIRONMENT development in my application's root .htaccess file. It is then easily retrievable from any php script with getenv('APPLICATION_ENVIRONMENT'). On the production server, on a sharedhost (dreamhost), I compiled myself php5.3.1 since it was not directly supported. Everything works fine except that getenv('APPLICATION_ENVIRONMENT') returns false. In the sharedhost root .htaccess for my domain, I use this .htaccess file ``` Options +ExecCGI AddHandler php-cgi .php Action php-cgi /cgi-bin/php.cgi <FilesMatch "^php5?\.(ini|cgi)$"> Order Deny,Allow Deny from All Allow from env=REDIRECT_STATUS </FilesMatch> Options -indexes ``` php5.cgi resides in /cgi-bin and works very well. Of course in my application's root folder I have another .htaccess defining: ``` SetEnv APPLICATION_ENVIRONMENT production ``` But when using getenv('APPLICATION_ENVIRONMENT') it returns false, any idea how to resolve this?

Original source