Set Environment Variables in Apache for PHP

We can setup custom environment variables in Apache and use it in PHP. The way to do it, for Ubuntu Server 12.04 for example, is by editing your vhost file under /etc/apache2/sites-available/

ServerAdmin admin@host
DocumentRoot "/var/www/"
ServerName local.server
ServerAlias local.alias.server
SetEnv VARNAME myvariable

You can access this variable in your application by doing:

$_SERVER['VARNAME']

Note: For future readers, getenv() is a good choice, since it’s case insensitive ? Correct me if I’m wrong. For early versions of php, $_SERVER wasn’t available, so have that in mind about my tip! Also, I’d like to point out, as a personal note, that in terms of performance, calling a fn() vs getting a variable value may cause some discussion in your team.

comments powered by Disqus