Using Mac OS X to Serve Web Pages
For most Mac users, a hosting service is the way to go. Hosting services handle the hard work of maintaining the hardware and software that is needed to serve your site to the Web. A hosting service must also deal with all the hacking to which most Web sites are subjected. A hosting service enables you to concentrate on your Web site rather than the Web server that provides it to the Web. Additionally, you can get many other services that make your Web site even more powerful (such as domain name registration).
However, there might be situations in which you want to be your own hosting service. If you have a site that will be accessed in a limited fashion by people outside your local network, hosting your own site can be an easy and effective way to share information. Also, you can easily host a site that will be accessed through your local intranet.
One of the great things about Mac OS X being based on Unix is that many of the tools available for Unix work with it as well. And when it comes to the Internet, most of the back end runs on Unix, so many useful tools are available.
Even better news is that Mac OS X includes very powerful and sophisticated Web server software Apache. This software is widely used across the Internet to serve Web pages, and Mac OS X users can take full advantage of it out of the box. Although Apache is a Unix application, you can perform the basic tasks of serving a Web site without messing around with text commands. Of course, to really customize and master Apache, you do need to get your hands into the command line. However, you can get a site up and running without ever interacting directly with Apache.
Establishing and maintaining a Web server is a complex and challenging task, and explaining all the details associated with hosting a Web site is beyond the scope of this book. However, in this section, you will learn the basics of hosting a Web site on your Mac and see how easily you can get started doing so. From there, you'll see pointers to more information so you can take your Web hosting to the next level.
If you plan to have a very active Web site with heavy traffic, you will need a dedicated machine to act as a Web server. In fact, if you want to be serious about hosting a Web site, you are foolish to use your "production" Mac. Bad things can happen to Web servers, so you should never host a Web site from a machine that you also use to do work. (However, this is feasible if your Web site will be accessed by only a few people and will have little traffic.) The biggest threat to a Web server is the hackers who love to break into such servers and wreak havoc. Because your Mac should be directly connected to the Internet to be capable of serving a site, it will be vulnerable to attack. If you use your production machine to host Web pages, you are opening yourself up for trouble. This is a prime advantage of using a hosting service because that service has to deal with the security of its hardware and software. There is no direct link between your Web site and your production Mac.
The general process for using for your Mac to host a Web site is the following:
To host a Web site, you need to have the following:
Hosting a serious Web site is resource- and labor-intensive, as you can see from this list. (A hosting service looks better and better, doesn't it?)
If you try to host a site from a "consumer" ISP, make sure that your license agreement does not prohibit such activity. For example, many cable modem service providers explicitly prohibit you from hosting a Web site through your Internet access account.
Registering Your Domain
Web sites are designed for people, and people don't like to deal with IP addresses. They much prefer working in letters and, even better, words that make some sort of sense to them. Because IP addresses are actually four series of numbers separated by periods, the two have a hard time getting together. That is where the domain name service (DNS) comes in. Basically, the DNS relates text that people use to the numbers that computers use. When someone enters a URL, the DNS server he uses translates the text he types into the literal IP address so he can move to that address.
For this to happen for your site, you must enter the domain you want to use and the IP address it is associated with. This process is called registering the domain name.
There are various ways to register your domain name (the easiest is to use a hosting service that also offers domain name registration). You can search the Web to find sites at which you can register a domain name. The fees you will pay vary depending on the length of time for which you register the name and the other services that are included.
Although many companies will register your name free, you usually have to pay the fee associated with the registration. (A fee is charged for each domain name so someone has to pay that fee.) Some companies charge you to register the name and require you to also pay the name fee. And some companies subsidize the fees if you use other services. Does this sound confusing to you? If so, join the club. There are as many fee structures as there are companies doing this, so you just have to make sure you know what you are signing up for.
When you search for domain name registration sites, you will find many of them. And most offer similar deals. For example, I could register a name for about $10/year. Some even include additional services in this agreement, such as email forwarding.
Email forwarding enables email to your domain name to come to your current email address. For example, if I registered the domain mymacrocks.com, I could have email addressed to email@example.com delivered to any address I prefer.
Be sure you carefully read all domain name registration agreements before you finalize them. You need to be absolutely clear on what you are getting into.
Put some thought into the domain you register because that is how people will find your site. Avoid really long names, no matter how clever you think they are. Simple names are better.
Configuring Your Mac to Serve a Web Site
Each user account on your Mac can serve its own Web site; this means you can serve many Web sites from your Mac at the same time. To provide a Web site for a user's account, you place the files for that site in the Sites folder that is contained in that user's Home directory.
Each user's Sites folder contains an index.html file that provides a default home page for that user's Web site. You replace this file with your own index.html file to serve your specific site (you can also modify the default index page if you want). The default index.html page contains information about how Web sharing works and is worth reading if you are new to the topic.
There is also an overall Web site for the Mac itself. You should also ensure that you add a Web site for the Mac so that when people move to the home page for your machine, they will see a meaningful site. (You will learn about that shortly.)
After you have created your site, registered a domain name, and configured your Mac for the connection you are using, the steps to get it online are pretty simple:
If you see the site, everything is in great shape and your pages are "out there."
The concept that each user who has an account on your Mac can have a unique Web site opens many possibilities for you. You can create user accounts for the sole purpose of providing specific Web sites. For example, if I wanted to host a Web site for this book, I could create a user account called SEUsing MacOSX with the short name usingosx. I could then place the Web site in the Sites folder for this account and the site would be published.
If you are going to be hosting any Web pages on your Mac, you should also configure the root-level Web site for your Mac. This site will appear if someone accesses the Web site on your Mac without including a specific user's account in the URL. For example, if someone enters only http://12.34.567.89/, he would see the root-level Web page.
Consider creating a Web page that provides URL links to all the Web sites that are hosted on your Mac and posting that at the root level. This would make it easy for a visitor to get to any of the sites on your machine.
If you open the directory located at Mac OS X/Library/WebServer/Documents/, where Mac OS X is the name of your Mac OS X startup volume, you will see many index pages. Each of these has an abbreviation for the language for which that page is applicable at the end of its filename (for example, the file index.html.en is the English version of the page). The version that Apache serves depends on how your machine is configured.
To post a root-level Web page, do the following:
By default, a test page is placed at this location so that when you move to your root level, you can tell that the Web server is working (see Figure 14.14). This page also contains links to information about Apache.
Figure 14.14. This page lets you know that Apache is up and working on your Mac.
The Web server continues to run as long as your Mac is turned on and the Web site for each user account is served continuously, even if no one is logged in to the system. You can stop the server by shutting down the Mac or by turning off Web sharing using the Sharing pane of the System Preferences utility.
Going Further with Apache
The information in this section has barely scratched the surface of Apache specifically and Web serving in general. However, a great deal of information on both topics is available within Mac OS X as well as on the Net.
To access the Apache documentation included with Mac OS X, open the manual alias that is within the Documents folder (contained in the WebServer folder). The Apache folder will open. Open the index.html file and the Apache User Guide will open.
To find information on the Web about Apache, visit www.apache.org. There is plenty of information on this site, including some nice tutorials.
Before you leave this section, I must give you two more warnings.
First, make sure you protect your Mac from attack if you intend to publish and publicize Web pages you host from it.
Second, some ISPs have limitations on what you can do from particular types of accounts. For example, the license agreement you signed with your ISP might preclude you from hosting Web sites using that account. Make sure you understand any such limitations on your ISP account before you use it to host a Web site.
For information about protecting a Mac from Net attacks, p. 908.
You can also serve an FTP site from your Mac. On the Sharing pane, check the FTP Access check box. When you use ftp instead of http in the URLs for your machine, you move to the FTP site instead of the Web site.