Apache Web Serving with Jaguar
by Kevin Hemenway coauthor of Mac OS X Hacks04/04/2003
Editor's note: Most people know that Mac OS X ships with a built-in Apache web server, but don't realize that it's easy to configure and run. In fact, you can host a web site in minutes, after completing just a few simple steps. But that doesn't mean that Mac OS X is a lightweight in this category. It's not. In fact, you can drill down and get very serious with this state-of-the-art serving software.
In this first installment of a (revised for Jaguar) multi-part series, Kevin Hemenway shows you how to start serving web pages directly from your Mac. In the articles that follow, he'll show you the techniques that system administrators use for maintaining robust web sites. As you read Kevin's tutorials, you'll learn the functions of some of those mysterious folders deep within your Mac OS X hard drive, and will soon become your own OS X sysadmin.
You stare at the screen, fingers twitching nervously. You've been telling the rest of the world how great Mac OS X is, basking in the sheer enjoyment of upgrading your Classic applications to jelly-powered delights. After much spouting, your boss has finally given you a chance to prove your saliva-laced rantings. The task is quite simple: "build the GatesMcFarlaneCo (see sidebar) intranet with features up the wazoo."
|
A rather stupid joke within a joke. McFarlane Toys is often considered the "one to beat" in action figures, and thus the loose association with Bill Gates. The combination of the two names resembles "Gates McFadden," who played Beverly Crusher on Star Trek: The Next Generation. Wil Wheaton, who played her TV son, Wesley Crusher, announced that he'd be reprising his role with a cameo appearance in the final Star Trek: TNG movie (the scene was eventually cut). Wesley Crusher is often regarded as the most-despised Star Trek: TNG character, and that's where we make full circle back to Bill Gates. Glad you wondered? |
Once again, it's time to convince the herd about Mac superiority. What better way to begin than to use a well-respected web server that's proven its mettle time and time again?
Introducing Apache to the Mac Faithful
Apache is regarded as the most popular web server available today. With its incredible portability and support for anything you'd ever want to do, Apple wisely decided to ship Apache with its Unix-based operating system. With this web-serving powerhouse at your fingertips, I'll explain how you can impress your boss and solidify your love for Mac OS X, all at the same time.
Getting Started with Apache and Jaguar
The easiest way to begin Apache web serving is via your Mac OS X System Preferences. Click your Apple Menu, choose "System Preferences," and then select "Sharing." Within the Sharing preference panel you'll see a number of options, only one of which is of immediate value. See that "Personal Web Sharing" service? Simply select it and then click the "Start" button to fire up the built-in Apache web server.
|
|
By default, the Apache server has been configured to serve your personal web pages from a directory based on your Mac OS X "short" user name. (You can find your short user name in the "Users" preference panel. Click on "Edit User.") If your short user name is "morbus," for example, you can access your personal web site by opening any web browser on the local network and typing http://127.0.0.1/~morbus/.
That 127.0.0.1 (also known as localhost) is pretty special--every computer has one. Both names represent the computer itself; by going to 127.0.0.1 in a web browser, you are accessing the local Apache web server you activated in the "Sharing" preference panel.
But, how do make your web pages available to folks on the World Wide Web?
The answer is found back in your "Sharing" preference panel. Go there, click the "Personal Web Sharing" service, and note the IP address or hostname listed at the bottom of the window. Using that IP address or hostname is how outsiders can access your Apache server. If you see 209.204.146.22, visitors could access http://209.204.146.22/~morbus/ to view your personal web site (the exact IP/hostname you see will be specific to your Internet connection). In a future article, I'll discuss how to limit outside access to only those you deem worthy.
Note: In some cases, you may not be able to view your own web page when you access it via your IP or hostname (as above). This could occur for a number of reasons:
Your Internet provider deliberatively restricts access to port 80, the web-serving port. They often do this to prevent users from "running their own server," a common restriction listed in their Terms of Service. The quickest solution is to change the port Apache binds to, but we won't be explaining how to do that in this series--check the Apache documentation for tips.
Your Internet provider or network has a firewall or routing issue that blocks or restricts access to your internal web server (or entire computer). Since step-by-step solutions to this differ depending on your specific configuration, we won't be getting into them--talk to your network administrator.
Depending on your configuration and browser, you may not be able to browse your own web site, even though other outside users can see it fine. This seems to occur most often when using Internet Explorer, but it'll affect only local users (i.e., you)--your visitors would still be able to see your site, regardless of their browser or OS. Oftentimes, viewing your site via IP or hostname works fine in other OS X browsers, such as Safari or Mozilla.
Your Personal Web Space
If you went to your personal web site now, you'd see a generic introduction written by Apple describing Apache and how to use "Personal Web Sharing." We'll soon be deleting this page, but be sure to give it a read, because it contains some helpful bits of information.
Much in the same way your personal web site is based on your user name, so is the location of your web space in the Mac OS X file system. Continuing our assumption of the "morbus" user name, our current web space lives at /Users/morbus/Sites/.
If you browse there now, you'll see the introductory file (index.html), as well as an images/ subdirectory. Delete (or back up) everything, and use a text editor (such as BBEdit) to create a new index.html file with the following contents:
<html>
<body>
<h1>Gleefully Served by Jaguar!</h1>
</body>
</html>
Upon saving, reload http://127.0.0.1/~morbus/, and you'll see your rather bland, boring home page. This certainly isn't very impressive--downright pathetic, actually. You can't use server-side includes, can't install CGI programs to your Sites directory, and can't use PHP or mod_perl. Plus, your intranet is wide open to the Internet. Not so good, bub.
|
Related Reading
Mac OS X Hacks |
Turning on the Magic
What we've done so far won't impress anyone. We have no features and an ugly web site address filled with tildes and unprofessional user names. We've got a long way to go before we can feel good about showing off our masterful Mac OS X box.
Our first step is to give the site a prettier URL. Above, we placed our index.html file into our own user directory (such as /Users/morbus/Sites/)--now, we'll place a copy into Apache's DocumentRoot. The DocumentRoot is the default server (not user) location from which Apache will serve documents. For Mac OS X, this is located in the /Library/Webserver/Documents/ directory. This directory also has a default introductory file--to see it, visit http://127.0.0.1/ (or http://localhost/) within your preferred web browser.
Localization or "Content Negotiation"
If you browse to /Library/Webserver/Documents/, you'll inadvertently run across another nice feature of Apache: content negotiation. Depending on your visitor, Apache can serve up international versions of your site--for example, index.html.en for English speakers, or French (index.html.fr) and German (index.html.de) versions. Apache for OS X comes with over 25 translated versions of the default index.html file.
You can take advantage of this feature on your own pages. When you're creating the GatesMcFarlaneCo intranet in your native language, you can translate your pages into other languages and add the two-letter language encoding ("de" for German", "fr" for French, "es" for Spanish, etc.) to the file name. Any time international employees of GatesMcFarlaneCo access your intranet, they'll automatically be served the page that's in their native tongue (assuming their web browser correctly identifies their native language).
For now, back up or delete all of the files you see in /Library/Webserver/Documents/ and copy over the index.html file we created a few steps ago. If you return to http://127.0.0.1/, you'll see our "Gleefully Served" page.
What's Next?
This is a good point to catch your breath and play with what I've outlined here. You might want to build some nicer HTML pages to serve as your test site. In the next installment of this series, I'll tackle CGI access for your Apache server. Until then, enjoy!
Kevin Hemenway is the coauthor of Mac OS X Hacks, author of Spidering Hacks, and the alter ego of the pervasively strange Morbus Iff, creator of disobey.com, which bills itself as "content for the discontented."
Return to Mac DevCenter.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 16 of 16.
-
Intranet not accessable anymore
2005-03-27 19:18:18 NMEfoofoo [Reply | View]
On Mac Powerbook G4, OS X 10.2.8 I am not able to start Personal Web Sharing. Have tried from System Preferences-Services.
In Sharing, the Start button remains grayed out (never turning to Stop).
In teminal this is the report
[Me-Computer:/usr/sbin] ra% sudo apachectl restart
/usr/sbin/apachectl restart: httpd not running, trying to start
httpd: bad user name nobody
/usr/sbin/apachectl restart: httpd could not be started
[Me-Computer:/usr/sbin] ra%
I recently edited httpd.conf to try to set up a blog site. I backed up the old unedited version and replaced it after giving up on the installation. The changes I made seem to be back to their original settings, but now I`m not able to access my Intranet in the Sites folder.
-
I'm still having problems...
2004-07-21 14:11:02 caprio487 [Reply | View]
So far I've done everything you said, and can access my website from my own computer and from other computers in my house that are connected to my computer through the same network. However, none of my associates can view the site from their computers.
Do you know what's wrong, and how can I fix it?
Thank you...
-
Problems with connecting to the Net or starting PWS
2004-07-08 09:48:46 guzzler [Reply | View]
I've posted this to article 1 (non jaguar too) stumbled across big composers comment when doing a google search.
router setup/manual IP
2004-07-08 08:37:08 guzzler [Reply | View]
I'm having the very same problem :( I tried out the articles last night. All was well. This morning when I rebooted all my problems started. I followed instructions from art. 1-4 related to Jaguar and 5 and 6 from the earlier series coz i just couldnt wait! And now i've got the internet back but when i click on personal web sharing it gets stuck at starting... also i can no longer see my local site from the Sites folder and not from the Webserver/Documents folder.. Any help will be highly appreciated since the computer i've been fiddling around on is not mine!! :(
2004-06-15 20:51:58 bigcomposer [Reply | View]
Ok -- I'm not sure if anyone is still reading this or not, but it's worth a shot.
I'm having some really strange things happening. I'm trying to get a simple server up and running using Jaguar. I've followed all of the instructions and understand most of it.
Problem #1
I have configured my router to pass requests to port 80 to a specified IP address. In my case, I'm using **.**.**.25 -- where the first three fields are the first three digits in the routers IP address (Using a simple Belkin router, I'm unable to change these anyway).
My problem occurs when I try to configure the server IP. When I set it to get an IP via DHCP, I have an internet connection; however, when I choose Configure IP Manual" in the Network Control Panel and enter all of the information, I lose internet connection. For the manual configuration, I type in the correct IP, Subnet Mask, and Router IP.
Interestingly, when I swith back to get IP via DHCP, the router sets the same address that I have in Manual and I then have an internet connection. Of course I don't want to have a dynamic IP on my server (ie, if my router has to be reset, etc... it may assign my server another IP).
Does anyone have an idea? Ive also tried using DHCP with a Manual Address with the same results....my computer doesn't see the router.
Problem #2
Just to test it out, I left my computer set to using "DHCP" to obtain the IP address. I then simply configured my router to pass web requests to the IP it assigned to that computer. However, when I tested it from another computer (ie, entered my permanent IP in a browser), I did not see my webpage. Instead, I got the front-page utility of my router! This freaked me out and I disabled websharing and the virtual server of my router. Again, any help will be very appreciated.
Any help will be appreciated!
thx,
Mike
problems starting web sharing
2004-02-18 06:20:17 jonners [Reply | View]
"I wanted to restart or check that apache was workign so went to system preferences and turned web sharing on.......but is says, ;starting' and just stays like that and never starts. Can anyone think why?"
(only kept what applied to me from jon's note)
Thanks,
jon
-
PWS hangs
2004-06-12 16:34:37 Durer [Reply | View]
on my osx 10.2.8 the PWS hangs. doesnt start, it says starting but never does, grayed out.
-
Not Able to Start Personal Web Sharing
2004-04-08 21:47:10 bhuddles [Reply | View]
On Mac Powerbook G4, OS X 10.2.8 I am not able to start Personal Web Sharing. Have tried from System Preferences-Services and have tried from Terminal.
In Sharing, the Start button remains grayed out (never turning to Stop).
In teminal this is the report
[Bruce-Huddlesons-Computer:~] brucehuddleson% sudo /usr/sbin/apachectl restart
/usr/sbin/apachectl restart: httpd not running, trying to start
fopen: No such file or directory
httpd: could not open document config file /etc/httpd/httpd.conf
/usr/sbin/apachectl restart: httpd could not be started
[Bruce-Huddlesons-Computer:~] brucehuddleson%
The httpd.conf file is stored in Macintosh HD/Previous Systems/Previous System 1/private/etc/httpd/httpd.conf
Do I need to move the httpd.conf to another directory? If so which directory and how do I move it?
Thanks for assisting a newbie.
-
"Users" now in "Accounts" prefs
2004-01-10 05:52:25 anonymous2 [Reply | View]
Just a minor correction for any real newbies. Finding your username under Jaguar, the following instructions are incorrect:
'find your short user name in the "Users" preference panel. Click on "Edit User.")'
It should now read:
'find your short user name in the "Accounts" preference panel . Select the " Users" tab and click on "Edit User...")'
If anyone is doing any updating. It's good to keep this article around for those who are sticking to 10.2 and allowing others do Panther bug testing.
- Dave Everitt
-
Can view web site on LAN but not WAN
2003-12-02 09:04:02 kidloco [Reply | View]
Hi,
I've been trying to set up my G4 to run Dynamic web sites, my web site has been tested on P.C's and works fine on both LAN and WAN. But for some reason whenever i use my Mac to host the site it says "Forbidden 403" when trying to access the site from the internet but runs fine on my network. i thought it could be the dynamic content so i removed this making the site static and still no joy. the site is stored inside /library/WebServer/Documents/site folder/ which should work fine. The Mac is connected to a network of 4 Mac's and the internet connection goes through a hub and a router, do you think my router could be causing this problem????
cheers all.
rik_mcclure@hotmail.com
-
no http - access!
2003-07-11 01:13:15 anonymous2 [Reply | View]
How is it possible that ... apache claims to be running (the buttons do) but I couldn't access
any file through http://...
where could I start to look for the error? (there shouldn't be any, it's newly installed: osX 10.1.2 on an external 'fw-hd')
I'd be immensly grateful for any suggestion
THANKS!
-
changing ip address to letters
2003-06-02 18:48:16 anonymous2 [Reply | View]
I know that you can change the ip address in jagaur to different numbers. But is there a hack that would let me add letters to my network ip address. So that I could open my website as that on my computer or another computer on my ethernet?
-
Trouble With Making A Page
2003-05-24 16:39:57 anonymous2 [Reply | View]
I have a pre-existing index.html page made with Dreamweaver. I followed everything the article said but I still can't get it to work. I can't connect to my site through other computers. I also have Airport, and I can't connect through the IP listed in the Airport Admin Utility. Please help me
-
No index of folder in Sites
2003-05-20 03:52:12 anonymous2 [Reply | View]
Why can't I get an index on my personal web sharing web page? I have checked permissions on the folder in Public that I want to index. I can access the files inside if I type the exact path in my browser. I get error 403 (permissions).
-
Automatic direction to user folder?
2003-05-11 07:10:54 anonymous2 [Reply | View]
I'm running an apache server at home (Mac OS X) & would like to shorten my URL. Is there a way to configure apache so that it automatically goes to /~morbus/sites/ ???
This would eliminate the extra /~morbus (which many people are stumped by) from my URL.
-
V Host
2003-04-09 08:15:35 anonymous2 [Reply | View]
Thanks for the series update Kevin. In the (NEAR) future, can you do a step by step for v hosting ? TIA
-
Virtual Hosts
2003-04-06 16:03:45 anonymous2 [Reply | View]
Is there a simple tool that allows the creation of Virtual Hosts (I am aware that Mac OS X Server, Tenon's iTools and Webmin do this).
I am after something simple like the Weblock Pref Pane for configuring htaccess, or the MySQL pref pane that Aaron Faby has created (www.aaronfaby.com).
Thanks
Hamish
-
The php form page that kept disappearing...
2003-04-05 07:16:27 anonymous2 [Reply | View]
I have just activated PHP on the Apache server on my iBook, but I have one slight problem.
I have a php-generated form page that works fine on my web-host's apache server, but only partly works on my machine.
When accessed initially, the page loads up and processes it's php content perfectly. But when you submit the form (which posts to itself), the browser reports a failure to load the page.
I have a few other php pages that are visible in directories viewed by the browser, but cannot be found when I attempt to actually view them in the browser.
Any thoughts, anyone?







In Sharing, the Start button remains grayed out (never turning to Stop).
A8A8A8
In teminal this is the report
[Bruce-Huddlesons-Computer:~] brucehuddleson% sudo /usr/sbin/apachectl restart
/usr/sbin/apachectl restart: httpd not running, trying to start
fopen: No such file or directory
httpd: could not open document config file /etc/httpd/httpd.conf
/usr/sbin/apachectl restart: httpd could not be started
[Bruce-Huddlesons-Computer:~] brucehuddleson%
The httpd.conf file is stored in Macintosh HD/Previous Systems/Previous System 1/private/etc/httpd/httpd.conf
Do I need to move the httpd.conf to another directory? If so which directory and how do I move it?
Thanks for assisting a newbie.
Not Able to Start Personal Web Sharing
2004-05-08 20:42:30 keath [Reply | View]
If you haven't fixed this already, the answer is that the config file should be in '/etc/http/' and named 'httpd.conf' as the computer is requesting.
You can copy it to the right place using the terminal. Type 'sudo cp /Previous\ Systems/Previous\ System1/private/etc/httpd/httpd.conf /etc/httpd'
Not Able to Start Personal Web Sharing
2004-12-05 12:03:43 PeterEliasson [Reply | View]
I have the same problem. it DID work, but I must have messed stuff up. Now when I used the command line
sudo cp /Previous\ Systems/Previous\ System1/private/etc/httpd/httpd.conf /etc/httpd
I get the answer
cp: /Previous Systems/Previous System1/private/etc/httpd/httpd.conf: No such file or directory
I'm a total newbie so I'm so regretful. How can I make things right again?