Setting Apache2 PATH environment variable
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.
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.
-
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
-
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
-
To change these variables, I edit the envvars file:
$ sudo vim /etc/apache2/envvars
-
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 useexport PATH
and not justPATH
:export PATH=$PATH:/opt/local/bin
-
Restart apache
$ sudo service apache2 restart * Restarting web server apache2 ... waiting ...done. $
-
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.
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
.
Make sure you've loaded mod_env.
The correct syntax is (example):
SetEnv LD_LIBRARY_PATH /usr/local/lib
This worked for me.
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