Create a Virtual Host in OSX
As someone who had always developed on PCs, switching over to using Mac OS X was like going from peasant to prince. Compared to Windows-based machines, Mac's OS X operating system is light years better. One OS X feature I make much use of is the integrated Apache server. Hosting websites on my local machine during development is a must. Let me show you how to create multiple websites on your local machine.
Step 1: Create Website Folder
User websites appear in the Users/Sites/ directory. Let's create a directory within Sites named "mynewsite":
cd ~/Sites/ mkdir mynewsite
As you'll see in a moment, you can add this directory anywhere you'd like, but for the sake of this post we'll add it in the standard Sites directory.
Step 2: Edit
The next step is adding a new record to Apache's
httpd-vhosts.conf file. My
httpd-vhosts.conf file is located within the following directory:
private/etc/apache2/extra/. This configuration file holds your website virtual host location details. Let's add mynewsite to the configuration:
<VirtualHost *:80> DocumentRoot "/Users/myUserName/Sites/mynewsite" ServerName mynewsite.local </VirtualHost>
The two important configurations are DocumentRoot and ServerName. DocumentRoot points to the directory we created in step 1. ServerName refers to the address we want to type into the browser to get to this new site. I generally create all my hosts with the ".local" domain.
You can also place more specific configurations within this new virtual host, including ProxyPass, directory settings, error documents, redirects, and other settings you sometimes see in .htaccess files:
<VirtualHost *:80> DocumentRoot "/Users/myUserName/Sites/mynewsite" ServerName mynewsite.local <directory "/Users/davidwalsh83/Sites/mynewsite/trunk/"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </directory> ProxyPass /web-service http://someotherwebsite.com/api/ ProxyPassReverse /web-service http://someotherwebsite.com/api/ </VirtualHost>
You can learn more about the possible settings here: http://httpd.apache.org/docs/2.2/vhosts/
Step 3: Edit
hosts file contains a list of host names and IP address to which they match. We'll add another record to this file which points the new website's
ServerName setting from above (mynewsite.local) to the localhost IP address:
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost 127.0.0.1 mynewsite.local
Now mynewsite.local host name is pointing toward your machine. Coupled with the
httpd-vhosts.conf file's new settings, the address will find your mynewsites directory!
Step 4: Apache Restart
The last step in the process is restarting your Apache install:
The other method of quickly restarting Apache is by opening System Settings >> Sharing and unchecking and then checking "Web Sharing". That's all! Now you can navigate to http://mynewsite.local and see your web files!
Quick Side Note
Your IT people would probably prefer you not use your machine's Apache and instead use a virtual machine for your web development. I've personally felt the pain of using my MacBook Pro instead of a virtual machine but that was due to MySQL and not Apache. This tutorial is meant to show you how to quickly get a new site up and running on your local machine. The same process can be used to create a new virtual host on other machines but the file locations will simply be different.