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:
- Please read the Notes section on the bottom of this article.
- It’s understood you have already installed Apache, PHP, and MySQL on your local environment.
- 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.
- Open the ‘hosts’ file located on ‘/private/etc/’.
- Append the following to this file:
127.0.0.1 yourSiteName 127.0.0.1 anotherSiteName and so on...
- Open ‘yourUserName.conf’ located on ‘/private/etc/apache2/users/’.
- 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...
- Open ‘httpd.conf’ located on ‘/private/etc/apache2/’.
- Search for ‘#Include ‘/private/etc/apache2/extra/httpd-vhosts.conf’.
- Remove ‘#’ to read ‘Include ‘/private/etc/apache2/extra/httpd-vhosts.conf’.
- 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:
- You can’t find ‘etc’ folder. Cause: this folder could be hidden on MACINTOSH HD. Solution: install an application such as Houdini.
- 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.