Virtual Hosting on Mac OS X
Enter virtualhost.sh -- the most powerful and easy shell script for Mac OS X that sets up a virtualhost into your system. Using NetInfoManager, virtualhost.sh lets you easily create a named virtual host that is only accessible locally.
Here's how to use it:
- Because of what this script does, you must have root privileges. The best way to achieve this is to use sudo.
sudo /path/to/virtualhost.sh <virtualhost>
Example:
sudo /path/to/virtualhost.sh mysiteNote: when you type in the name of your virtualhost, do not include the angled brackets. It is a Unix/command-line convention that denotes a requirement parameter. (Square brackets are used to denote an optional parameter.)Where "/path/to" is the path to where you installed virtualhost.sh ("./virtualhost" will work if you're in the same folder), and <virtualhost> is the name of the virtualhost you want to add. You should pick a one-word name so that it won't interfere with your everyday surfing. For example, if your live site is mysite.com, then you should pick mysite.
- If it's your first time running the script, it's going to ask you if you want to update the DocumentRoot of your webserver to be your Sites folder. It's up to you, but I recommend that you do it.
- Next, it will ask you for the folder name of the site you are setting up. The default is fine, but it gives you the option of changing it.
- Lastly, you will -- oh wait, that's it. At this point, you're done! If you made a mistake at any point, just re-run the script, and you can overwrite an existing configuration.
Update: Do to popular demand (including my own), I have added removal functionality. To remove a virtualhost you've added, run the script like so:
You can install this script wherever you like. I put mine in /usr/local/bin, strictly because of habit from previous Unix systems. I've tried to test this as much as possible, but if you experiencing any problems, drop me a line.
Known limitations:
Several people have asked me if you can use this script to setup a live site. This is currently not possible. The majority of users using this script on their local machine will have a dynamically-assigned IP address. Because Apache's name-based virtualhosting requires you to enter the IP address you're using, a dynamically-assigned address would mean that you would have to update your Apache configuration every time your address changes. Additionally, this script uses NetInfoManager (/Applications/Utilities/NetInfoManager) to make the hostname resolveable. A real IP address with a real hostname does not need an entry in NetInfoManager, and the presense of one could be problematic if the IP address ever changed (I believe NetInfoManager is consulted before a DNS request). At any rate, if you wish to change the default IP address used by the script, you can do a search and replace like so:
That all said, if you do happen to have a hostname and a static or dynamic address that never changes, and you wish to enable a name-based virtualhost, you can do what I do:
- Locate the virtualhost file:
cd /etc/httpd/virtualhosts
- Copy the generated file to a new file:
cp mysite mysite.ext
- Edit the file, and replace the IP address in the <Virtualhost 127.0.0.1> directive with the address you wish to use, and the ServerName mysite with ServerName my.real.hostname.com (where my.real.hostname.com is your real hostname, and not literally what I wrote).
- Edit /etc/httpd/httpd.conf, and add a NameVirtualHost statement right below the one the script originally added (at the bottom of the file):
NameVirtualhost 127.0.0.1... where aaa.bbb.ccc.ddd is the IP address you're using.
NameVirtualHost aaa.bbb.ccc.ddd - Give Apache an ol' restart (apachectl restart) and then type your URL into your browser to make sure it worked.
Several people have asked about donations of some kind. Now of course, this is completely unnecessary, but if you'd still like to show your appreciation somehow, I wouldn't say no to a PayPal donation or Amazon gift. ;)
|
|
