132

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.

1

4 Answers 4

193

Something along the lines:

<VirtualHost hostname:80>
   ...
   SetEnv VARIABLE_NAME variable_value
   ...
</VirtualHost>
Sign up to request clarification or add additional context in comments.

5 Comments

was not in my $_ENV array, for retreiving value see: stackoverflow.com/questions/2378871/…
@i_a you can access the value in PHP with getenv('VARIABLE_NAME')
in xampp on windows the file will be C:\xampp\apache\conf\extra\httpd-vhosts.conf
@i_a On my machine the value can also be found in the $_SERVER variable.
Also you can use you Include or IncludeOptional if you don't want to change vhost.conf
36

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

1 Comment

is this any different from using a .env file such as the way laravel does this? Does this make it any way less secure? I ask this because I'm building the same backend structure with multiple frameworks (laravel, express, django, etc.) and some developers of those other languages say its' not appropriate to use .env and that the "server config" should be used instead, or doing it through the command line, heroku style
18

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

1 Comment

If you are coming to this question for a LAMP stack, this is probably what you want to use. Good answer btw!
11

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;

1 Comment

Not sure why in MacOSX's apache, the DocumentRoot ${mydocroot} simply write it as a string vs evaluating it and place it in the vhost

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.