Setup apache virtual hosts in Mac OS X and xampp

When we develop projects sometime we need to make the website act as a document root like first party domain. By accomplish this we need to set up virtual hosts on the local server. I’ll explain here about this using XAMPP on MAC OS X LION. XAMPP uses Apache web server so we need to do it by configuring Apache. But who are new in development and MAC OS may fall some problem with it. I’ll discuss on the problems you may face here.

To create virtual hosts on our OS X with XAMPP, we need to do the following:

  1. Modify the httpd.conf to include the virtual hosts file
  2. Create a virtual hosts entry for each virtual host
  3. Modify the hosts file to work the virtual hosts you added.

Modify the httpd.conf

All you need to open and edit the httpd.conf which is located at /Applications/XAMPP/etc/httpd.conf. By default it is locked and you can’t edit it with GUI without rooting. So open your terminal. and add the following command :

sudo nano /Applications/XAMPP/etc/httpd.conf

Uncomment the following line

Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf

Save it by Control+X and then Y then enter.

Create a virtual hosts

Now you need to open the httpd-vhosts.conf which is located at /Applications/XAMPP/etc/extra/httpd-vhosts.conf. Same as above open terminal use the following command:

sudo nano /Applications/XAMPP/etc/extra/httpd-vhosts.conf

Comment out or delete everything of the example of virtual hosts then add the following to keep http://localhost working

# this keeps http://localhost working
<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
ServerName localhost
</VirtualHost>

Now Add your virtual host by adding the following:

# this adds a virtual host called planet.local which will have files located in
# /Applications/XAMPP/xamppfiles/htdocs/planet
<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/planet"
ServerName planet.local
ServerAlias www.planet.local
</VirtualHost>

Save it by Control+X , Y then enter.

Modify the hosts file

Open Terminal, if itโ€™s not already open. Then type this command to edit the hosts file.

sudo nano /etc/hosts

At the end of the file, enter the virtual host and ip here our ip is 127.0.0.1

127.0.0.1 planet.local

Use XAMPP Control to stop and then start Apache.

Open your browser and try http://localhost and the virtual host you added by entering like http://planet.local and see it works !

Hope you will able to add virtual host easily with XAMPP and MAC OS X.

I’ll continue with setting up virtual host with SSL as well for local development. Just stay tuned you’ll see …

Cheers!

Advertisements

, , , ,

  1. #1 by koddi on April 7, 2012 - 12:02 PM

    Hello.
    I’m new to xampp on mac. Thanks for this tip start.

    But on modifying hosts file I guess you mean :

    sudo nano /etc/hosts.

    By the way, I think it is also helpful to have separate error log and access log for each virtual hosts.

    I set mine like this :

    # this keeps http://localhost working

    DocumentRoot “/Applications/XAMPP/xamppfiles/htdocs”
    ServerName localhost
    ServerAdmin webmaster@localhost
    ErrorLog “logs/error_log”
    CustomLog “logs/access_log”

    # this adds a virtual host called planet.local which will have files
    # located in /Applciations/XAMPP/xamppfiles/htdocs/planet

    DocumentRoot “/Applications/XAMPP/xamppfiles/htdocs/planet”
    ServerName planet.local
    ServerAlias http://www.planet.local
    ServerAdmin webmaster@localhost
    ErrorLog “logs/planet-error_log”
    CustomLog “logs/planet-access_log”

    Thanks.

  2. #3 by Ari Palo (@aripalo) on July 24, 2012 - 1:43 PM

    At the end of the article:


    Modify the hosts file

    Open Terminal, if itโ€™s not already open. Then type this command to edit the hosts file.

    sudo nano /Applications/XAMPP/etc/extra/httpd-vhosts.conf

    Instead of /Applications/XAMPP/etc/extra/httpd-vhosts.conf it should be /etc/hosts file where you add the “127.0.0.1 planet.local” line.

    • #4 by Rashedul Islam Sumon on August 5, 2012 - 3:42 AM

      Thanks for your comment I just edited the line. It’s a silly mistake ๐Ÿ™‚

  3. #5 by PhaniKiran Gutha on August 4, 2012 - 6:44 PM

    HI,
    Good article ..
    i sucessfully setup the name based virtual hosts.
    can you let us know how to setup ip based virtual host in Mac.

    • #6 by Rashedul Islam Sumon on August 5, 2012 - 3:48 AM

      I think you can do it the same way because in the hosts file where you adding the domain name you’re also giving the ip address. Such as you can write a line in the /etc/hosts like your_real_ip_address yourdomain.com.

  4. #7 by Paul on September 5, 2012 - 1:53 PM

    Thanks…

    This worked a treat, I’ve but meaning to setup virtual hosts for along time on my local machine and this was painless to follow ๐Ÿ˜‰

    No more include/absolute path headaches

  5. #8 by Tauseef Khan on February 25, 2013 - 8:38 PM

    Hey Thanks Man.

    Short and Best Article.. It helped me ๐Ÿ™‚

    KEEP IT UP (Y)

  6. #9 by RAA on March 24, 2014 - 8:54 AM

    Hi,

    Did you manage to write the follow up article about setting up virtual host with SSL ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: