Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Apache name-based virtual hosting made easy UNIX
If you're a web developer who works on numerous sites, it can be very beneficial to simulate a production environment on your local machine. Doing so typically requires a virtual host, that is a configuration for a particular website with a specific DocumentRoot.

Using Apache and Apple's NetInfoManager, it's possible to setup simple name-based virtual hosts that run only on your machine. For example, if you are a developer of macosxhints.com, you might have a development version on your local machine. Wouldn't it be handy to preview your development version by pointing your browser at http://macosxhints/ (note the lack of ".com")? This way, all your absolute image references (starting with /) and such would work just as they do in the production environment.

I have written a script that lets you add such a virtualhost to your local machine really easily. You simply type:

sudo /path/to/virtualhost.sh

... where /path/to/ is the path to where you installed virtualhost.sh. I like to place mine in /usr/local/bin, as it's convention, and I have added this to my path, but you're free to install it wherever you want. If you want to just test it out in your current directory, type:

sudo ./virtualhost.sh

Follow the instructions, and within a minute you'll have yourself a working virtualhost! For more information, see my website or to dive right in and download the script. If you have any questions, problems, or bug reports, drop me a line.

[robg adds: While I haven't tested this script yet, I did verify that there's no malicious code of any sort. If you try it, please post your experiences, as it seems like a real time saver!]

    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[11,705 views]  

Apache name-based virtual hosting made easy | 13 comments | Create New Account
Click here to return to the 'Apache name-based virtual hosting made easy' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Apache name-based virtual hosting made easy
Authored by: griptales on May 27, '03 11:21:54AM

I will admit that I am a novice when it comes to Apache configurations. I have tried several tutorials for setting up a name based virtual host on my testing machine - with no luck.

I bit the bullet and tried this script [after checking for anything that looked questionable], and I have to say that I love it.

This script has made it so easy to set up a virtual host, that I will use it for every site I work on.

I consider it a MUST HAVE.

Two Thumbs Up
5 Stars
a 10
You pick the system of measurement, and I will give it a top rating.



[ Reply to This | # ]
Apache name-based virtual hosting made easy
Authored by: Tastannin on May 27, '03 04:19:57PM

For those who may not catch it the first time around, this is only for your LOCAL machine. It is not something you can use for your public webserver. :)



[ Reply to This | # ]
specific "home page"...
Authored by: zeb on May 27, '03 04:46:47PM

Great script!

I have a web site that doesn't use the default "index.html". How can I configure just this site to point to a specific "home page"?



[ Reply to This | # ]
specific "home page"...
Authored by: patrickgibson on May 27, '03 05:44:28PM
You can delete the index.html that's generated by the script, and replace it with your own. Alternatively, if you want to have something like index.cgi or index.php as your index, you can create a .htaccess file in the website folder with the following line:

DirectoryIndex index.php index.cgi index.html

(The order that you specify will be the search order.)

You can also search for "DirectoryIndex" in /etc/httpd/httpd.conf

[ Reply to This | # ]

specific "home page"...
Authored by: zeb on May 28, '03 12:01:23AM

Thanks Patrick, I set the "DirectoryIndex" in the httpd.config file to search for possible home pages that I might use...



[ Reply to This | # ]
specific "home page"...
Authored by: subgeniux on May 29, '03 08:12:27AM

I searched through the httpd.conf file and found these lines. Do a search through the httpd.conf file for HTML and you'll find these lines, then change the index.html in the DirectoryIndex line to your homepage file, I think this might do it.

#
# DirectoryIndex: Name of the file or files to use as a pre-written HTML
# directory index. Separate multiple entries with spaces.
#
<IfModule mod_dir.c>
DirectoryIndex index.html
</IfModule>



[ Reply to This | # ]
specific "home page"...
Authored by: subgeniux on May 29, '03 08:15:45AM

Oops, Patrick has already mentioned what I just said.



[ Reply to This | # ]
Updated version
Authored by: patrickgibson on May 27, '03 05:47:36PM

Just a note that I have updated the script to allow people to customize where the virtualhost folders are installed. Edit the script, and you will find some documentation.



[ Reply to This | # ]
Updated version
Authored by: scottgo on May 27, '03 07:25:34PM

Patrick, very cool!! I was trying to figure out how to change the directory of my root within the script. I am glad you offered this addition.

This will be a very popular I'm sure

---
There are 10 kinds of people in the world, those who understand binary and those who do not.



[ Reply to This | # ]
Apache name-based virtual hosting made easy
Authored by: bluehz on May 28, '03 11:18:45PM

Didn't work for me. I had previously setup some virtual hosts and they may be conflicting or something. Although - if I do an httpd -S it will normally show me the hosts on this machine and the new one I created with the script is not showing up either.

hmmmmm....



[ Reply to This | # ]
Apache name-based virtual hosting made easy
Authored by: patrickgibson on May 30, '03 02:34:47AM
I should have mentioned that the script assumes a stock, Apple-provided httpd.conf. Unexpected changes to the configuration file can have unexpected results. If you were willing to revert to a stock config (with PHP enabled), I can send you mine. Contact me here.

[ Reply to This | # ]
Apache name-based virtual hosting made easy
Authored by: kwiersma on Jun 02, '03 01:08:11AM
Don't forget to add the new virtualhost name to your exclude list if you use a proxy server. I forgot about this and couldn't figure out why it wasn't working until I realized the proxy server was trying to handle the virtual host name instead of passing it on to OS X first.

--Kurt
A web developer's weblog

[ Reply to This | # ]
add Rendezvous to name-based virtual hosting
Authored by: nwfrg on Dec 05, '04 10:48:25PM
A recent hint about setting up a Rendezvous link to your virtualhost is a good complement to name-based virutal hosting. This makes all your virtualhost sites easy to find using the Rendezvous Collection in Safari bookmarks.

[ Reply to This | # ]