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
Look for the following line:
And below that, on a new line add the following:
This tells Apache to load all
.conf files in this directory.
Note: You could also add all the hosts in the
httpd-vhostfile, but I found this a cleaner method.
Now we need to make the configuration files, make sure the directory exist or create it.
Now we can create our first configuration in that folder.
sudo nano /etc/apache2/vhosts/daily-dev-tips.conf
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>
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
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
And add a line like this:
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.
echo 'Hello world from my own Mac OS X server';
Now open the website again and see the beauty of your server.
Thank you for reading, and let's connect!
Interested in reading more such articles from Chris Bongers?
Support the author by donating an amount of your choice.