How to setup virtual hosts on Mac OS X 10.5 (Leopard)
Posted: 20 September 2008
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.
mate /private/etc/apache2/extra/httpd-vhosts.conf
Append to this bottom of this file an entry for your virtual domain:
<VirtualHost *:80>
ServerAdmin webmaster@learnhowtojuggle.dev
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.
mate /private/etc/apache2/httpd.conf
Locate the following line and remove the # sign (also known as an "octothorpe" ) from in front of it.
Include /private/etc/apache2/extra/httpd-vhosts.conf
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.
mate /private/etc/hosts
Add the following entry:
127.0.0.1 learnhowtojuggle.dev
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.
Additional Configuration
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.
Example configuration:
<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.
Post a comment
Comment Guidelines
- You can subscribe to the comments on this entry via RSS.
- Have no more than 2 links, otherwise your comment will be flagged as spam.
- Links are automagically generated.
- <em>text</em> to make text italic.
- <strong>text</strong> to make text bold.
JavaScript needs to be enabled to comment.
Your comments (subscribe)
Danny 27 Jan 09 at 3:43pm
Cool, this worked and is VERY helpful. :)
MIchael 10 Apr 09 at 1:11pm
excellent. worked like a charm. I didn't know I had a user.conf file. I think that's where maybe I was going wrong before.
much appreciated and very straight forward tutorial.
Cheers, Michael
erick2red 21 May 09 at 5:01am
I'm having troubles at seeing my home folder at http://localhost/~myuser/. It's telling me redmine didn't found it.
Neil 22 May 09 at 1:17pm
Redmine is RoR application. Turn it off and see if you get a different error.