13

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.

Matteo
  • 457
  • 3
  • 14
  • 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. –  Sep 27 '11 at 17:04

8 Answers8

15

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.

dav
  • 245
  • 1
  • 12
Stefan Lasiewski
  • 22,949
  • 38
  • 129
  • 184
  • 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 00: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.» https://paste.debian.net/361218/ – Nemo Jan 06 '16 at 14:57
4

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

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

Chase T.
  • 201
  • 3
  • 5
1

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

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
1

Make sure you've loaded mod_env.

The correct syntax is (example):

SetEnv LD_LIBRARY_PATH /usr/local/lib

This worked for me.

weeheavy
  • 4,039
  • 1
  • 27
  • 41
1

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
quanta
  • 50,327
  • 19
  • 152
  • 213
1

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.

dcarrith
  • 141
  • 3
  • 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.» https://paste.debian.net/361218/ – Nemo Jan 06 '16 at 15:03
1

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
frazras
  • 193
  • 1
  • 9
0

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!

peterg22
  • 79
  • 2