How do I create virtual hosts on my Apache2 web server, Mac OS X 10.5 (client) computer?

Instructions for setting up name-based virtual hosting to identify multiple non-SSL virtual hosts (all on port 80, for example).

2-9-2010 by Tami Williams

Why

Here's why you might do this:

How

  1. Open /private/etc/hosts:
  2. #
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1	localhost
    255.255.255.255	broadcasthost
    ::1             localhost 
    fe80::1%lo0	localhost
    
  3. Add a line for each virtual host to the bottom:
    (ex. url = http://xxx.test.xxx/)
  4. 127.0.0.1	xxx.test.xxx
    127.0.0.1	xxx.dev.xxx
    127.0.0.1	xxx.lmc.xxx
    
  5. Open /private/etc/apache2/extra/httpd-vhosts.conf and add all virtual hosts:
  6. #
    # Use name-based virtual hosting.
    #
    NameVirtualHost *:80
    
    # for SSL
    NameVirtualHost 127.0.0.1:443
    
    
    # Lasso 8 runs as the default
    <VirtualHost *:80>
      #Just to keep things sane...
        ServerName 127.0.0.1
        DocumentRoot "/Library/WebServer/Documents"
        
        # HAVE LASSO HANDLE .lmc SUFFIX
        AddHandler lasso8-handler lmc
        
        SSLEngine off
    </VirtualHost>
    
    
    # test
    <VirtualHost *:80>
    	    ServerName xxx.test.xxx
    	    DocumentRoot "/Library/WebServer/Documents/test"
    	    ErrorLog /private/var/log/apache2/test_errorlog
    	    CustomLog /private/var/log/apache2/test_accesslog combined env=!nologimg
    	    
    	    SSLEngine off
    </VirtualHost>
    	
    # dev
    <VirtualHost *:80>
    	    ServerName xxx.dev.xxx
    	    DocumentRoot "/Library/WebServer/Documents/dev"
    	    ErrorLog /private/var/log/apache2/dev_errorlog
    	    CustomLog /private/var/log/apache2/dev_accesslog combined
    	    
    	    SSLEngine off
    </VirtualHost>
    
    # LassoMasterClass (lmc)
    <VirtualHost *:80>
    	    ServerName xxx.lmc.xxx
    	    DocumentRoot "/Library/WebServer/Documents/lmc"
    	    ErrorLog /private/var/log/apache2/lmc_errorlog
    	    CustomLog /private/var/log/apache2/lmc_accesslog combined
    	    
    	    SSLEngine off
    </VirtualHost>
    
  7. Open /private/etc/apache2/httpd.conf and UNCOMMENT the line for virtual hosts:
  8. # Virtual hosts
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    
  9. To check for configuration errors and if none are found, restart the Web server, type (in Terminal):
  10. sudo apachectl configtest
    sudo apachectl graceful
    
  11. Restart your computer.
  12. Check that your Web server (and Lasso, if you're using Lasso) are working correctly. Open a web browser and go to http://127.0.0.1/ and one of your virtual hosts, ex. http://xxx.test.xxx, and, if you're using Lasso, one of your Lasso web sites.

What to watch out for

Don't skip step #5! If any errors are reported when you do sudo apachectl configtest your Web server probably won't run.

You must restart your computer in step #6 before it will see the change made in step #2 (/private/etc/hosts).

Next Step

How do I add a virtual host with self-signed SSL to my Apache2 web server, Mac OS X 10.5 (client) computer?