Category: Mac, Category: Server, Category: Websites
Originally posted on my iBlog 8 January 2005.Summary
: I've just set up our old 300MHz slot-loading G3 iMac as a server running OSX 10.3.9 over our TelstraClear cable broadband connection as one of several computers on an AirPort wireless network. The tricky bit (less tricky when I'd figured it out) was serving several website domain names from our one static IP address. Here are my notes on how this was done. Perhaps it's of use to one of you excellent people in web land.A clean start
I decided to start everything from scratch, since our iMac (named Rinchen Barsbold) has had a long and hard life before becoming a server. First, I erased the hard drive with DiskUtility. Panther (OSX 10.3) was reinstalled and updated to 10.3.9. BBEdit was installed as my preferred text editor. To save space on our 10GB hard drive, all printer drivers were deleted (from /Library/Printers/). Superfluous Apple applications were removed (iMovie, iPhoto, etc.). Monolingual (monolingual.sourceforge.net/
) was run to remove all language files except English, Australian English, and US English. Well over a GB was saved, just like that.Start web sharing
Making the iMac a server is dead simple in OSX. In the Sharing control panel of System Preferences, turn on Personal Web Sharing. That's it! This starts Apache, the most popular open-source web server software, which is automatically built in to OSX. While in the Sharing control panel, I also named the computer again and turned on Personal File Sharing (so I can access the iMac from inside our wireless network) and Remote Login (so I can access it from work).
I also clicked on Firewall tab in the Sharing panel and started the firewall. All ports will be shut to external traffic except those involved in the above sharing.
In System Preferences/Network, the TCP/IP computer is set to DHCP. This gets the IP address and other connection information from our AirPort Express wireless base station. My iMac got given the IP address 10.0.1.4. (We plug our broadband cable modem ethernet cable into this little gadget from Apple and our Macs can all surf the web at once. Pretty cool. We also have our stereo plugged into the Airport Express so we can play our iTunes music through our stereo wirelessly. More cool.) The IP address 10.0.1.4 just happened to be the IP address the Airport Express had dynamically given the iMac when I was setting this up. There's nothing magical about it. Just make sure the your server has the same IP address as you set in Airport Admin Utility to receive external web traffic (details on that are below). Before continuing, I switched over the Network from "Using DHCP" to "Manually", retaining the same network settings, so that if I ever had to restart the iMac or reset the Airport, this internal network IP address of 10.0.1.4 would be retained.
I also went into the Energy Saver control panel of System Preferences and changed the computer to never sleep and for the screen to dim immediately (the hard drive can still spin down when not in use). I didn't want our sever caught snoozing when someone wants to visit our website.Set up Airport Admin Utility
Airport Admin Utility is an application in the Utilities folder of the Applications folder. I won't go through the settings for getting Airport set up in case there are security issues in making this information public. It was simple with the info from our broadband provider. The important point is that we get a single static IP address (a number of the form 22.214.171.124) which is the physical address of our network on the web.
The other trick to set up port mapping so requests from afar for normal internet ports get directed to our iMac server (and not one of our other Macs which might be on the wireless network at the time). Remember that the iMac has the IP address of 10.0.1.4 inside our wireless network. So we use the Port Mapping window to direct all web traffic through the standard internet ports to this IP address. (Note that I've masked out the numbers for safety.)
Now, typing http://126.96.36.199 (or whatever your IP address is) into Safari (or your favourite web browser) will bring up Apache's default webpage. This will happen from anywhere in the world. I was now serving a website, but I wasn't quite there yet.Domain name registration
Of course, http://188.8.131.52 isn't much of a web address. I went to www.domains4less.co.nz
and bought the domain names I wanted. I got mollivan.com, canterburynature.org, and canterburynature.org.nz. My plan was to serve two websites from the iMac, our own website (www.mollivan.com ) and a prototype for a community website on nature in the Canterbury region (which I wanted both addresses to point to).
Now here's where I got a little confused. As it's name suggests, www.domains4less.co.nz
specialises in selling cheap domain names. While it allows you to direct your purchased domain name to another existing website, it doesn't allow you to set its domain name servers to point your new name to your own IP address (at least I couldn't find anywhere on its site that would allow me to do this properly). In my website dealings, I hadn't struck this before and it took me a while to figure out how to get around this. What I could do was use their URL direct settings to automatically direct anybody who typed in www.canterburynature.org.nz to www.canterburynature.org.
As a perhaps useful aside, note that some odd stuff happened before I figured out that URL redirects are not equivalent to a proper name server. For example, when I redirected www.mollivan.com to our IP address, and then typed www.mollivan.com into Safari, I would see the correct default OSX webpage. But when I tried to then navigate into a folder ("bob") that I created within the default OSX web Documents folder, by surfing to www.mollivan.com/bob/ I would get the following webpage warning:
You don't have permission to access /bob/ on this server.
It took me ages to realise that this wasn't due to faulty permissions on my folder, or the folders it was inside (this was the solution mentioned on websites I found when googling this warning). Instead, it seems to be because the iMac server didn't know what to do with http://www.mollivan.com/bob/ request. I figured out this by temporarily redirecting to the iMac another domain name I had access to from another domain name company. Unlike www.domains4less.co.nz
, the website of this company (www.discountdomains.co.nz
) allowed me to set my IP address in their name server. In this case, the index.html file inside Bob loaded fine without warning.
The solution I took was to go to www.everyDNS.net
("DNS for the rest of us"), which offers free use of its domain name servers (thanks!). There are several free DNS services out there. From my googling, everyDNS seemed like a good combination of ease-of-use and competence. I registered (free!) and set up both www.mollivan.com and www.canterburynature.org to point to the static IP address of our broadband cable modem (e.g., 184.108.40.206, not my real address).
My everyDNS settings for mollivan.com (real IP address replaced with 220.127.116.11) were:
*.mollivan.com A 18.104.22.168
mollivan.com A 22.214.171.124
The asterisk (*.mollivan.com) means that any other web address I set up inside mollivan.com (e.g., jon.mollivan.com) will also be directed automatically to our IP address.
I did the same for canterburynature.org.
The everyDNS.net website gives the names of their name servers (e.g., ns1.everydns.net). So I then went back to the www.domains4less.co.nz website and entered two of these name servers in as the master and slave external name servers for my domain names. Domains4less required IP addresses for the name servers as well as their names. The everyDNS.net website only gave the names. So I looked up the IP address for these name server addresses using the "whois" function of Network Utility (another useful Apple application in the Utilities folder of the Applications folder).
Then I waited. I had read on the web that it can take 24-48 hours for DNS changes to filter through the web. Indeed, it was about a day later when typing my domain names into Safari took me to my default OSX webpage.Using virtual hosting in Apache to host several domain names from one IP address
"Virtual hosts" can be used in Apache to direct visitors of different domain names to different folders within a computer with a single IP address. This turned out to be quite simple to do. But I only figured that out after following a number of complicated false leads that dealt with using virtual hosts to allow you to type in a domain name into Safari while off-line and test out a website in development.
The solution is based on Scenario Two in the useful ONLamp.com article by Russell Dyer titled "Simplify Your Life with Apache Virtual Hosts " (www.onlamp.com/pub/a/apache/2003/07/24/vhosts.html
Remember that Apache is the open-source application that runs the web server in OSX. Its settings are contained in the hidden file, httpd.conf, which is found in etc/httpd/ folder (i.e., in your Mac computer above your user directory). Note that this is hidden. Try to browse there in the Finder and you won't even find the "etc" folder. Macs conveniently hide a lot of these things, I guess in part to stop regular users like me from screwing things up. So what are you supposed to do?
There are several solutions. My favourite is to use BBEdit
, quite possibly the best text editor in the world. You can open hidden files (File:Open Hidden...). So I use BBEdit to open httpd.conf and make the necessary changes to the settings. When opening in BBEdit, the default will be set to open All Readable Files. You might need to switch this to All Files since at least my version of BBEdit (7.1.4) does not recognize files with the extension ".conf" by default.
I changed two things in this file.
# DocumentRoot "/Library/WebServer/Documents"
Here I comment out the first of these lines (search for it in the file) and add the second line below it. This switches the computer website from the folder Documents to the Sites folder inside the mollivan User. This way, if I ever set up another user on the iMac, the websites will still only be server from inside the mollivan user.
Next, I went right to the bottom of the file and added the following lines.
Russell Dyer's article explains well what all this code is doing. In summary, it says that when requests are received at port 80 (the usual port for website requests), the website shown will depend on which web address the user has typed in. Requests for www.mollivan.com are directed to the mollivan folder inside the Sites folder. Requests for www.canterbury.org are directed to the canterburynature folder inside the Sites folder. The homepages for each site are the index.html files inside each of these folders. (In my case with only a single IP address, the asterisk (*) in this code could be replaced by my IP address.)
Once done, save the file and restart Apache by going to System Preferences:Sharing and turning Personal Web Sharing on and off (or you can use the command sudo apachectl restart in Terminal).
If you don't have BBEdit, you can also use Terminal to make all these changes, especially if you are comfortable with UNIX. Terminal is the application in the Utilities Folder of the Applications folder that allows you to interact with the rock-solid UNIX core of OSX. In a Terminal window, type the following commands.
this changes the directory to httpd (the hidden folder)
sudo emacs httpd.conf
this opens the file http.conf in the text editor emacs, included inside OSX. Sudo allows you to access places you shouldn't otherwise go (you'll need to have administrative rights on your computer and you'll have to enter your password).
Scroll down inside emacs and make the above changes. Use ctrl-x ctrl-s to save your changes and ctrl-x ctrl-c to close emacs.
Then restart Apache as above.That's it!
When I did all that, my iMac as happily serving two different websites from the same static IP address through our home broadband connection.
My next steps are to turn on PHP and install MySQL and PHPMyAdmin to add some dynamic features to my websites. But that's a different story.