Use Mac as a Web Server (5/5)
Posted by Wesley on
So far, we've got a whole website running phpBB and s9y, and... great... you can connect, login, make a post, etc. But you and you only right now. The website needs to get out of the basement and see the light.
6. Do We Have an Address?
The trusty "System Preferences - Sharing - Services - Personal Web Sharing and see bottom" gig gets you an IP address that your website is accessible from. If you're connecting to the internet directly via dedicated line or a modem (DSL, cable, etc.), you'll see an actual IP address that your website can be accessed from elsewhere on the internet. If that's the case, great. Move to section 7. However, if your Mac is hooked up to a router that relays the internet connection, you'll see an internal (private) IP address such as 192.168.1.32 showing up. The most common one starts with 192.168 but other examples include 172.16 and 10.0. Read this Wikipedia entry for details.
6. Do We Have an Address?
The trusty "System Preferences - Sharing - Services - Personal Web Sharing and see bottom" gig gets you an IP address that your website is accessible from. If you're connecting to the internet directly via dedicated line or a modem (DSL, cable, etc.), you'll see an actual IP address that your website can be accessed from elsewhere on the internet. If that's the case, great. Move to section 7. However, if your Mac is hooked up to a router that relays the internet connection, you'll see an internal (private) IP address such as 192.168.1.32 showing up. The most common one starts with 192.168 but other examples include 172.16 and 10.0. Read this Wikipedia entry for details.
Internal IP address cannot be used as your website address. You need to 'link' the actual routable IP address that the router is holding to the website on your Mac so that it can be connected from elsewhere on the internet. This is called 'port forwarding'. Websites normally use port 80 to communicate, so you need to configure your router so that it forwards port 80 to the internal address of your Mac. Basically, it'll work like this (addresses are just examples and not real) :
[ Internet ] - [ Router: 234.156.80.79 ] - Port 80 - [ Mac: 192.168.1.32 ]
So if someone from the internet tries to access a website (web browser opens port 80) on IP address of 234.156.80.79, the router forwards the port 80 communication right to the internal IP address of 192.168.1.32, which your Mac is assigned to. The web server running on your Mac will convey appropriate data in and out of port 80, and which goes through the router and to internet. That someone will be able to see your website appearing from IP address of 234.156.80.79 thanks to this port forwarding.
6.1. Configure Port Forwarding
In order for you to make this work, you must have full access to your router and a manual explaining how to configure various settings for them. Many routers these days will enable you to access the setup page via your web browser by entering a special internal address such as 192.168.2.1 and entering ID and password. See your manual for actual method and enter this page. After this, you need to do two things: assign a static internal address to your Mac, and set up port forwarding.
Look for a setting that can link a specific MAC address to an internal IP address. MAC here doesn't mean your Mac, but stands for Media Access Control; MAC address is a unique 12-digit (hexadecimal) address given to each LAN port (NIC). Basically, if you tie a MAC address to an internal IP address, that LAN port will always get a specific internal IP address instead of a random one that is usually given out by the default automatic configuration. This is needed since you can forward a port to only one address.
In this setting page, it's likely that you will have a choice of selecting a DHCP ('automatic') connection with MAC address (e.g. 01-AB-23-CD-45-EF), current IP address (e.g. 192.168.1.32), and possibly hostname showing up. Find the one that matches the IP address that the System Preferences is reporting: that's your Mac. Copy the MAC address and the IP address to the appropriate configuration boxes and add the information so that the two are linked. If this is successful, your Mac will always have that IP address assigned no matter what.
If this option is not available in your router, go to System Preferences - Network - Built-in Ethernet - TCP/IP. In Configure IPv4 setting, choose Use DHCP with manual address option and enter a valid internal address. Generally, you should put the same address that was already assigned automatically before. Press Apply Now. You might need to restart the router or your Mac if it doesn't work immediately. If the setup is successful, the configured address should show up in System Preferences - Sharing - Services - Personal Web Sharing, and you should be able to connect to internet without problems.
Once you have a static internal IP address for your Mac, set up port forwarding. Look around your router's setup page for Port Forwarding. It may be labeled as 'Virtual Server' in some cases among other things, so be careful and look for a setting that you can put an IP address and a port number in them. Once you found the right page, enter your Mac's IP address in the address input box and number 80 in the port input box. Enable the setting and you have port forwarding working.
7. Test the Waters
At this point, your website should be accessible via a regular IP address. Those who did the port forwarding can check what the 'regular IP address' is by looking at the status page in your router setup page and see what IP address the router is assigned to. Get a friend to attempt connecting to your website with that address (what, you don't have a friend on the internet? shame on you). Simply entering the IP address on the web browser will work, so just tell your friend the number. He/she should be telling you that the site will connect without problems. If not, you or your connection got messed up somewhere. Check back.
Alternately, if your friends happen to be offline (right? right??), you could try connecting through a public proxy server. Google for one, and enter the proxy server's address and port number in System Preferences - Network - Bult-in Ethernet - Proxies - Web Proxy (HTTP). Press Apply Now and first check that regular websites you visit connect well without any problems (if not, get another address.. there are lots of dead proxies). Then, try your website's IP address. It should work, and if that's the case you're on 3rd base. Congrats.
8. Get a Domain Name
As you know, most websites don't go by their IP addresses for a reason. You can't frickin' remember the numbers. So we use domain names in place of them so they'll be easier to remember. There's another reason, actually, and it usually applies to someone running a server using a home internet connection. Unless you've signed up for a specific service plan that gives you a static IP address for your connection, it's far likely that your IP address will change every once in a while - the ISP rotates the IP addresses around so they can work with limited set of IP addresses on their network. This is called dynamic IP address. Because of dynamic IP address, the IP address of your website will probably change once every few hours to every few days. Not only is your website address hard to remember, they change around! That's pretty lame.
To make this not lame, there is a service called Dynamic DNS or DDNS for short. This service ties a domain name to your server regardless of IP address change, so your website can always be accessible under a certain domain name. It solves two problems at once. What's more, there are lots of free DDNS services around.
In my case, I signed up at DynDNS. Go there, create an account, and login. Once you've done that, go click My Services right under your login info at the top right corner. On the left sidebar, find Add Host Services under My Hosts and click Add Dynamic DNS Host. Choose a nice hostname. Proper IP address should be automatically detected even if you're using a router. You can check 'Enable Wildcard'. After you're done, press 'Add Host'. Now you have a domain name that resolves to your web server's IP address automatically. Try the new address they way you tested the IP address earlier. If it doesn't work immediately, wait for about 10 minutes to an hour for the DNS information to propagate and check again.
We're not quite done after confirming that the domain name works. There must be a way for the IP address change to be automatically reflected on the DDNS service. This is where the DDNS update client comes in. This client periodically checks your IP address and reports to the DDNS server immediately if the IP address has changed. If you're using a router, there may be a DDNS update function integrated, but you shouldn't use it unless the DDNS provider has certified it. Lots of the hardware implementation of the update client in the routers are faulty, and keep sending update info even if there weren't any changes. This may cause the provider to blacklist your domain name and lock the IP address from changing. It happened to me, and while it's not hard to get off the blacklist, it wouldn't be ideal to have the domain name getting on the list frequently, so we should use a software client.
Good thing is that there is an official Mac client for DynDNS. Go to Update Clients section of the Support at the website and download the OS X client. Decompress to get the client program out and put it in Applications folder. Run the client and follow the instructions. Press Add User and enter your account information. You can either do the Auto Setup (this will fetch the relevant hostname information from DynDNS server) or do it manually and add the hostname later (this is the way to go if the Auto Setup hangs the program like I did).
Once your account and hostnames are set up and both get 'Ok' on the Status, click on your hostname and click Edit Host button. If you're using direct connection, 'Default Interface' or 'Built-in Ethernet' should be okay, but if you're behind a router, leaving it under this setting will have the IP address pointing to your internal address, which is not good. Change it to 'External (bypassing Proxy)' and press Save.
We're all done now! You can close the program. The client's daemon still runs in the background and should periodically update your DDNS info without user intervention. You only need to open the program if you need to change the settings or confirm the status.
This concludes the guide to setting up your Mac as a web server. I hope you get nice websites running! Oh, and read the appendix for some extra configuration stuff.