27

I'd like to automatically change my database connection settings on a per-vhost basis, so that I don't have to edit any PHP code as it moves from staging to live and yet access different databases. This is on a single dedicated server.

So I was wondering, can I set a PHP variable or constant in httpd.conf as part of the vhost definition that the site can then use to point itself to a testing database automatically?

$database = 'live';
if (some staging environment variable is true) {
    $database = 'testing'; // and not live
}

If this isn't possible, I guess in this case I can safely examine the hostname I'm running on to tell, but I'd like something a little less fragile

Hope this makes sense

many thanks

Ian

3
  • This is a not a good idea. At least, IMHO. Use your VC, do a tag and commit the changes necessary and be done with it. It will be much cleaner then some sort of server-side config. Commented Sep 29, 2008 at 1:38
  • 2
    We do twenty releases a week, Till... Why is what you're suggesting a good idea? Commented Oct 9, 2008 at 7:26
  • Consider that shell scripts running outside of apache would also need to know their environment. Commented Aug 8, 2014 at 16:41

6 Answers 6

81

Yep...you can do this:

SetEnv DATABASE_NAME testing

and then in PHP:

$database = $_SERVER["DATABASE_NAME"];

or

$database = getenv("DATABASE_NAME");
Sign up to request clarification or add additional context in comments.

3 Comments

As far as i know it should be $_SERVER['DATABASE_NAME'] instead of $_ENV. Tested it here on php 5.2 debian and the vars aren't inserted in $_ENV but in $_SERVER
Looks like you're right. $_ENV works on the command line, but it doesn't seem to get populated when running under Apache.
This sounds more like the answer OP was asking for. The Accepted answer is for overriding settings.
19

You can set an environment variable and retrieve it with PHP.

In httpd.conf:

SetEnv database testing

In your PHP:

if (getenv('database') == 'testing') {

or

if ($_SERVER['database'] == 'testing') {

1 Comment

Works in apache 2.4
15

Did you tried to use the .htaccess file? You could override the php.ini values using it.

Just put the .htaccess file into your htdocs directory:

php_value name value

Futher information:

4 Comments

Doing this through htaccess sounds like a much better idea than httpd.conf.
Disagree; doing it in the vhost configuration section is better, as it means that you can disable .htaccess entirely, which is a performance gain for apache as it doesn't have to check for a .htaccess file for every request.
If this is still not working, check in your httpd.conf for an AllowOverride none command. Spent 45 minutes trying to figure out why my .htaccess file was being ignored just now.
And how to retrieve it in PHP?
3

I would not set an environment variable, as this is also visible in default script outputs like PhpInfo();

just use a php_value in your .htaccess just above the htdocs folder and you're done and safe :)

Comments

2

The problem with .htaccess is that it is part of the code base tree. And the code base tree is part of VC/SVN. Hence any change in local/dev gets moved to production. Keeping the env variable setting in httpd.conf saves you the effort of being careful about not accidentally overwriting the server vs dev flag. Unless of course you want to do with IP address or host name, both of which are not scalable approaches.

1 Comment

Not to mention you can also set it while defining a VirtualHost.
0

I was also looking at this type of solution. What I found is this, under Apache you can use the SetEnv KeyName DataValue in the http.conf and in IIS you can use Fast CGI Settings >> Edit... >> Environment Variables >> ... and add KeyName, DataValue.

This in turn allows the PHP $var = $_SERVER["KeyName"]; to be set to the DataValue and used as needed under both IIS and Apache consistently.

I know this is a strange use case. I use WAMP at work and MAMP at home so it is nice to be able to work the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.