1

I have a PHP app and I would like to host staging, demo, and production servers from one instance on Linode. The challenge I am running into is loading different environment variables for each server.

I have created multiple .conf files in /etc/apache2/sites-available/ which look like this:

<VirtualHost *:80>
     ServerAdmin info@example.com
     ServerName stag.example.com
     ServerAlias stag.example.com
     DocumentRoot /var/www/example-app/public
     ErrorLog /var/www/example-app/writable/logs/errors.stag.log
     CustomLog /var/www/example-app/writable/logs/access.stag.log combined
     RewriteEngine on
     RewriteCond %{SERVER_NAME} =stag.example.com
     RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

I have tried creating .sh files for each server in the /etc/profile.d/ directory that include all the environment variables, which look like this:

export ENV1="abc"
export ENV2="def"

But I don't know how I can load that file in the .conf file. I have also tried explicitly setting an APP_ENV variable in the .conf (i.e. SetEnv APP_ENV staging). I would then check the APP_ENV variable in the PHP code (i.e. getenv("APP_ENV")) and based on that, I could then read the correct .sh file and load the environment variables in that file into $_ENV. However, for some reason, the APP_ENV variable is not showing up when I retrieve all the environment variables in my PHP code (i.e. print_r getenv()). When I do that, only the variables set in the /etc/apache2/envvars are listed.

I am new to Ubuntu and Linode (previously, I only used managed servers), so it is possible I am missing something simple, so any help you can provide, would be much appreciated.

Evan Appleby
  • 111
  • 2

1 Answers1

1

PHP environnement variables are not the same as your system ones or apache ones.

If you want to set an APP_ENV environment variable that could be retrieved from your PHP code using getenv("APP_ENV"), you shall consider declaring it in your php.ini file. (better explanation here: https://w3schools.in/php/environment-variables )

  1. Go to /etc/php/XX/apache2/php.ini (where XX depends of your PHP version)
  2. Add a variable (example: APP_ENV = "PROD")
  3. Read it from your PHP code getenv("APP_ENV")

Does it helps? Best regards.

  • Thank you. That explains why the system environment variables weren't accessible to the PHP code. Is there a preferred way to determine which server is being loaded (e.g. staging or production), so the APP_ENV variable can be set correctly? – Evan Appleby Nov 02 '22 at 13:36
  • If i understand well what you want to do. Inside your apache directive, you may add a "SetEnv APP_ENV XXXX" (where XXXX is what you want). Like that, your PHP will be able to retrieve the APP_ENV variable modified corresponding to your virtual_host. This is something that you may also specify in servers .htaccess files. – petitradisgris Nov 02 '22 at 13:46
  • Yes, that is what I assumed, but when I then try to retrieve that variable in my PHP code, I don't see any results. I've tried each of these in my PHP code: `print_r($_ENV);` `print_r($_SERVER);` `echo apache_getenv("APP_ENV");` `echo $_SERVER["APP_ENV"];` and `echo getenv("APP_ENV");` – Evan Appleby Nov 02 '22 at 13:58
  • Okay i see. In your `php-fpm.conf` file, try to find the directive `clear_env` and set it to 'no'. `clear_env = no`. Then restart php-fpm, and restart apache2. And try again displaying the variable. – petitradisgris Nov 02 '22 at 14:05
  • Thanks again for all your help here. Unfortunately, this didn't work for me. For reference, this is what I did. 1. `apt-get install php-fpm` 2. `nano /etc/php/8.1/fpm/pool.d/www.conf` 3. uncommented `clear_env = no` 4. `systemctl start php8.1-fpm.service` 5. `systemctl reload apache2` – Evan Appleby Nov 02 '22 at 14:48
  • Just a question: may you confirm which php configuration file is linked to your apache2 server? Its in your apache.conf file. – petitradisgris Nov 02 '22 at 16:58
  • In my phpinfo.php page, it shows the php configuration file as /etc/php/8.1/apache2/php.ini. I don't see a apache.conf file, but I do see a apache2.conf in the /etc/apache2 directory. That file doesn't seem to refer to any php config file though. – Evan Appleby Nov 02 '22 at 17:40
  • Just to follow-up on this, I believe the variables set with SetEnv were not available in the PHP code because I did not have the mod_php module enabled (h/t to https://www.linode.com/community/questions/23444/how-do-i-access-environment-variables-set-in-conf-file-in-php-code). Rather than enable that, which would have conflicted with multi-threaded modules that I have installed, I instead relied on the host to determine which environment to load, then pretty much followed your directions. Thanks again for the assistance. – Evan Appleby Nov 04 '22 at 22:12