Last Updated: January 23, 2019
· heldrida

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:


2 Responses
Add your response


I'd use getenv('VARNAME').

over 1 year ago ·

@ochekurishvili thanks for pointing it! 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.

over 1 year ago ·