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

Set up new virtual hosts by creating new folders UNIX
As a web developer, I wanted a simple way to easily create and remove virtual hosts for Apple's built-in Apache server. The solution I've discovered allows me to create a virtual host just by creating a folder with the virtual hostname I want to use and editing my /etc/hosts file so that my browser will be able to find it. Removing the virtual host then is as easy as deleting the folder and the entry in /etc/hosts -- there's no need to edit Apache configuration files or restart Apache each time.

To set this solution up in Mac OS X 10.5.x, you'll need to edit two Apache configuration files, but you'll only have to do it this once. (Note that it's a good idea to make backups of your configuration files, just in case something doesn't work. I make no guarantees, nor am I responsible if you mess your system up!)

Here's what you need to do to get this working.
  1. In Terminal, edit (with root privileges) /etc/apache2/httpd.conf. In the section, add the following two lines:
    LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    CustomLog /private/var/log/apache2/access_log vcommon
    You will also need to comment out the following line, by putting a # at the beginning, as seen here:
    #CustomLog /private/var/log/apache2/access_log common
    When finished, that section will look something like this: (Note: For brevity's sake, I took out the explanatory comments):
    <IfModule log_config_module>
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
        LogFormat "%h %l %u %t \"%r\" %>s %b" common
        LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
    
        <IfModule logio_module>
          # You need to enable mod_logio.c to use %I and %O
          LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
        </IfModule>
    
        #CustomLog /private/var/log/apache2/access_log common
        
        #
        # Logging virtual-host data with the common format
        #
        CustomLog /private/var/log/apache2/access_log vcommon
    
        #CustomLog /private/var/log/apache2/access_log combined
    </IfModule>
    Finally, uncomment the Include line in the Virtual hosts section so that it looks like this:
    # Virtual hosts
    Include /private/etc/apache2/extra/httpd-vhosts.conf
  2. Edit (again with root privileges) /etc/apache2/extra/httpd-vhosts.conf to look like this:
    #
    # Virtual Hosts
    #
    # If you want to maintain multiple domains/hostnames on your
    # machine you can setup VirtualHost containers for them. Most configurations
    # use only name-based virtual hosts so the server doesn't need to worry about
    # IP addresses. This is indicated by the asterisks in the directives below.
    #
    # Please see the documentation at 
    # <URL:http://httpd.apache.org/docs/2.2/vhosts/>
    # for further details before you try to setup virtual hosts.
    #
    # You may use the command line option '-S' to verify your virtual host
    # configuration.
    
    #
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80
    
    #
    # Mass Virtual Host configuration as taken from the Apache web documentation
    #
    <VirtualHost *:80>
        # get the server name from the Host: header
        UseCanonicalName Off
    
        # include the server name in the filenames used to satisfy requests
        VirtualDocumentRoot "/Library/WebServer/Documents/%0"
        VirtualScriptAlias "/Library/WebServer/Documents/%0/cgi-bin"
    </VirtualHost>
  3. If Web Sharing is currently on, restart it using the GUI or apachectl.
The setup is now complete, and the only thing left to do is create folders with the name syou want to use, and edit your /etc/hosts file. Here's an example setup:
  1. Create a folder named mysite.dev in /Library/WebServer/Documents
  2. Edit /etc/hosts, adding the following line:
    127.0.0.1       mysite.dev
    The /etc/hosts file needs root privileges to modify it. I recommend using BBEdit or TextWrangler, as they will prompt you for your password to save the changes.
And that's it. Now you can point your web browser to http://mysite.dev, and it will display your local web site. You'll want to be careful as to what you name the local site, so that it won't interfere with an actual web site. For example, you'll never get out to the actual google.com if you use that name in /etc/hosts.

Bonus Round: Set up Parallels to access your local site.
  1. If you look in the Network System Preference on your Mac, you should see one or more Parallels[networking type] Adapter interfaces on the left. Examine each, and note down the IP addresses. (The virtual machines see one of these addresses as your Mac's IP address.)
  2. Run your virtual machine and find out its IP address. The first three numbers should match one of the ones for your Mac. Edit the virtual machines hosts file as above, but using the Mac's corresponding IP address instead of 127.0.0.1. (In my copy of Windows XP, the hosts file is stored at C:\system32\drivers\etc\hosts)
You can now go to the virtual host in your web browser, and test your web site in Internet Explorer.
    •    
  • Currently 2.56 / 5
  You rated: 1 / 5 (9 votes cast)
 
[16,537 views]  

Set up new virtual hosts by creating new folders | 13 comments | Create New Account
Click here to return to the 'Set up new virtual hosts by creating new folders' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Set up new virtual hosts by creating new folders
Authored by: gregraven on Dec 16, '08 08:15:33AM

One problem with doing it this way is that your web files must reside in /Library/WebServer/Documents. This makes it tougher to get to and from these documents, compared to setting up a separate account for each web client, and having all client files in that account.

I prefer to use VirtualHostX from clickontyler.com. It's only $9, it handles all the config files for you, and allows you easily to point to any folder you want for your virtual host.

---
--
Greg Raven
Apple Valley, CA



[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: boxcarl on Dec 16, '08 11:17:50AM
You do have to be careful about /Library/Webserver/Documents since upgrading OS X, for example, will clobber whatever files you put in there. My solution is just to move the Documents to Documents-Apple Junk then add a symlink to my ~/Sites folder using ln -s. Actually, I have a couple sites that I administer, so what I do is symlink to ~/Sites/localhost, and "localhost" itself is a symlink to either ~/Sites/site1 or ~/Sites/site2, etc. depending on what I'm working on testing today. That way you don't need to sudo in order to change your localhosting directory.

[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: krang on Dec 16, '08 08:19:59AM

There is another alternative, by using Apache rewrite rules:

http://www.craigfrancis.co.uk/features/setup/apache/

The main part of the linked document is:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^[a-z0-9]+(.[a-z0-9]+)*.[a-z]+.domain.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([a-z0-9]+(.[a-z0-9]+)*).[a-z]+.domain.com(.*) /Library/WebServer/Projects/$1$3

Where the "domain.com" bit should be changed (or removed).

This system is setup to use URLs like:

http://project.name.server.domain.com

And with a locally running DNS server, you can wildcard the sub-domain "server.domain.com" to point to the localhost (or its IP, if you want multiple hosts to connect).

Details for the DNS setup can be found:

http://www.craigfrancis.co.uk/features/setup/bind/



[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: ghoppe on Dec 16, '08 10:01:53AM
Set up new virtual hosts by creating new folders
Authored by: megabulk on Dec 16, '08 03:17:59PM

big ++ to this



[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: solitario on Dec 16, '08 10:13:02AM

Actually any configuration file you make that has the .conf extension an is placed in the /private/etc/apache2/other/ directory will be included by apache, because of this statement in httpd.conf

Include /private/etc/apache2/other/*.conf

So, you don't need to edit any of the stock files, just add your own configuration file with all the options to set up the vhosts.



[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: fahirsch on Dec 16, '08 11:24:37AM
I use Headdress (http://headdress.twinsparc.com/) (which at least today has disappeared from the web). I keep all my sites in the Sites folder, making it easy to do anything. It's shareware, but up to two sites, free.

[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: fahirsch on Dec 17, '08 12:25:56AM

And today is no longer on VersionTracker



[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: megabulk on Dec 16, '08 03:19:39PM

also, downloading Bonjour for Windows makes a pretty sweet way to access local virtual hosts from a Parallels/VMWare install.



[ Reply to This | # ]
Why no mention of MAMP?
Authored by: jiclark on Dec 18, '08 11:21:46AM
Doesn't MAMP do the same thing, pretty painlessly?

http://www.mamp.info/en/download.html

Works for me, and the config necessary is to change the preferences to look for the its "htdocs" in your Sites directory, instead of in the MAMP folder in Applications...

[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: regulus on Dec 19, '08 01:00:30AM

Maybe I don't understand the power of virtual hosts for testing websites on your own computer. I've used virtual hosts for allowing several websites on a computer to be accessed over the internet, but for just simple internal testing it seems like overkill.

All you need to do is drag the index.html file onto safari and the website runs. It doesn't matter what folder the file is in. All links work too. You can then bookmark it if you want repeated access. This allows you to keep your websites in any folder, there's no mucking around with config/hosts files of any kind. Plus you don't even need to run the web server at all. What could be simpler?

If I'm missing something please explain it to me.



[ Reply to This | # ]
Set up new virtual hosts by creating new folders
Authored by: davermont on Dec 20, '08 05:03:25AM

If you're just serving straight HTML pages and you use relative URLs in your code (../images/logo.gif), your solution works fine.

It will not work if your code uses root-based URLs (/images/logo.gif), or if your site uses php or cgi scripts.



[ Reply to This | # ]
This post is compatible with IP:PORT Vhosts
Authored by: faunzy on Dec 22, '08 05:17:33PM
This works in conjunction with IP address and port number style virtual hosts:

<VirtualHost *:80>
ServerName virtual-host1.local.dev
DocumentRoot /Library/WebServer/Documents/virtual-host1/
<Directory /Library/WebServer/Documents/virtual-host1/>
...
</Directory>
</VirtualHost>

It should also be noted that each host address needs an additional entry into the hosts file, but they can all be on one line, like so :
127.0.0.1 virtual-host1.local.dev virtual-host2.local.dev virtual-host3.local.dev


[ Reply to This | # ]