Archive of March 2010

Share an Apache Config With Dropbox

Like many, I run local development environments. I have no love for a shared development environment. Also like many, I split time between two computers—one at home and another at the office. Finally, still clutching the “like many” mantra, my work-life balance kind of sucks. My vocation is also my avocation, so when I’m working on something interesting, it follows me around from location to location, computer to computer with no regard for this mythical concept of balance.

I’ve always had different systems for work and play and it’s no secret that I’m a huge fan of Dropbox, so sharing what I need to and can has always been part of my setup. In the past, though, my systems have always been heterogeneous; a Mac at home, Windows or Linux at the office. Because the environments were different enough, sharing was often a rudimentary effort that involved multiple variations of a file that was optimized for its runtime. Still useful for access and versioning, but there’s no meaningful sharing going on there.

These days both of my machines are Macs these days (yay!) and both run Apache installed via MacPorts, so a few months back I decided it was time to share properly. My httpd.conf file was already in my Dropbox, as were a few projects that I like to have access to on either machine so I expected this to be easy. And it was.

Except that it wasn’t. The sand in the gears is that my username on each machine is different. That makes my home directory different on each machine and Macs, more so that either Windows or Linux, really encourage users to keep everything in their home directories. To some degree, that last statement is my own projection—I realize that it’s possible and even easy to install and store files anywhere—but I’ve really liked keeping everything in that tight of a grouping and wanted to continue doing so. That left me with the problem that I couldn’t hard code my paths.

Before we go on, I should outline my configuration at a very high level. I do everything web-related in virtual hosts. The reasons for doing so are beyond the scope of this post, but suffice to say that it’s something I think every developer should be doing. The fact that I still see developers working in circa-1998 directories beneath a single web root makes me crazy.

Anyway, back to the sharing. My Dropbox is in the standard location at ∼/Dropbox and my development environments are all in ∼/Development/domains. Similarly, for convenience, I keep my virtual host configs in individual files that I can easily include or uninclude. Those that I want to be able to access on either machine are stored in my Dropbox (∼/Dropbox/Application Support/apache/conf.d) and those that I don’t are stored in my Application Support directory (∼/Library/Application Support/MacPorts/apache/conf.d).

That’s a lot of resources tucked neatly into a directory that’s different on each machine that I want to share across, but fortunately Apache understands environment variables, so I just tweaked my httpd.conf and shared virtual host config files. I replaced each path that explicitly referenced my home directory with ${HOME}. For example:

Include "${HOME}/Dropbox/Application Support/apache/conf.d/*.conf"
Include "${HOME}/Library/Application Support/MacPorts/apache/conf.d/*.conf"

Once complete, I bounced Apache and everything worked.

Except that it didn’t. You didn’t really think it’d be that easy, did you?

Some time later, after a reboot, I noticed that Apache didn’t start automatically like it always had in the past. I don’t reboot often, so my Apache config changes were ancient history; I just chalked it up to a hiccup, started Apache manually and went on with my day. Eventually, in spite of the infrequency of reboots, I came to recognize a pattern. Something was wrong.

This post has become longer than I intended, so I’ll cut to the chase. The problem is that, at boot, Apache isn’t starting as me, so it doesn’t understand the ${HOME} directory I told it to use. Once booted, that’s not a problem, so manual starts worked just fine. I tried several solutions and asked questions related to this on StackOverflow here, here and here (in the order of their asking). Eventually I had to settle for a Linux-like file system config coupled with symlink usage to maintain my OS X consolidation.

I already had my Apache config file (/opt/local/apache2/conf/httpd.confJ) symlinked to a physical file in my Dropbox, so that was fine. Next I needed to access my shared and local virtual host config files, so I created /opt/local/apache2/conf.d. Inside of that directory, I created two symlinks. local pointed to my non-shared virtual host files in ∼/Library/Application Support/MacPorts/apache/conf.d and shared pointed to the shared config files in ∼/Dropbox/Application Support/apache/conf.d.

Next I needed to be able to access my development environments from outside of my home directory. I chose the Linux-like path of /var/www. I created a symlink such that /var/www pointed to ∼/Development/domains.

Finally, I just updated my Apache config so that it loaded the virtual host config files using a relative path:

Include "conf.d/*.conf"
Include "conf.d/*.conf"

And updated all of my virtual host configurations so that all resources were being accessed through /var/www instead of my home directory.

Git Tip: Ignore Changes to Tracked Files

Every once in a while, I find myself working on a project that forces me to modify key files—often config files—in order to get it running locally. In those cases, the last thing I want to do, for a number of reasons, is to commit those changes. That’s hard to do, though, since I regularly use git add . and/or git ci -a to commit everything I’ve changed. Make enough changes in enough files that you don’t want to commit and these changes begin to cause as many problems as they solve.

As is so often the case, it seems, Git comes to the rescue with its update-index command. Reading the documentation, it’s not really intended for this purpose, but its effectiveness as a “coarse file-level mechanism to ignore uncommitted changes in tracked files” is recognized. To apply it, simply make a change to a committed file, say, database.yml and execute git status. Git should report the modified file. Since we don’t want to commit, we don’t want to see this listed as a modified file until the end of time and we can’t ignore it (because it’s already being tracked), we need to tell Git to assume the file is not changed.

git update-index --assume-unchanged path/to/database.yml

I’ve been using this command since I learned of it a few weeks ago and it works perfectly for this use case. Inevitably, though, a question will arise:

What files have I marked this way?

Since those files will no longer appear in the modified list, can’t be easily found in a .gitignore file or exposed by removing a .gitignore file, there will eventually be a need to know this. Maybe you’re trying to get another instance running or maybe you’re just the curious sort and you’ve forgotten. Like many things in Git-land, the functionality exists, but is far from obvious. I asked on StackOverflow and Andrew Aylett provided the answer I was looking for.

If you ever find yourself needing to know, this command will display the files that have been marked as —assumed-unchanged.

git ls-files -v | grep -e "^[hsmrck]"