mvcqcg
Last Updated: February 25, 2016
·
7.195K
· heldrida
6ba51be61ac6a969f616d9e2f073d039

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']
Say Thanks
Respond

2 Responses
Add your response

7215
F898b4e1ba5b8ed93f643859b9d55cd6

I'd use getenv('VARNAME').

over 1 year ago ·
7216
6ba51be61ac6a969f616d9e2f073d039

@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 ·