Accessing environment / server vars in PHP

I’m using a php 7.0.11 with Apache on a Ubtunu 14.04 (trusty) machine.

I accessed Project settings > Environment Variables and defined a couple variables to access in my code via $_SERVER (Similar to what AWS ElasticBeanstalk does) but my code (when run by Apache) is not seing those variables.

If I ssh to the machine and run php -i | grep MY_VAR it will print both MY_VAR=value and $_SERVER['MY_VAR']=value. If I run php index.php it will say that $_SERVER['MY_VAR'] is defined.

However, when I do wget it causes an error saying that $_SERVER['MY_VAR'] is not defined.

Snippet from my circle.yml

version: 7.0.11
# Exporting server vars as apache vars so they can be accessed as $_SERVER
- echo “export MY_VAR=$MY_VAR” | sudo tee -a /etc/apache2/envvars
# Removing old php mod
- sudo rm /etc/apache2/mods-enabled/php5.load
# Enabling our test VirtualHost
- sudo cp ~/app/testhost.conf /etc/apache2/sites-available
- sudo a2ensite testhost
# Enabling mod_rewrite so .htaccess can work
- sudo a2enmod rewrite
# Restarting apache after all changes
- sudo service apache2 restart
- sudo service apache2 reload
- wget -O -

And here is my testhost.conf file

Listen 8080


<VirtualHost *:8080>
LoadModule php7_module /opt/circleci/php/7.0.11/usr/lib/apache2/modules/

DocumentRoot /home/ubuntu/app/public
<Directory "/home/ubuntu/app/public">
    Options FollowSymLinks Indexes
    AllowOverride All
    Order deny,allow
    Allow from
    Deny from all
    Require all granted
1 Like

The fix was to create a shell script to export my envvars to my apache host file using printenv + perl regex and then reload Apache