Whilst developing a new project it can be convenient to setup a virtual host on your local machine. For example, if I wanted to have a local development/demonstration version of a website I'm developing (e.g. http://learnhowtojuggle.info), I could setup the following virtual host: http://learnhowtojuggle.dev.
N.B. Only follow this tutorial if you are comfortable with editing system configurations and are aware of the risks. If you have TextMate installed, you can click the file path links in these instructions to quickly open them on your local computer (More Information).
1. Modify the virtual hosts configuration
Use terminal to find and modify your virtual hosts configuration file.
Append to this bottom of this file an entry for your virtual domain:
<VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "/Users/neil/Sites/learnhowtojuggle" ServerName learnhowtojuggle.dev ServerAlias learnhowtojuggle.dev ErrorLog "/private/var/log/apache2/learnhowtojuggle.dev-error_log" </VirtualHost>
Make sure the DocumentRoot you enter actually exists. For convenience, I've placed mine in my Site folder. You can now save and close this file.
2. Check Apache knows about virtual hosts
Next we will need to modify the Apache configuration file to make sure it includes the virtual hosts file.
Locate the following line and remove the # sign (also known as an "octothorpe" ) from in front of it.
Save and close this file as well.
3. Restart Apache
For any changes to take effect on the Apache server, you will need to restart it.
sudo httpd -k restart
4. Update hosts file
Finally, the easiest way to let our browser know to use our local server when it get a request for our virtual host is to modify the hosts file.
Add the following entry:
After you save and close this file, navigate to http://learnhowtojuggle.dev in your browser to see the newly created virtual host on your local machine.
Since I am serving my new virtual host out of my Sites folder, and I want to use my .htacess file for URL rewriting, I had to update the Apache configuration again to allow permission for this.
The configuration for my Site folder was located at /private/etc/apache2/users/neil.conf (yours will be different based on your system profile name). I then set my permissions to be pretty loose since it is only my local machine.
<Directory "/Users/neil/Sites/"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all </Directory>
After you update your directory permissions, you will need to restart apache again for these to changes to take effect.