Configuring Apache Virtual Hosts on OSX Yosemite


Having recently made the switch to OSX, I had some difficulties accessing my projects (which I created using my Windows PC). My dev workspace is on my Dropbox, so having them on the Mac was no problem. I’d tried to setup Apache (which came installed with OSX Yosemite) but came unstuck so I ditched the process.

Then I stumbled on this post that showed me how to configure Virtual Hosts using XAMPP on OSX. Long story short, it saved my life! Literally. And so I don’t forget how to do this, and also for others out there, making the transition (however temporal) from Windows to OSX, I’ve reblogged about 95% of the post

Enable VirtualHosts

The first thing you’ll need to do is open the file /Applications/XAMPP/xamppfiles/etc/httpd.conf  in your favourite text editor. Look for the following lines:

Uncomment the second line by removing the hash (#), so that Apache loads your custom VirtualHosts configuration file:

Create your VirtualHosts

Open the file /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf . Towards the bottom of the file you will see some example VirtualHosts, which you should comment out or delete.

At the bottom of the file, add ‘localhost’ as the default named VirtualHost:

This step is necessary to ensure that http://localhost still points at XAMPP’s htdocs  directory once we’ve created our custom VirtualHosts. Personally I don’t use the htdocs directory a lot, but occasionally it’s useful to have somewhere to perform quick tests.

Now you are ready to create your own VirtualHosts. After the default localhost that you just created, add:

In the above example you should replace “mysite.local” with your own hostname. This can be anything you wish, but make sure you choose a hostname that won’t conflict with a real domain name. Using a .local extension makes it obvious that the site is hosted locally rather than on a public web server.

The path to your website can point at any folder in your OS X user directory. I store most of my sites inside of Dropbox so that I can access them on both my home and work machines. If your path includes spaces, make sure you enclose it in quotes, like in my example.

Edit your hosts file

Once you’ve saved your httpd.conf  and httpd-vhosts.conf  files, the next step is to edit your OS X hosts file so it knows how to handle your new ServerName. The hosts file is used by OS X to map hostnames to IP addresses. In this case we want to map your new ServerName to the IP address, which is your localhost.

Fire up a Terminal instance, and at the prompt type the following command:

Enter your OS X password when prompted, and the hosts file will be opened in the nano text editor. You’ll see that the hosts file already contains some default hostname mappings (e.g. “ localhost”). Use your keyboard’s arrow keys to navigate to the bottom of the file and add your own mapping:

Save the host file using the key combo control+o, and pressing return when prompted to choose the filename. Close the file using control+x.

Restart Apache

So that your changes take effect, restart Apache. This can be done using XAMPP Control, which is found at /Applications/XAMPP/XAMPP .

Point your browser at http://mysite.local (or whatever ServerName you chose) and you should see your website. However, there’s a chance that instead you’ll be faced with a…

403 error

Because Apache runs as the ‘nobody’ user by default, it may not have adequate permission to browse your OS X user directory or some of its subdirectories, in which case you’ll see a 403 ‘access forbidden’ error when you try and view your development site. Similarly, you may find that although you can view your dev site, PHP throws errors when you attempt to write files or make directories on the filesystem.

To fix this you can configure Apache to run as your OS X user. Open /Applications/XAMPP/xamppfiles/etc/httpd.conf  and look for the following lines:

Change User to your OS X username, and save the file:

Restart Apache and you should now be able to navigate your site without any issues, including manipulating files and folders using PHP.

If you have problems viewing the XAMPP splash pages at http://localhost  now that Apache is running as your user (e.g. nothing happens when you try to set the language), then you’ll need to give your user read/write privileges on the file /Applications/XAMPP/xamppfiles/htdocs/xampp/lang.tmp .

Making the change I’ve described above carries certain security risks, and if you choose to run Apache as your OS X user then you’ll need to be quite certain that XAMPP is not accessible from outside your local network. From what I understand, XAMPP’s built in security features ensure that this is the case out-of-the-box, and it is straightforward to beef up security for additional piece of mind.

If you’re not convinced that it’s safe to let Apache run as your OS X user, another option is to change the permissions of your dev directories so that the ‘nobody’ or ‘_www’ user can read/write to them.I suspect that I would quickly tire of setting folder permissions, which is why I have opted to take the path of least resistance!

How to determine what programs are bound to what ports on Windows


Sometimes you get the need to know what programs are listening or bound to a particular port on Windows. For me, the most frequent scenario is this: I install WampServer on a PC hoping  to use Apache as a web server only to discover that Apache cannot start.

After wondering (a little stupidly at times) why, I resort to check Apache’s logs and discover that the service isn’t starting because it can’t bind to port 80. Implication, some other program got there first! (Well, not really. Just that the other program was installed first)

Most times, I’d just open up the Apache server config file and change the port which it binds to. Some other times, I’d need to identify the offending program and change the port. At a point in the past, if Skype was installed on Windows, you’d need to instruct Skype to use another port (after maybe spending a whole afternoon wondering why Apache won’t start) as it listens for incoming connections on port 80.

Then comes the hard part: How do I know what program is listening on what port?

It turns out it’s fairly easy. As this StackOverflow Answer points out, running this command:  C:\> netstat -a -b  should do. More from the answer:

(add -n to stop it trying to resolve hostnames, which will make it a lot faster)

-a Displays all connections and listening ports.

-b Displays the executable involved in creating each connection or listening port. In some cases well-known executables host multiple independent components, and in these cases the sequence of components involved in creating the connection or listening port is displayed. In this case the executable name is in [] at the bottom, on top is the component it called, and so forth until TCP/IP was reached. Note that this option can be time-consuming and will fail unless you have sufficient permissions. -n Displays addresses and port numbers in numerical form.

Running this command with the -b option might require administrative privileges as I discovered on my computer.

To get the Process Identified (PID) of the of the process making use of any port, execute the command with the -o option like this:  C:\>netstat -a -o or like this  C:\>netstat -ao

This would show you the PID which you can use to look for the “offending” program or process in the Windows Task Manager.

If you’d rather use a GUI (or you’re not that much of a command-line person), you could use these tools which I found during the course of my search:

  • TCPView –
  • CurrPorts by NirSoft –

Speeding Up Apache, configuring Virtual Hosts the right way


A while back, I thought creating virtual hosts on my Windows development machine was a pretty novel way to develop my PHP applications. So I looked for how to configure my Windows machine and soon enough I modified my hosts file in order to add my virtual hosts.

In making the edits, I did something like this:

Prior to this time, my web server had been relatively good (speed-wise) and I never really noticed the speed drop until I tried to benchmark one of my apps.

When I did, I found out that a page request that was meant to be served in 100-200ms took between 5 and 7 seconds. At first, I thought maybe it was bottlenecks in my code but when I deployed the same application on the production server, response time dropped to the expected 100-200ms band.

So I fired up my browser and tried to Google a solution. I found this somehow related question on server fault:

As indicated in the thread, the OP found his answer here:

As it turns out, I put all of my Virtual Hosts definitions on the same line and it seemed to reduce the response time by about 65%. While this isn’t as fast as it once was, it’s definitely faster than 5-7 seconds.

Reference Links