Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can you set the PATH environment variable for Apache2? I've tried setting it in /usr/sbin/envvars and in httpd.conf with SetEnv PATH (and passing it along to SSI with PassEnv), but it just doesn't get carried along.

share|improve this question
1  
Need more information. This depends on the OS in question. Windows does things differently from Un*xes. FreeBSD, RedHat and Ubuntu all handle this in different files. –  Stefan Lasiewski Sep 27 '11 at 16:57
    
You say some approaches "don't work for PATH". You need to show us how you're testing it. –  Steve Kemp Sep 27 '11 at 17:04

6 Answers 6

As others have said, you do this through use of the an environment variable file. I will provide more details in this answer, and show proof that it works.

This environment variable file must be source from apachectl. On my Ubuntu box, this file is at /etc/apache2/envvars. On RedHat, this is at /etc/sysconfig/httpd. On FreeBSD, this is set in /etc/rc.conf (I think). As an alternative, you can also set this information in a startup script (/etc/init.d/httpd or apachectl, etc.). However, I think it's best to leave the startup scripts alone if possible. The best place is in the designated environment variables script.

  1. Verify the location of this envvars file. On Ubuntu, /etc/init.d/apache2ctl shows that it sources /etc/apache2/envvars:

    # the path to the environment variable file
    test -z "$APACHE_ENVVARS" && APACHE_ENVVARS='/etc/apache2/envvars'
    # pick up any necessary environment variables
    if test -f $APACHE_ENVVARS; then
       . $APACHE_ENVVARS
    fi
    
  2. To view the variables, I am using a Perl printenv.cgi script, and made it available at http://example.org/cgi-bin/printenv.cgi . The script shows me the following PATH:

    PATH = /usr/local/bin:/usr/bin:/bin
    
  3. To change these variables, I edit the envvars file:

    $ sudo vim /etc/apache2/envvars 
    
  4. Modify your PATH in this file. In this example, I will append /opt/local/bin to my PATH. In some cases, you may need to use export PATH and not just PATH:

    export PATH=$PATH:/opt/local/bin
    
  5. Restart apache

    $ sudo service apache2 restart
     * Restarting web server apache2
     ... waiting    ...done.
    $
    
  6. View the results on http://example.org/cgi-bin/printenv.cgi , which now show that the PATH now contains a new element:

    PATH = /usr/local/bin:/usr/bin:/bin:/opt/local/bin
    

If the above does not work, something unusual may be happening happening. Perhaps your script is ignoring or overwriting the PATH.

share|improve this answer
    
Make sure you restart httpd - I tried using graceful, and that did not cause the changes to take effect. –  Xiong Chiamiov May 23 '13 at 0:52

On my system it's /etc/apache2/envvars.

share|improve this answer
    
It works but not for PATH –  Matteo Sep 27 '11 at 15:32
    
@Matteo: This will work. See my detailed example below for proof. –  Stefan Lasiewski Sep 27 '11 at 17:38

Make sure you've loaded mod_env.

The correct syntax is (example):

SetEnv LD_LIBRARY_PATH /usr/local/lib

This worked for me.

share|improve this answer
    
It works but not for PATH –  Matteo Sep 27 '11 at 15:32
    
How do you load mod_env? Doesn't it get loaded by default as it is built-in and statically compiled? –  Chase T. Jun 4 at 13:56

On my Apache2 system I found that a lot of visitors only wanted to pillage my carefully-compiled collection of Smileys/emoticons (seriously!), so I decided to use a randomly-generated symlink to the directory path that would be picked up as an Apache2 variable, and it was practically impossible to share links.

So, every 30 minutes, cron ran a perl script that would rewrite the .htaccess file and include a SetEnv as you can see below:

....
SetEnv SMILEYDIR "QyQGTZ8cgcK3zMVL0aNJ"

Then, at the top of my PHP pages I'd read the variable like so, and later in the page there'd be that variable used to build a path to the apprpropriate smiley:

$SMILEYDIR = apache_getenv("SMILEYDIR");
....
....
echo '<img src="/'.$SMILEYDIR.'//SMILEY_yay!.gif" border="0" hspace="3">';

Works for me, and hope it helps you!

share|improve this answer
    
It works but not for PATH –  Matteo Sep 27 '11 at 15:32

You can set it in start() function of init script, something like this:

start() {
        echo -n $"Starting $prog: "
        check13 || exit 1
        export PATH=${PATH}:/var/ossec/bin
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

Create a Perl script to list all the environment variables:

#!/usr/bin/perl -wT
print "Content-type: text/html\n\n";

foreach $key (sort keys(%ENV)) {
  print "$key = $ENV{$key}<p>";
}

Place it into /var/www/cgi-bin, and check http://domain.com/cgi-bin/env.cgi, you will see the belows:

PATH = /sbin:/usr/sbin:/bin:/usr/bin:/var/ossec/bin
share|improve this answer

On 2.2, the PATH environment variable cannot be set using Setenv.

http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.