Having a local web server to test your web sites and applications is essential to your development environment. It is very easy to set-up numerous virtual hosts for each of your projects, which allows you to develop your interactive masterpieces in a close to real world implementation. There are actually many aspects of Flash development that will exhibit different behavior or not even work at all if not served from a valid domain and by golly localhost counts.
The worst thing that can happen to a developer is deploying your site or application to the clients server and having it not work and it's usually because it was tested only in Flash or by dragging your HTML file into a browser. All developers on my team are required to have an active local web server that they manage, which also makes it easy for me to track their development efforts in basically real-time. If you have a Mac, then you're cool, but also already have everything installed that you're going to need. Let's get started.
The easiest way to do this with minimal fuss and muss is to do it though your Systems Preferences -> Sharing Preference panel. Click on the Services tab and select Personal Web Sharing from the list. Click start and there you go, Apache 1.3.33 is up and running. Your web site's URL is displayed at the bottom, which is usually your computer's IP address. You can also type in http://localhost to get there. The actual directory where you place files to be viewed at this URL is /Library/WebServer/Documents/. Anything you place in this directory can be viewed in any web browser, even IE6, just as if it were on a real grown up web server. Only other computers on your LAN will be able to access this web site, by IP only - not localhost, but that's cool. You wouldn't want to use your dev or personal computer to serve live sites anyway.
Ok, so you've got a single site up and running. Now if you're like me you have a ton of projects that should be set-up this way. Time to get our hands dirty and dig into the Apache config file, enable the root user and a brief tutorial on managing your Apache install from the Terminal.
First off you have to enable your root user and give it a password if you haven't done so already. Only the root user has permission to edit the Apache config file, so let's start there. In your /Applications/Utilities folder you will find a program called NetInfo Manager. Go ahead and launch it. In the top menu bar click on Security -> Authenticate and type in your computer's admin password. Now the Enable Root User option will be available so go ahead and do that, you'll have to authenticate again, type in the new root password, click ok, then Security -> Deauthenticate for good measure and you're done.
Now we can make the necessary changes to the Apache config file and start/stop/restart the web server from the Terminal. Be extremely careful when using the Terminal as the root user, you can cause some very serious damage to your computer if not used with knowledge and caution. Open the terminal and type the following (do not type the $ or # characters, they represent the command line prompt):
$ su -
Password: <type in your new root password>
# vi /etc/httpd/httpd.conf
Now your in the text editor vi which I will not discuss here, but believe me you will love to hate it. You can also use pico instead of vi which is a bit easier to use, but your sys admin will laugh at you if caught doing so. For now use the arrow keys to move up and down through the config file. Since we're already here we might as well turn on PHP. Arrow down until you see the following line:
#LoadModule php4_module libexec/httpd/libphp4.so
In the config file the # symbol represents a commented out line of text, same as using // in ActionScript or JavaScript. Right now you are in the browse mode of vi and are unable to edit text. Hit the i key to go into insert mode, move the cursor in front of the # symbol and hit the delete key to remove it. Hit the esc key to get back out of edit mode and now you have uncommented that line of text. Keep arrowing down until you find this:
#AddModule mod_php4.c
Do the same thing you just did to remove the # comment symbol. If at this point you feel you are struggling with vi,
Google it and there are a million vi tutorials online. Now that you have edited the config file to enable PHP lets go ahead and save, then test to make sure it's going ok so far. To save in vi type :wq, this saves the file and quits vi. To just save and not quit type :w and to quit and not save type :q!
To make this take effect you need to restart Apache, but first lets check to make sure we haven't saved a bad config. At the command prompt type the following:
# apachectl configtest
If everything goes ok, you'll see the following output: Syntax OK. Now use the up arrow key to see your previous commands until you see:
# vi /etc/httpd/httpd.conf
then hit enter.
Now were back in the config file again. Scroll all the way to the bottom of the file where the VirtualHosts directive is. The best way to have multiple virtual hosts on your local computer, since you're probably not part of a domain or if so have no access to the Domain Controller, is to set them up on ports. A virtual host URL running on a port looks like this: http://localhost:9000. You may have see this before because there all kinds of services running on other ports on your computer. If you have Tomcat set-up, it runs on port 8080 as an example. It is generally safe to pick port numbers in the 9000 and 10000 range. The secret to this kind of set-up is to be sure to configure a port based virtual host for the default server which runs on port 80. Here is an example of a VirtualHost configuration that includes the default host and a custom one. All of your custom VirtualHost can have root directories anywhere on your computer.
Listen *:80
<virtualhost *:80>
DocumentRoot /Library/WebServer/Documents
</virtualhost>
Listen *:9000
<virtualhost *:9000>
DocumentRoot /Users/chris/projects/sample/build/bin
</virtualhost>
Now write, save, test the config and restart - done. You now have the power to create tons of individual web sites on your own computer. Check out the
Mac DevCenter for more information on this subject.
[update: 10/1/2007]
For those of us still on Tiger (10.4.x) and want to upgrade to PHP5, check out
Entropy's site for a super easy installer.
[update: 12/10/2007]
For those that are cool enough to be using Leopard here are some updates to the above information.
Enabling root user:
enable root user using utilities/Directory Utility
unlock to enable changes
go to edit-->enable root password
done.
httpd.conf configuration:
check out this blog post on
Bust Out Solutions