Can't get env vars in PHP

build-image
envars
php

#1

I set env variables in circle.yml and project settings too but i can’t fetch them in PHP app using _SERVER or _ENV.
Anything that am missing? Thanks.


#2

Please show related bits of your circle.yml file.


#3

Thanks alexander.
Below is my circle.yml

machine:
  php:
    version: 5.6.17
  environment: 
    ENVIRONMENT: development 
    CIRCLE_ENV: test
    DATABASE1_HOST: xx.xx.xx.xxx
    DATABASE1_NAME: xxxx
    DATABASE1_USER: xxxx
    DATABASE1_PASS: xxxx
    NGROK_TOKEN: xxxx
    GHOST_API_KEY: xxxx
    GHOST_SUITE_ID: xxxx
dependencies:

and i fetch them with:

$_SERVER['DATABASE1_HOST'] 
$_ENV['DATABASE1_HOST']

both are undefined.


#4

Hi @alexander. can you please help. Thanks.


#5

The configuration looks fine. Hmm… maybe try using getenv function instead of reading SERVER and ENV?


#6

@alexander nothing working , i tried getenv function, tried $GLOBALS
This is very strange, i can use these env variables in circle.yml but can’t get them in php.


#7

How is PHP being run and by what user? Environment variables for CircleCI will be defined in /home/ubuntu/.circlerc. This file needs to be sourced by the same user running PHP for those variables to be available.

So for example, if you were running PHP via Apache, which uses the www-data user by default, they wouldn’t be available.


#8

@FelicianoTech I do have two more env variables in circle.yml

APACHE_RUN_USER: ubuntu
APACHE_RUN_GROUP: ubuntu

so apache and php both running under ubuntu user.


#9

And what should we do so PHP via Apache can see the Environment Variables?


#10