Create virtual hosts using apache in Mavericks

Starting apache in mavericks

Apache comes preinstalled with Mac OSX Mavericks so you dont have to install it seperately.

Starting apache

In terminal enter

sudo apachectl start

Stoping apache

sudo apache stop

Terminal will ask for administrator password.

Restarting apache

sudo apache restart

Enabling virtualhost

You have to enable virtualhost in httpd.conf file inside apache folder. Find the following entry in config file and uncomment to enable.

# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

Create virtual hosts

/etc/apache2/extra/httpd-vhosts.conf contains the information about virtual hosts in mavericks. Locate it and start adding vhosts entries. It should look like below.

 #
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

## Use name-based virtual hosting.
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

<VirtualHost *:80>
    ServerAdmin postmaster@cp.local
    DocumentRoot "/xampp/htdocs/cp/php"
    ServerName cp.local
    ServerAlias www.cp.local
	ErrorLog "/private/var/log/apache2/apple.com-error_log"
	CustomLog "/private/var/log/apache2/apple.com-access_log" common

</VirtualHost>

<VirtualHost *:80>
    ServerAdmin postmaster@cp.local
    DocumentRoot "/xampp/htdocs/media/php"
    ServerName media.cp.local
	ErrorLog "/private/var/log/apache2/apple.com-error_log"
	CustomLog "/private/var/log/apache2/apple.com-access_log" common

</VirtualHost>

Add sites on hosts file

Locate the /private/etc/hosts file and add entires containing ip address and site name. See the example below.

127.0.0.1    cp.local
127.0.0.1    media.cp.local

Now restart the apache using terminal.

sudo apachectl restart
Troubleshooting

Error: You dont have permission on the / folder on this mac or something like this.

Solution: You need to edit /private/etc/apache2/httpd.conf file to fix this kind of error. Open up in the editor and fine the --- and replace it with following .

<Directory >
    #Options FollowSymLinks
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order deny,allow
    Allow from all
</Directory>