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 http://127.0.0.1:8080/ it causes an error saying that $_SERVER['MY_VAR'] is not defined.

Snippet from my circle.yml

machine:
php:
version: 7.0.11
dependencies:
pre:
# 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
post:
# 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
test:
pre:
- sudo service apache2 reload
- wget -O - http://127.0.0.1:8080/

And here is my testhost.conf file

Listen 8080

ServerName www.example.com:8080

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

DocumentRoot /home/ubuntu/app/public
ServerName dev.circle.com
<Directory "/home/ubuntu/app/public">
    Options FollowSymLinks Indexes
    AllowOverride All
    Order deny,allow
    Allow from 127.0.0.1
    Deny from all
    Require all granted
</Directory>
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