If you find yourself developing more than one web site at home, or anywhere with a *NIX machine with Apache, and want to be able to access all the sites for testing, it is time to set up Virtual Hosts. By using Virtual Hosts, you can access all your web sites easily (especially if you use absolute links) and separately by typing in:
http://other. How does this work? Like I mentioned before, the first step is to set up Virtual Hosts. The second step is to edit your /etc/hosts file so you can assign names to your localhost address (jappler for http://jappler/)
Setting up Virtual Hosts
- To set up virtual hosts, you will need to edit your Apache configuration (
sudo pico /etc/httpd/httpd.conf). You can make a number of changes in the Apache configuration to set up an environment that is right for you, but for now, all you are concerned about is located at the end of the standard config file.
- Uncomment the line:
NameVirtualHost *:80(delete the number sign in front of the line)
- Add in your Virtual Hosts information:
ServerAdmin [email protected]
CustomLog /Volumes/www/Sites/jappler.com/logs/access_log common
To break it down line by line:
- ServerAdmin: your email address, not really too important for local testing.
- DocumentRoot: where your site files are located.
- ServerName: This is the name you want to use when to test the web site in your browser (ex. I use jappler for http://jappler/)
*This will also match the name you will add to your /etc/hosts file)
- ErrorLogs: Location of your error logs. You can create separate log files for each hostname, or use one log file for all errors.
- CustomLog: Location of your access logs. You can create separate log files for each hostname, or use one log file for all access information.
Modifying the /etc/hosts file
- You will need to edit the /etc/hosts file (
sudo pico /etc/hosts).
- Your /etc/hosts file will look like:
# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
You will need to add in the site names you want, after the 127.0.0.1 localhost line. You can add in as many as you want, but know these will only work on your local machine. More on the /etc/hosts file. Using pico or any other editor, add the hostnames you want to use locally to the /etc/files so it will now look like this (see changes in bold):
*Make sure your hostnames match the names you used in your Apache Virtual Host section
Once you have your Virtual Host information in place, and you have editted your /etc/hosts file, it is time to start up Apache. To start, stop, or restart Apache, you can use
apachectl in the command line. On Mac OS X, this is located:
/usr/sbin/apachectl and in most UNIX/Linux distributions, it is located:
/usr/local/apache/bin/apachectl. To use it, you can type in (on Mac OS X)
sudo /usr/sbin/apachectl stop (to stop Apache). To start Apache type in:
sudo /usr/sbin/apachectl start. If you have any syntax errors, Apache will display them after you type in that command, if not and everything is ok, you will get the message:
/usr/sbin/apachectl start: httpd started.
Now you are all set to serve your web projects on your local machine with unique names for testing purposes! Questions? Just ask.
[tags]Mac OS X, web development, apache, vhosts[/tags]