3

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.

4
  • Have you tried export PATH=$PATH:/new/folder/path from the terminal if you're just looking to add to your path? Commented Jan 7, 2018 at 13:45
  • I'm not looking to add to the path, I'm looking to add a totally new variable. Commented Jan 7, 2018 at 13:50
  • Then you should be able to just do export EXAMPLE=1 from terminal Commented Jan 7, 2018 at 13:55
  • I'm not talking about system environment variables, updated question to clarify this Commented Jan 7, 2018 at 14:09

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Oliver, note that php/fcgi/apache is managed by cpanel, and there doesn't seem to be any clear way to add a wrapper script.
Search for ORIG_SCRIPT_FILENAME in the output of phpinfo() that's the wrapper script being used.
Updated answer by FcgidInitialEnv MY_VARIABLE SomeValue in your Apache VirtualHost configuration
Thx again, I'd already tried FcgidInitialEnv also to no avail. Updated my answer to include this
0

Finally found a solution, the only way that works is to add a wrapper script around the calling of php, and setting the values there - thanks to Oliver Hader for the tip.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.