Jump to content

Mac OS X Tiger/Using your Mac as a Web Server

From Wikibooks, open books for an open world

[clarification needed]

Starting the Apache web server

[edit | edit source]

Using Mac OS X as a web server is easier than you might think. The reason for this is that Mac OS X is shipped with the powerful and widely used Apache web server software built right in.

To begin, open Applications > System Preferences (look for it in the Dock) and go to the Sharing pane. Under the Services tab, check Personal Web Sharing or select it and click Start. The Apache web server should now be running and will automatically start everytime your computer boots unless you change the settings in the System Preferences.

Because Apache runs as a background service or daemon and not a normal application, it won't appear as an item in the Dock when it's running.

Setting up your web pages

[edit | edit source]

Open a web browser - like Safari - and type in localhost in the address bar. You should see a page that says something along the lines of "Seeing this instead of the website you expected?" This message is the default page that the Apache web server will show when it's first installed. It means that Apache is configured and working correctly and that you're ready to put your site up!

Naturally, you don't want people to see the default homepage when they go to your web site, you want them to see your own web page. To do this, open a Finder window and go to /Library/WebServer/Documents/. You should see a group of HTML and image files including one called index.html. These files are used in the default Apache page you just viewed.

You can delete these files (or put them somewhere else if you want) and you'll then be ready to put your own web pages in this folder. When you've put your own HTML and image files into the /Library/WebServer/Documents/ folder, go back to localhost in your browser and refresh the page. You should now see your web pages instead of the default Apache one. Give yourself a pat on the back, you have now established a web server! However, you are not quite done yet...

Giving others access to your web site

[edit | edit source]

Now you've got your web server set up, you need to give people an address to visit your web site.

This part of the tutorial assumes that your computer is connected directly to the internet and isn't behind a firewall or connected using a network hub or DSL router. If your computer is connected through a router, you may need to consult the manual that came with it to enable port forwarding a facility that allows your Apache web server to send data to the internet via the router. The built in Mac OS X firewall automatically allows Apache to send web pages to the internet but other firewall software may differ.

If your computer is connected directly to the internet, you could just give people the IP address of your computer which you can find out using WhatIsMyIP but there can be problems with doing this...

The problems start because most internet service providers (ISPs) give their home user's computers a new dynamic IP address each time they connect to the internet. The IP address is used to identify each individual computer on the internet. This means that as your computer's unique IP address changes, the corresponding address of your website will change and therefore any links and bookmarks to your site will stop working!

Fortunately there's a solution to this problem called DynDNS, a service that forwards visitors to a web address of your choice (URL) to your dynamic IP address. All you have to do is register with DynDNS, download the Mac OS X client program, set it up and it will automatically update your DynDNS account with your IP address whenever it changes.

If you are lucky enough to have a static IP (which doesn't ever change) then you won't need a DynDNS account, but you might want to buy a domain name so people don't have to type in a number to visit your page. DynDNS also offers you free domain names for static IPs.

Once you're set up with DynDNS, you can get to your website by typing in the domain name you chose to use with them (note sometimes, the DynDNS service takes a while to update when you first set up your account).

Troubleshooting

[edit | edit source]

Apache isn't running

[edit | edit source]

If you can't see pages on your web server from your own computer, check that Apache is running. In the Finder, open the menu Go > Utilities and open the Activity Monitor application. In the window that appears, look for an item in the list of running processes called httpd. This is the Apache web server and if its missing from the list, Apache isn't running.

To start Apache, you can either turn on Web sharing in the Sharing pane of the System Preferences or go into the Terminal and type sudo apachectl restart followed by your password when prompted for it.

Can't access your site from your own computer

[edit | edit source]

If Apache is running and you can't see your website from the Mac it's running on, check that the address bar says either http://127.0.0.1/ or http://localhost/. If it does, check that you have put some files into your Apache web sites folder.

Can't access your website from the internet

[edit | edit source]

If Apache is running but you can't see your website from a computer outside your network or by typing in a DynDNS address, there may be a network problem preventing computers outside your network from connecting to your Mac. Check your router to see if port forwarding is enabled and configured correctly.

If you still can't get to your website from outside your home network, If it is enabled, check that your ISP allows you to operate webservers on their network. Some ISPs block TCP/IP port 80 which is used for the HTTP protocol used by web servers and browsers. If this port is blocked, then you will have to use dyndns or no-ip.com to bypass port 80 and use another port such as 8080. This need to configure your apache too.

Web pages for multiple users

[edit | edit source]

If you have more than one user set up on your Mac OS X computer, you may be interested to know that each of these users can have their own web site as well. You might have noticed that every user on Mac OS X has a folder in their home directory called Sites.

HTML and other web page files you put into this folder can be viewed by opening the URL http://localhost/~username/ in a web browser where username is the shorthand version of your Mac OS X user account name (the name of your home folder in the Finder).

Security Considerations

[edit | edit source]

There are some important issues to consider if your Mac hosts a webserver exposed to the internet. This is especially true if you use scripts (like perl, PHP, JSP, etc.) It's worth taking the time to get informed. Also, if you don't know what you're doing, do not attempt to configure Apache manually.

[edit | edit source]

Here are some other useful resources: