Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • CWJ 09
« I’m on “Managing The Gray”
CIO Magazine Trolls and Gets Spanked Hard »

Battling with Apache and PassEnv

Dear Reader,

I’ve been managing Apache servers since 1999 and while I consider myself competent, I never processed to be an expert. So it really didn’t surprise me yesterday when I learned something new.

A buddy of mine was discussing a problem he was having with his corporate web site. It’s hosted on multiple web servers behind a load balancer. The problem is, they are having problems with one of the web servers but they can’t figure out which. I remember that back at Jupiter, we had a similar problem and that one of my guys there injected a header that allowed us to track which server a specific request was coming from. Unfortunately for my friend, that was about as far as I remembered. (I was management then, I didn’t pay attention to details) However, he’s a bright guy and had already thought of this…the difference was, he knew basically how to do it.

I started playing with ideas on my development server and got about 75% of the way there when he IMed me that he had it working. (So much for speed) I fired up FireFox and TamperData and sure enough, he had a custom header in there. (BTW, TamperData is invaluable for debugging things like this.) However, when I followed his direction, it just was not happening for me. Googling around found me a LOT of copies of the Apache manual, but no concrete examples. So, since I couldn’t find the answer on the web, I decided to post how I did this in case some other Apache noob comes looking for it.

First, an important detail, my development environment is a customized version of CentOS 5. I use yum for just about everything, except Apache, PHP, MySQL and their support programs. I use a script that comes with DirectAdmin, my production control panel of choice, to maintain those pieces. The important thing to note here is that CentOS does NOT use apachectrl to start and stop apache.

The answer to inserting a header, of course, revolves around PassEnv. If you have mod_env installed in your Apache 1.3.7 or Apache 2.x, this will work.

I’m working with Virtual Hosts and each development site has it’s on conf file that is included into the main httpd.conf. The great thing about PassEvn though is you can put it in an .htaccess file as well. This means for testing, you don’t have to constantly be bouncing the service.

To identify my server, I decided I wanted to add a header that displayed the host name that the page was being served from. This means in my conf file or .htaccess, I need the following:

PassEnv HOSTNAME
Header set X-MyHeader "%{HOSTNAME}e"

That gets us about 75% there. The PassEnv makes the HOSTNAME environment variable available to APache and the Header command actually sets the new header. “%{VARAIABLENAME}e” is the syntax for displaying the variable in the header.

All that is fine and good and if you drop that in your .htaccess file and then hit a page on the site, you will get:

X-MyHeader (null)

Now, my friend said he added:

export HOSTNAME=`hostname`

to his apachectrl script and all was good. I tried this and (if you read the note above, you see this coming) it did not fix the problem.

Digging deeper, I found that apachectrl sources a file /usr/sbin/envvars. Looking it, it’s obvious that this is where they expect you to put these commands so you don’t have to have a customized version of apachectrl. So I took my export out of apachectrl and put it in envvars. Still no dice, yeah, I know, you expected this.

The problem, as I explained above, is that if you are using “service httpd start” on CentOS then apachectrl is being ignored. Lickily for us, /etc/init.d/httpd is another bash script. So I grabbed the lines out of apachectrl that sourced /usr/sbin/envvars, inserted them in /etc/init.d/httpd and BINGO, we have a header.

if test -f /usr/sbin/envvars; then
  . /usr/sbin/envvars
fi

I put them high up in the file, near where it sources the functions file.

So, it is possible, and even easy to add custom headers into Apache that include environment variables. You need to make sure that mod_env and mod_headers are both installed and the rest is just figuring out where to put things.

UPDATE: Since I was asked, yes you can access this new information in PHP via the $_ENV super global.

A var_dump of the $_ENV on my development server now looks like this:

array(8) {
  ["HOSTNAME"]=>
  string(5) "david"
  ["TERM"]=>
  string(5) "xterm"
  ["LD_LIBRARY_PATH"]=>
  string(15) "/etc/httpd/lib:"
  ["PATH"]=>
  string(29) "/sbin:/usr/sbin:/bin:/usr/bin"
  ["PWD"]=>
  string(1) "/"
  ["LANG"]=>
  string(11) "en_US.UTF-8"
  ["SHLVL"]=>
  string(1) "2"
  ["_"]=>
  string(15) "/usr/sbin/httpd"
}

So you can see, you can see that while the Header X-MyHeader is not exposed, the variable it contains, HOSTNAME, is.

For the record, hostname was probably not the best example since it’s usually in the $_SERVER array. However, the point of the experiment was not to expose this information to PHP, it was to put it in the response form the server.

Until next time,
(l)(k)(bunny)
=C=

Related posts

  • No related posts.

Tags: apache php mysql, apache servers, centos 5, custom header, TamperData

This entry was posted on Saturday, January 26th, 2008 at 11:36 am and is filed under Apache, Programming. You can follow any responses to this entry through the RSS 2.0 feed.Both comments and pings are currently closed.

Comments are closed.

  • Team Based PHP Training

  • Sponsors and Ads

  • Conferences I’m Attending

  • About Me

    cal_evansThis is my blog. Sometimes it's my deep thoughts, sometimes it's a journal of things I've learned. Every now and then it's my box of shattered dreams. Most of the time though, it's just the place I like to write. Sit with me as I show you some postcards from my life. While you are here, do me a favor and leave a comment.

    If you are looking for my contact information, bio, picture, ASL, check out my EPK.

    My name is Cal Evans and this is my blog.



    Follow me on FriendFeed!

    View Cal Evans's profile on LinkedIn

  • My First Book

  • Support PHPWomen


    US Shop | European Shop

  • What I'm Doing...

    • RT @ruslany If you host PHP on IIS 7.0/Windows Server 2008 today - update the FastCGI module now! http://bit.ly/bENNxa RT PLZx 3 hrs ago
    • RT @snipeyhead: YAY! @thinkgeek has the Evil League of Evil shirts in both babydoll, too! http://bit.ly/aSSFYX http://bit.ly/akHZaX 4 hrs ago
    • I'm at Cold Stone Creamery (782 Old Hickory blvd, I-65, Brentwood). 4 hrs ago
    • More updates...

  • Tags

    API article Cal Evans codeworks conference contest cw09 developers devzone elizabeth naramore Exim flex fun IBuildings iPod jobs Kathy Evans linkedin Management Marketing microsoft MySQL Nashville phar photography PHP phparchitect php developers podcampnashville podcast podcasting poem Programming Quickies respect Silly-Con Valley sixty second tech software development terry chay twitter upgrade video wordpress zend zend framework

  • RSS PHP Podcasts

    • oddWeek Episode #4
    • Creating Custom Zend_Form Decorators
    • Habits of Highly Scalable Web Applications
    • PHPSPCast #6 – Ao vivo da Campus Party (Q&A)
    • php|architect Podcast: oddWeek #002
    • Podcast #2010-02: Stalker Edition
    • php|architect podcast: oddWeek #003
    • php|architect Podcast: oddWeek #001
    • php|architect Podcast: 2009 in Revue
    • php|architect Podcast: Bake’n’Shake

  • XBox Gamer Card

  • Me

    • Best web design company
    • Cal Evans Dot Com
    • Cyrano’s Apprentice
    • Evans Internet Construction Company
    • My Life as a Child
    • PHP Podcasts
    • Sixty Second Tech

  • RSS My Blog at php|arch

    • An error has occurred; the feed is probably down. Try again later.

  • Flickr Recent Photos

    Blue Parabola Southern Office-Rear Annex is closed for snowSnow Heart@dzuelke getting ready to give his talk@fabpot talking about Dependancy Injection@derickr giving the opening keynotePeople meeting other peoplePHP Benelux Goody Bag ContentsCheck InDSCN2280The main room

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me
    • PHP
    • podcasting
    • Programming
    • SQL
    • Technology
    • Web 2.0
    • wordpress
    • WordPress Plugins
    • writing
    • zend framework

  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org


Postcards From My Life is proudly powered by WordPress
Entries (RSS) and Comments (RSS).