How To Set Up Virtual Hosts On Mac OS X Lion 10.7.5

How To Set Up Virtual Hosts On Mac OS X Lion 10.7.5

When building a new website, I set up a virtual host in my development environment, because it helps me get organized. One of the advantages is that the site’s URL shortens to http://mySiteName. If you want to set up one or more virtual host(s), you will need to update three files: hosts, yourUserName.conf, httpd.conf. Here are the steps to follow:

  1. Please read the Notes section on the bottom of this article.
  2. It’s understood you have already installed Apache, PHP, and MySQL on your local environment.
  3. It’s understood you have already created at least one subfolder named ‘yourSiteName’ in the Sites folder, which contains your site’s files. All your sites should be distributed in separate subfolders.
  4. Open the ‘hosts’ file located on ‘/private/etc/’.
  5. Append the following to this file:
    127.0.0.1	yourSiteName
     127.0.0.1	anotherSiteName
    and so on...
  6. Open ‘yourUserName.conf’ located on ‘/private/etc/apache2/users/’.
  7. Edit this file to look as follows:
    <directory "/Users/yourUserName/Sites/*/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </directory>
    
    NameVirtualHost *:80
     
    <virtualhost *:80>
        DocumentRoot /Users/yourUserName/Sites/yourSiteName
        ServerName yourSiteName
    </virtualhost>
    
    <virtualhost *:80>
    	DocumentRoot /Users/yourUserName/Sites/anotherSiteName
        ServerName anotherSiteName
    </virtualhost>
    and so on...
  8. Open ‘httpd.conf’ located on ‘/private/etc/apache2/’.
  9. Search for ‘#Include ‘/private/etc/apache2/extra/httpd-vhosts.conf’.
  10. Remove ‘#’ to read ‘Include ‘/private/etc/apache2/extra/httpd-vhosts.conf’.
  11. Restart Apache: go to System Preferences > Sharing, check off ‘Web Sharing’, and check it on again. You should be able to retrieve your site on the browser by entering ‘http://yourSiteName’ in the address toolbar.

Notes: challenges you might experience:

  1. You can’t find ‘etc’ folder. Cause: this folder could be hidden on MACINTOSH HD. Solution: install an application such as Houdini.
  2. You can’t edit file(s) mentioned above. Cause: the file is locked. Solution: install TextWrangler, which allows you to unlock the file by clicking the pencil on left top corner once you retrieve the file in this application.

RELATED

You might be interested in the following: