tyebillion tyebillion - 1 month ago 9
Apache Configuration Question

How to set an environmental variable in Apache (or OS) and get the value in PHP?

I made sure the env module was installed:

a2enmod
Your choices are: access_compat
...
Which module(s) do you want to enable (wildcards ok)?
env
Module env already enabled


I tried setting an environmental variable in "/etc/apache2/apache2.conf" for localhost by adding the following section:

<VirtualHost *:80>
ServerName localhost
ServerAdmin me@somewhere.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SetEnv devmode 1
</VirtualHost>


...restarted Apache using:

sudo /etc/init.d/apache2 restart


...and then getting the value in PHP using:

getenv("devmode")


or:

apache_getenv("devmode")


...but nothing shows up with these functions when accessing the script via a browser on localhost, the values are empty in PHP. I tried placing the command on it's own, without the VirtualHost section:

SetEnv devmode 1


I tried adding the following line to envvars:

export devmode=1


Update
I also tried putting the VirtualHost section in a dedicated file in /etc/apache2/sites-available, and adding the file with a2ensite.

I am using Linux Mint version 17.3. What am I doing wrong?

Answer

After putting the VirtualHost section into a dedicated file, as I described in the original question, I found that the solution was to run the Apache restart command again:

sudo /etc/init.d/apache2 restart

Then the devmode variable showed up in PHP.

Comments