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.
-  1Need 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– Stefan Lasiewski2011-09-27 16:57:02 +00:00Commented Sep 27, 2011 at 16:57
-  You say some approaches "don't work for PATH". You need to show us how you're testing it.user9565– user95652011-09-27 17:04:29 +00:00Commented Sep 27, 2011 at 17:04
8 Answers
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/apache2ctlshows 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/binto my PATH. In some cases, you may need to use- export PATHand not just- PATH:- 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.
-  Make sure you restart httpd - I tried usinggraceful, and that did not cause the changes to take effect.Xiong Chiamiov– Xiong Chiamiov2013-05-23 00:52:44 +00:00Commented May 23, 2013 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/361218Nemo– Nemo2016-01-06 14:57:24 +00:00Commented Jan 6, 2016 at 14:57
On 2.2, the PATH environment variable cannot be set using Setenv.
On my system it's /etc/apache2/envvars.
-  It works but not forPATHMatteo– Matteo2011-09-27 15:32:13 +00:00Commented Sep 27, 2011 at 15:32
-  @Matteo: This will work. See my detailed example below for proof.Stefan Lasiewski– Stefan Lasiewski2011-09-27 17:38:16 +00:00Commented Sep 27, 2011 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.
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/apachectlmentions/etc/sysconfig/httpdinstead, 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/361218Nemo– Nemo2016-01-06 15:03:47 +00:00Commented Jan 6, 2016 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 forPATHMatteo– Matteo2011-09-27 15:32:18 +00:00Commented Sep 27, 2011 at 15:32