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.

  • 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

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. Perhaps your script is ignoring or overwriting the PATH.

  • 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 fedora 23: «This file is no longer used to configure additional environment variables for the httpd process. It has been replaced by systemd.» paste.debian.net/361218 – Nemo Jan 6 '16 at 14:57

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

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

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

  • 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.

  • 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 '14 at 13:56

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

On RedHat with Apache 2.4.x compiled from source with --prefix=/usr --enable-layout=RedHat the envvars file is located at /usr/sbin/envvars. I can confirm that setting the appropriate PATH in that file makes it so Apache and PHP are aware of it.

I used the same approach as Stefan Lasiewski to determine that. For Apache 2.4.12, the /usr/sbin/envvars file is sourced on line 49 of /usr/sbin/apachectl.

  • In fedora 23, /usr/sbin/apachectl mentions /etc/sysconfig/httpd instead, which contains: «This file is no longer used to configure additional environment variables for the httpd process. It has been replaced by systemd.» paste.debian.net/361218 – Nemo Jan 6 '16 at 15:03

Edit the Apache2 plist file with whatever editor you like (example using vim):

$ sudo vim /System/Library/LaunchDaemons/org.apache.httpd.plist

Add the following to the the plist file:

<key>EnvironmentVariables</key>
<dict>
  <key>PATH</key>
  <string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>
</dict>

If the EnvironmentVariables key already exists, just add

<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin</string>

to the <dict>

NB: For Mac OSX 10.11 (EL Capitan) or higher, you need to run command csrutil disable in Terminal after rebooting and hitting CMD+R and then you will be able to edit this file.

Restart Apache2

$ sudo /usr/sbin/apachectl stop
$ sudo /usr/sbin/apachectl start

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!

  • It works but not for PATH – Matteo Sep 27 '11 at 15:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.