Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add php environment variable?

php 5.4/fcgi on apache 2.4, centos 7.1. php/apache are built/managed by cpanel easyapache.

I'm trying to add an entry to the "environment" section that displays on phpinfo()

enter image description here

I have tried the following :

  • SetEnv in httpd.conf - it doesn't add it as an environment variable, it appears under $_SERVER
  • add to /etc/sysconfig/httpd - has no effect whatsoever.
  • FcgidInitialEnv MY_VARIABLE SomeValue in httpd.conf - also no effect.

Any advice appreciated. Note this section in phpinfo is NOT for showing system environment variables.

like image 577
Sherif Buzz Avatar asked Apr 19 '26 21:04

Sherif Buzz


1 Answers

Assuming you're using mod_fcgi and you configured a wrapper script like this:

FcgidWrapper /var/www/wrapper/php72wrapper .php

The wrapper script php72wrapper then defines the environment variables when the request is dispatched to the real php binary - MY_VARIABLE has been added as example:

#!/bin/sh
export PHPRC="/var/www/wrapper/php72wrapper"
export MY_VARIABLE="Whatever it should contain"
exec /usr/bin/php72

In PHP you could access that value by e.g. using getenv('MY_VARIABLE'). Besides that MY_VARIABLE is also listed in the "Environment" section of phpinfo().

An alternative way is to use FcgidInitialEnv in your Apache VirtualHost configuration:

FcgidInitialEnv MY_VARIABLE "Whatever it should contain"

see https://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html#fcgidinitialenv

like image 181
Oliver Hader Avatar answered Apr 22 '26 17:04

Oliver Hader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!