The Mac OS has had a web server bundled with it since Mac OS 8 debuted in 1997. Traditionally, Apple has referred to the included server software as Personal Web Sharing or just Web Sharing. While little is known about the web server that shipped with OS 8 and OS 9, OS X has included Apache, the most popular web server in the world, which is capable of far more than “personal” sharing – ever since version 10.0 (or earlier, if you count developer versions and the Public Beta).
At first glance, it appears that OS X 10.8 Mountain Lion breaks with tradition and ships with no web server.
It turns out that Apache is still alive and well in Mountain Lion, but Apple has chosen to remove its checkbox from the Sharing pane in System Preferences. Why they have done that is anyone’s guess; my personal supposition is that hardly anyone used the built-in Apache in OS X, and I imagine the Web Sharing checkbox probably confused more than a few OS X users who were looking to turn on Internet Sharing, which can route your Internet connection from one network port to another – in from WiFi and out to ethernet, for example.
Whatever the reason, I was more than troubled after I installed Mountain Lion and went to do my normal OS X tweaking. I always like to have Apache, PHP, and MySQL up and running on all my Macs so I can do some quick web testing – or even install a web-based app I might like to have around for personal use. The good news is that Apache is alive and well in Mountain Lion, despite a lack of a way to enable it in the GUI. A quick trip to the Terminal will get you serving web pages once again.
The relevant Terminal command is called apachectl, which is short for “Apache control”. When combined with the old standby sudo, which allows us mere mortals to run commands as the all-powerful Unix user “root”, Apache web sharing is started in Mountain Lion (or any previous version of OS X, if you are so inclined):
sudo apachectl start
You will be prompted for your OS X password (your account needs to be “Administrator” status to use sudo), and Apache will fire up in the background.
With Apache started, fire up your web browser of choice and enter the URL http://localhost/ and you should be presented with a page that says simply “It works!”. Indeed, this is your verification that Apache, version 2, is up and running on your Mac.
As with previous versions of OS X, the directory that serves up web pages is /Library/WebServer/Documents/. Place your HTML files in there and watch them get served up in your web browser.
If you want to stop Apache, take another quick trip to the Terminal, and enter – you guessed it:
sudo apachectl stop
There’s also another option for apachectl besides start and stop – graceful, which waits until all open connections to Apache terminate before restarting the server. When running a personal web server, with yourself as the only client, you’re not likely to benefit from this command; plain-old stop will suit your needs just fine.
As has become the norm for OS X (and Linux and other modern, user-friendly Unix-based operating systems), someone quickly developed a GUI app to run these commands for you. OS X and Web development firm Click On Tyler has whipped up the Web Sharing preference pane. This gives you a nice toggle switch in System Preferences for turning Apache on and off, just like previous versions of OS X had in the Sharing pane.
But what about enabling PHP and installing MySQL? This requires more Terminal work, which I’ll cover in future editions of Plays Well with Others. So while the System Preference pane is nice, you might as well get comfortable with the Terminal, because we’ll be taking yet another trip back there next time so that we can serve up dynamic PHP files, rather than static HTML, and also link up the MySQL database server to PHP in Mountain Lion. (And a third part of this series will look at MAMP, which bundles its own Apache, PHP, and MySQL for a completely Terminal-free experience.)
Keywords: #enableapache #restorwebsharing #osxmountainlion
Short link: http://goo.gl/JFoYvW
searchword: enableapache, restorwebsharing