Setting environment variables for accessing in PHP when using Apache

PhpApacheUbuntuEnvironment Variables

Php Problem Overview


I have a Linux environment and I have a PHP Web Application that conditionally runs based on environment variables using getenv in PHP. I need to know how these environment variables need to be set for the application to work correctly. I am not sure how to set this up on Apache.

Also, I need to be able to configure separate environment variables for each domain separately.

Please advice on how can I achieve this.

Php Solutions


Solution 1 - Php

Something along the lines:

<VirtualHost hostname:80>
   ...
   SetEnv VARIABLE_NAME variable_value
   ...
</VirtualHost>

Solution 2 - Php

You can also do this in a .htaccess file assuming they are enabled on the website.

SetEnv KOHANA_ENV production

Would be all you need to add to a .htaccess to add the environment variable

Solution 3 - Php

If your server is Ubuntu and Apache version is 2.4

> Server version: Apache/2.4.29 (Ubuntu)

Then you export variables in /etc/apache2/envvars location.

Just like this below line, you need to add an extra line in /etc/apache2/envvars

export MY_ENV_NAME=myEnvValue

Solution 4 - Php

Unbelievable, but on httpd 2.2 on centos 6.4 this works.

Export env vars in /etc/sysconfig/httpd

export mydocroot=/var/www/html

Then simply do this...

<VirtualHost *:80>
  DocumentRoot ${mydocroot}
</VirtualHost>

Then finally....

service httpd restart;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbishekView Question on Stackoverflow
Solution 1 - PhpwroniastyView Answer on Stackoverflow
Solution 2 - PhppaquettgView Answer on Stackoverflow
Solution 3 - PhpSachin RaghavView Answer on Stackoverflow
Solution 4 - Phpdanday74View Answer on Stackoverflow