TweetFollow Us on Twitter

User, Meet Apache. Apache, Hug.

Volume Number: 19 (2003)
Issue Number: 7
Column Tag: Untangling the Web

Untangling the Web

User, Meet Apache. Apache, Hug.

by Kevin Hemenway

Or: How To Learn More About Your Cuddly New Web Server.

Hopefully, if you were at all interested in the last column (MacTech, June 2003), you've mentally prepared yourself for the coming months of web serving hemming and hawing. I'll continue to assume that you know more about your own network and internet provider than I ever could, and as such, will only touch briefly on ISP-related workarounds. In this installment, we'll turn on our web server, explore its directory structure, and learn how to interact via the command line. You should know how to operate Apple's Terminal application - if you don't, I highly recommend brushing up on Chris Stone's series, Learning the Terminal in Jaguar, on MacDevCenter.com.

HARTMAN: Today You People Are No Longer Maggots

A word of encouragement: 63% of the web sites on the Internet are using Apache (according to a 2002 report from http://netcraft.com/). Apple, convincingly enough, has included Apache in its own OS X and OS X Server products. It isn't a "ported" version or a slimmed-down feature set, but rather a full-fledged implementation of Apache with all the fixin's. Much of what you learn in these articles will apply to any installation of Apache, regardless of whether it's on Mac, Linux, or even Windows.

If you're familiar with the differences between Mac OS X and Mac OS X Server, you're probably aware that Server contains more GUI based administration tools for programs like Apache, sendmail or MySQL. This doesn't indicate that one Apache is better than the other - everything you can do in Server can be done in the consumer OS, and vice versa. As of this writing, these articles assume you're using the consumer Jaguar, and not the Server version (which we won't be covering).

Alternatively, a word of possible disillusionment: much like any web developer worth his salt will use a raw text editor like BBEdit (which I prefer, see http://www.barebones.com/) to code their HTML, most system administrators eschew the need for hand-holding GUI tools and dive right into text editing a configuration file. This is decidedly un-Mac-like but, until a few years ago, so was the thought of including Linux (or, more accurately, BSD) as an under layer. If you want to become proficient at web serving or programming, you're going to have to get used to editing configuration files. Yes, there are GUI based tools available, but you'll do a lot better if you learn to fish, rather than plunking quarters into a vending machine (that made sense, right? Right!)

Enough soap-boxing. Commence the casting.

KAHN: Butterfly In The Sky... I Can Fly Twice As High!

There are a few different ways you can get a rise out of Apache, the most immediate of which is through the Macintosh GUI. In Jaguar, this setting is hidden underneath the Sharing System Preference; open that now (Apple Menu > System Preferences... > Sharing). The first tab we see, Services, lists a number of capabilities we can turn on or off, as well as our current network address (which may or may not be accessible to the outside world).

In Figure 1, you'll see that "Personal Web Sharing" has been highlighted. To turn our Apache web server on, either put a check in the box on the left, or click the "Start" button on the right. A few seconds later, we'll see the results shown in Figure 2. Of special interest is the new information at the bottom of the screen - the first URL is the home of the primary web site of your machine, and the second is the address of the current user's personal web site.


Figure 1. The Services tab of the Sharing System Preference.


Figure 2. The Apache web server has been started.

Depending on your network or ISP's configuration, you may be able to type (or cut and paste) those URLs into your browser's address bar and see the default pages of your built-in web sites (Figure 3 and Figure 4). If you don't see those pages, or else get an error message concerning connectivity, you should run through some of the steps in last month's column to try and determine your external IP and whether it's viewable to the outside world. For now, you should be able to follow along by using http://127.0.0.1/ and http://127.0.0.1/~username/ respectively.

Figure 3 shows the default web page that is shipped with most Apache distributions - it's just a quick confirmation that the Apache web server is up and running smoothly. Ultimately, you should see this page only once (that once is now). You'll also be informed that the Apache documentation can be accessed from http://127.0.0.1/manual/.


Figure 3. The default root web page of Apache.

Figure 4, on the other hand, shows a friendly blurb that Apple created to ease new users into their virginal web serving experience. It briefly covers what we just did (turning on the web server) as well as quick definitions of HTML and Apache. You should read over both the default pages - you'll probably be deleting the files that represent them shortly.

But, where are these files physically located? The first URL, being the root location of the web server, is served, semantically enough, from the root web directory of Apache: /Library/WebServer/Documents. The second URL, being user specific, is served from /Users/username/Sites (as explained in Figure 4).

And that, as they say, is that. Your Apache web server is running, you're seeing the default web pages, and you could publish vanilla HTML with nary a peek at the underlying sprockets and cogs. So far, though, this hasn't been very satisfying. It's hard to feel good about yourself when all you did was click a button and piddly-type a URL.


Figure 4. The default user web page from Apple.

BATISE (TRANSLATING): If No Pain, Nothing Good Is Born

Being spelunkers of the technical sort, however, it's time we dig deeper. To do so, we'll open up a Terminal and type httpd -V which gives us the results shown in Figure 5. What we're doing is asking Apache (represented by the shell program httpd) to show its compile time settings. These will change depending on your distribution (Redhat's output will be different from SuSE's output, which will be different from OS X's, and so forth). I'll explain some of the more important entries below.

Note: The screenshot, and my explanations, are based off version 10.2.6 of OS X. If you're following along with 10.1 (waiting for Panther, eh?) or earlier versions of 10.2, you may see a slightly different output. Don't fret... the differences rarely equate to much importance, and when they do, I'll make a glib comment.


Figure 5. The results of an httpd -V shell command.

The first bit of info is the server version of the currently installed Apache (and, for the esoteric, the timestamp of when it was actually compiled). Thankfully, Apple has generally been responsive with their security updates, and most OS X users are running the latest release (1.3.27, although, at the time of this writing, there were rumors of an impending 1.3.28).

The next line worth exploring is -D HTTPD_ROOT, which tells us where the Apache binaries have been installed. The most important files, like httpd and apachectl, live in /usr/sbin. All of Apache's modules (read: plugins) live in /usr/libexec/httpd. If you've had experience with Linux programs before, this layout is relatively familiar. The next line, concerning SUEXEC_BIN, can safely be ignored - suexec isn't enabled or shipped under OS X (we can, however, recompile Apache and add it ourselves. Long story. Eventually.)

-D DEFAULT_ERRORLOG, the next entry of importance, is probably the single greatest answer to all your problems before, now, and after. Whenever something goes wrong, check your error log. Whenever something goes right that shouldn't have gone right, check your error log. Whenever you suspect someone is chuckling behind your back, the error log will have their home address. "Check your error log" is the Apache equivalent of "RTFM" - before in-the-know users will answer any of your tech support questions, they'll want to know what the error log says. More often than not, the error log will tell you exactly what went wrong. Don't be embarrassed. Check your error log. The quickest and most helpful way is with tail /var/log/httpd/error_log which spits the last ten lines of any file you pass to it.

We'll talk a bit more about log files in an upcoming column, but for now, realize that a matching /var/log/httpd/access_log covers successful operations (in the sense that the original URL request garnered a "proper" response). In previous versions of OS X, you would have seen a matching -D DEFAULT_ACCESSLOG in Figure 5's output. This has since moved inside the configuration file, which is what our remaining four lines cover.

The first of the last, /etc/httpd/mime.types, contains the mapping between a file extension and the MIME type sent to the browser (or, more generically, the "requesting user-agent"). For now, we'll leave the actual definition of MIME types to a later column, but if a .jpg were served as text/html (and not its proper image/jpeg), then the browser wouldn't be able to properly render and display the picture.

The next entry is The Big One - /etc/httpd/httpd.conf points to the file that handles all the configuration of our Apache web server. It's suitably large, suitably commented, and suitably intimating, enough so that the included early warning should be taken to heart: Do NOT simply read the instructions in here without understanding what they do. They're here only as hints or reminders. If you are unsure consult the online docs. You have been warned. The last two entries in our output can be safely ignored - they're deprecated configuration files that have since been merged with the master configuration. If you get caught using them, your error log will spit out my home address.

HAMMOND: All Major Theme Parks Have Delays

If you recall from the first column, one of the ways an ISP can put a dent in your web serving plans is by filtering incoming HTTP traffic. With such a filter, any incoming requests on port 80 will be dropped, and they'll never reach your anxious and ever-ready Apache. Working around this is easy enough to serve as an introduction to editing the master configuration file. Note: For those who DON'T have an evil ISP, just mentally follow along - you'll have less mundane things to do come next column.

The first hurdle concerns saving our upcoming changes to /etc/httpd/httpd.conf. This file, being "special", requires heighten privileges to allow modifications - privileges that your user account doesn't have by default. To get these special administrative privileges in the Terminal, you'll need to preface your intent with the sudo command. Said command gives you, for that one instant, super powers - enough so to save your changes to an otherwise protected file. The downside of using sudo is that you need to be proficient in a shell editor like pico, emacs, or vi.

Myself, I prefer BBEdit 7.0's shell utility (which can be installed via their Preferences > Tools > Install "bbedit" Tool). BBEdit's utility is smart enough to know that when you attempt to save a protected file, you should be prompted for an administrative password. In my case, I'll launch into our next paragraphs with bbedit /etc/httpd/httpd.conf. For those without BBEdit, utter sudo EDITOR /etc/httpd/httpd.conf where EDITOR is your preferred program of choice. If you're new to shell editing, there's a quick tutorial on using pico in Chris Stone's MacDevCenter.com series.

However you get there, we should now be looking at Apache's primary configuration file. Within the first screen of information, you should see the warning I italicized above, and I'll warn you again: here they be dragons. Friendly Puff-like dragons, but dragons nonetheless.

In general, when you want to modify the configuration of Apache, more often than not, just do a search for your desire and you'll find something worth investigating. In our example, we've got problems with the ISP filtering port 80 traffic, so do a search for the word "port". You will find, soon enough, the following bit of text:

#
# Port: The port to which the standalone server listens.
# For ports < 1023, you will need httpd to be run as root
# initially.
#
Port 80

Here, you can see that Apache has been configured to start up on the default (and expected) HTTP port 80. Since our theoretical ISP has blocked that, we need to change it to something else. Good alternative choices are 8000, 8080, or 8088. Sadly, whatever you choose will turn all your URLs ugly... if you chose 8000, you'll be forced to give out http://127.0.0.1:8000/~morbus/ instead of http://127.0.0.1/~morbus/. Choose your poison, and save the file.

Since we've made a change to the configuration, we've now got to restart Apache. You'll become familiar with these steps as the column progresses: any time a change is made to the configuration, it won't be put into play until Apache is stopped and then started. Before we actually do that, it's often handy to run httpd -t first. Much like -V gave information about the built-in compile time settings, -t gives us some insight on our configuration file by testing it for errors. If everything's grand, we'll get a Syntax OK... if not, we'll be told on what line we actually screwed up. The benefit of testing before restarting should be obvious - it gives us a chance to fix problems before our visitors start complaining.

We can restart Apache one of two ways: by toggling the buttons in our Sharing System Preference (refer back to Figure 1 and Figure 2) or by using the apachectl shell utility. I personally prefer the shell utility, due to a shortcoming in the System Preference: if your httpd.conf has an error in it, the System Preference will attempt to "start" indefinitely, expecting a positive response that it will never get. With apachectl restart (or its implied brethren: apachectl stop and apachectl start) you'll be given an httpd -t diagnosis if anything goes wrong. Once Apache is restarted, the configuration change has taken effect, and you should be able to visit your newly tweaked http://127.0.0.1:8000/~morbus/ (or whatever actual port you chose).

Homework Malignments

In our next column, we'll begin exploring our first major feature: server side includes. Often ignored for being "too simple", they can do some fairly useful things without much effort. Until then, students may contact the teacher at morbus@disobey.com.

  • Peruse through the /etc/httpd/httpd.conf. Familiarize yourself.

  • The Apache documentation already loaded on your machine (see Figure 3) is some of the best open source documentation around. I'll refer to it from time to time.

  • Liked reading about URL design from the first column? Check out "Toward Next Generation URLs" by Thomas A. Powell and Joe Lima: http://port80software.com/support/articles/nextgenerationurls

  • Complete the ever-enlarging animal tree: maggot, butterfly, what, and what?

  • Each of the headings is a quote from a movie or TV show. Name them.


    Kevin Hemenway, coauthor of Mac OS X Hacks, is better known as Morbus Iff, the creator of disobey.com, which bills itself as "content for the discontented." Publisher and developer of more home cooking than you could ever imagine (like the popular open-sourced aggregator AmphetaDesk, the best-kept gaming secret Gamegrene.com, articles for Apple's Internet Developer and the O'Reilly Network, etc.), he's an ardent supporter of writing incorrect passwords on sticky notes, just to confuse peepers. Contact him at morbus@disobey.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more
B&H has 16-inch MacBook Pros on sale for...
Apple 16″ MacBook Pros with M3 Pro and M3 Max CPUs are in stock and on sale today for $200-$300 off MSRP at B&H Photo. Their prices are among the lowest currently available for these models. B... Read more

Jobs Board

IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
Top Secret *Apple* System Admin - Insight G...
Job Description Day to Day: * Configure and maintain the client's Apple Device Management (ADM) solution. The current solution is JAMF supporting 250-500 end points, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.