Setting the Apache 2.4 PATH environment variable in a brew services started httpd process

Written by James McDonald

December 15, 2016

Note the following perl script

#!/usr/bin/env perl -wT
print "Content-type: text/html\n\n";
#
foreach $key (sort keys(%ENV)) {
  print "$key = $ENV{$key}<p>";
}

As you can see the hash bang is #!/usr/bin/env perl  this is supposed to be more portable because it will find the first instance of perl in your path so if you put /usr/local/bin first in the PATH you get your Homebrew installed Perl

The problem is the above script wouldn’t find the Homebrew Perl because my Homebrew Apache 2.4 PATH was PATH = /usr/bin:/bin:/usr/sbin:/sbin

This wouldn’t be a problem except I had installed JSON, Config::Simple and other Perl modules in the Homebrew Perl under /usr/local and when I ran the CGI script that used these against the above default PATH they were erroing out with and @INC error because of not finding the Perl modules under the system Perl in /usr/bin/perl

It took me a while of Googling to find out how to configure the plist file to pass a new environment in but here it is. I hope it saves someone 2 hours.

So firstly shut your httpd24 down

brew services stop httpd24

edit your homebrew.mxcl.httpd24.plist file  and add the EnvironmentVariables key and the following <dict> to it

vim /usr/local/Cellar/httpd24/2.4.23_2/homebrew.mxcl.httpd24.plist

restart your httpd

brew services stop httpd24

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>homebrew.mxcl.httpd24</string>
  <!-- add this -->
  <key>EnvironmentVariables</key>
  <dict>
   <key>PATH</key>
   <string>/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin</string>
  </dict>
  <!-- end add -->
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/opt/httpd24/bin/httpd</string>
    <string>-D</string>
    <string>FOREGROUND</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

b

0 Comments

Submit a Comment

Your email address will not be published.

You May Also Like…

ZPL SBPL

Zebra Printer Language http://labelary.com/viewer.html The above website has a page where you can enter ZPL and it...

PHP Iterators

Just came across a Youtube talk "Iterators in PHP" by Jake Smith published in 2014 that steps through the many...

PHP array_map Multiple Arrays

array_map can take multiple arrays. I like how it starts mapping through them starting at the first element of each...