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
Why
Here's why you might do this:- You have separate test and development areas on your Web server.
- You have more than one Web site on your Web server.
- You simultaneously run different versions of Lasso on your Web server (for example Lasso 8.5.6 and 9).
- Note:
- Back up files BEFORE making changes!
- I use BBEdit File->Open Hidden to open all files.
- This is what I did on my Mac OS X 10.5 (client) powerbook. I believe it will also work for Mac OS X 10.6 (client).
- Tested on Firfox 3.6 and Safari 4.0.4.
How
- Open /private/etc/hosts:
- Add a line for each virtual host to the bottom:
(ex. url = http://xxx.test.xxx/) - Open /private/etc/apache2/extra/httpd-vhosts.conf and add all virtual hosts:
- Open /private/etc/apache2/httpd.conf and UNCOMMENT the line for virtual hosts:
- To check for configuration errors and if none are found, restart the Web server, type (in Terminal):
- Restart your computer.
- 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.
# # 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
127.0.0.1 xxx.test.xxx 127.0.0.1 xxx.dev.xxx 127.0.0.1 xxx.lmc.xxx
#
# 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>
# Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf
sudo apachectl configtest sudo apachectl graceful
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).
Creative Computing