I just a bought a new iMac a few weeks ago. Determined to improve my workflow as much as possible I decided to do something about an old annoyance.
As a web developer I have dozens of website living in my “Sites” folder on Mac OSX 10.6. Each time I wanted to access a website on “localhost” I had to open up my httpd.conf file and uncomment two lines related to the site I wanted to work on.
This is okay, as long as you’re only working on one site at a time. But recently I found myself working on four sites at the same time. I needed a better solution.
I wanted to have several subdomains that I could set up and bookmark for easy access to all my sites in progress. Like so:
If you’re reading this, you probably just want to know how it’s done. Here you go:
Step 1: Edit your httpd.conf file.
(You’ll need a text editor that can open hidden files. I use BBEdit.)
Add some “Virtual Hosts” to your httpd.conf file (located in /private/etc/apache2) below the settings for the document root (right after the last occurrence of “</Directory>” around line 224).
# Virtual Hosts
# Note: You also need to edit the hosts file /private/etc/hosts
Options Indexes FollowSymLinks
Allow from all
CustomLog "/private/var/log/apache2/wordpress.localhost-access_log" common
CustomLog "/private/var/log/apache2/phpmyadmin.localhost-access_log" common
CustomLog "/private/var/log/apache2/client1.localhost-access_log" common
CustomLog "/private/var/log/apache2/client2.localhost-access_log" common
Obviously, you’ll want to change the paths to wherever your sites live. You can make as many virtual hosts as you like. You’ll notice my wordpress configuration (the first virtual host) has some extra stuff in between a <Directory> tag. This allows you to override the default security options.
Allow from all
That basically tells apache that any .htaccess files inside the WordPress site take priority over any other rules set in parent directories.
Step 3: Edit your hosts file
Edit your “hosts” file (located at /private/etc/hosts). You’ll want one subdomain for each virtual host you configured in your httpd.conf file. Make sure the subdomains in your hosts file match ServerName in the virtual hosts defined in your httpd.conf file. Here’s the full content of my hosts file:
# Host Database
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
Step 4: Restart Your Web Server
Go to System Preferences -> Sharing and toggle “Web Sharing” off and back on again. This will reload your new server configuration file. You should be able to browse to each of your new domains.