Set an Environment Variable in both Apache and System

Posted on

Set an Environment Variable in both Apache and System – A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about Set an Environment Variable in both Apache and System was one problem in server stack that need for a solution. Below are some tips in manage your linux server when you find problem about linux, apache-2.4, redhat, httpd, environment-variables.

I’d like to set an environment variable in both system and httpd.

I set the variable, let’s say IMPORTANT_FOLDER = /MY/PATH, in /etc/profile and it works for all system users.

What i want to do is to bring this variable into httpd, so that it can be used by some php code.
I managed to make the var available only by adding SetEnv IMPORTANT_FOLDER = /MY/PATH in my httpd configurations.

Could you kindly provide me a method to pass the system variable into apache? The important thing for me is to make it centralized.

I’m using apache 2.4 on redhat 7.

Thank you.

I believe that you can use the SetEnv directive in an Apache configuration file. This file can be the httpd.conf file or you create a separated file in /etc/httpd/conf.d, or wherever your files are located.

For example:

SetEnv IMPORTANT_FOLDER = /MY/PATH

When the Apache server starts, you should be able to “see” that variable in the Apache environment.

An alternative to this would be to modify the HTTPD init script (/etc/init.d/httpd). In the following example, I’ve created a function that reads the output of a keychain (a utility to manage ssh-agent processes) created file and places it in a specific configuration file, prior to starting the Apache server.

The principle here is that you use the init shell script to modify an Apache configuration file (located in /etc/httpd/conf.d in this case) prior to starting Apache.

setKeychain() {

    if [ -a $(cat /etc/passwd | grep ^apache: | cut -d':' -f6)/.keychain/$(uname -n | cut -d' ' -f1)-sh ] ; then
        SSH_AUTH_SOCK=$(cat $(cat /etc/passwd | grep ^apache: | cut -d':' -f6)/.keychain/$(uname -n | cut -d' ' -f1)-sh | cut -d'=' -f2 | cut -d' ' -f1 | head -1 | sed 's/;$//g')
        SSH_AGENT_PID=$(cat $(cat /etc/passwd | grep ^apache: | cut -d':' -f6)/.keychain/$(uname -n | cut -d' ' -f1)-sh | cut -d'=' -f2 | cut -d' ' -f1 | tail -1 | sed 's/;$//g')

        echo "SetEnv SSH_AGENT_PID $SSH_AGENT_PID" > /etc/httpd/conf.d/keychain.conf
        echo "SetEnv SSH_AUTH_SOCK $SSH_AUTH_SOCK" >> /etc/httpd/conf.d/keychain.conf
        chmod 644 /etc/httpd/conf.d/keychain.conf
    fi

}

In the init script’s “start” function, I add the following to the beginning:

start() {

        setKeychain

This executes the previous function, which “dynamically” sets the value prior to starting Apache. You can modify this to pick up any kind of system variable.

Leave a Reply

Your email address will not be published.