DEV Community

Cover image for Mac OS X setting up virtual hosts
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Mac OS X setting up virtual hosts

You might remember the days of MAMP/XAMP/WAMP? Well, for Mac OS X, we don't need these tools anymore.

It's actually possible to host a website on your local Mac, and it's not as hard as you would think!

Today, we'll set up a local PHP website that we can reach through our browser.

Enabling virtual hosts on Mac OS X

The first step to making this work is enabling virtual hosts on Mac OS X.

To do this, we need to modify the httpd.conf file. Execute the following command in your terminal.



sudo nano /etc/apache2/httpd.conf


Enter fullscreen mode Exit fullscreen mode

Look for the following line:



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


Enter fullscreen mode Exit fullscreen mode

And below that, on a new line add the following:



Include /private/etc/apache2/vhosts/*.conf


Enter fullscreen mode Exit fullscreen mode

This tells Apache to load all .conf files in this directory.

Note: You could also add all the hosts in the httpd-vhost file, but I found this a cleaner method.

Now we need to make the configuration files, make sure the directory exist or create it.



mkdir /etc/apache2/vhosts


Enter fullscreen mode Exit fullscreen mode

Now we can create our first configuration in that folder.



sudo nano /etc/apache2/vhosts/daily-dev-tips.conf


Enter fullscreen mode Exit fullscreen mode

Place the following information inside:



<VirtualHost *:80>
    DocumentRoot "/Users/chrisbongers/www/daily-dev-tips"
    ServerName daily-dev-tips.local

    <Directory "/Users/chrisbongers/www/daily-dev-tips">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>


Enter fullscreen mode Exit fullscreen mode

You have to set the DocumentRoot to your own directory on your local machine. For me, it's a www folder at my user level.
Next, set the ServerName to the URL you want to serve it on.

Now we need to restart Apache



sudo apachectl restart


Enter fullscreen mode Exit fullscreen mode

However, if we now visit: http://daily-dev-tips.local we don't see anything...

Mapping the local domain

To make the local domain work, we need to map the domain to our local server.

Modify your host file.



sudo nano /etc/hosts


Enter fullscreen mode Exit fullscreen mode

And add a line like this:



127.0.0.1       daily-dev-tips.local


Enter fullscreen mode Exit fullscreen mode

Make sure you use the domain you set in the vhost file.

Testing our website

If you open your website link, another app might point to the default page, depending on whether you already set up the folder.

I didn't, so create the folder in the place you provided in the vhost file and create a simple index.php inside.



<?php
echo 'Hello world from my own Mac OS X server';
?>


Enter fullscreen mode Exit fullscreen mode

Now open the website again and see the beauty of your server.

Mac OS X Virtual Host server

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (0)