gornvix
gornvix

Reputation: 3374

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 [email protected]
    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?

Upvotes: 0

Views: 1142

Answers (2)

gornvix
gornvix

Reputation: 3374

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.

Upvotes: 1

D&#225;niel Kis
D&#225;niel Kis

Reputation: 2631

Have you tried apache_getenv ?

Upvotes: 1

Related Questions