A while back I posted about configuring virtual hosts in MAMP.
More recently I noticed that MAMP hadn’t been updated in nearly a year and their support forum resembled a wilderness. Thinking that maybe the writing was on the wall for MAMP I started looking for an alternative and came across XAMPP.
XAMPP provides a similar local server environment to MAMP, but configuring virtual hosts is a little different. Unlike the Pro version of MAMP , XAMPP doesn’t have a nice GUI to allow virtual hosts to be configured easily. So, there is no alternative but to do it manually.
With XAMPP we need to edit 3 files as opposed to only the 2 with MAMP. These are /etc/hosts, /Applications/XAMPP/xamppfiles/etc/httpd.conf and /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf. I’m assuming that you’ve not already added virtual hosts to the /etc/hosts file for a previous MAMP install.
Update: A new version of MAMP (1.8) was released in September 2009.
1. What You’ll Need
The /etc/hosts file is hidden and a standard or admin user doesn’t normally have write access to it. To edit this file you can open TextEdit with root privileges from a Terminal window like so.
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
An alternative is to use an editor like Smultron which has an option from the File menu to Open Hidden.. files and will ask for authentication if required. Smultron is free and whilst no longer under development works under 10.5.8.
In the example I’m going to configure a virtual host for a site that has a root folder of /Users/steve/Sites/MySite1/ and I’ll name the virtual host local.mysite1.
Ensure you stop XAMPP’s Apache server before editing either of the files below.
3. Editing the hosts File
Open the /etc/hosts file. You should see something like this:
# /etc/hosts ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost
We need to add a line to the end of this file for each virtual host we want to configure. The comment line is optional.
# Virtual Hosts 127.0.0.1 local.mysite1
The file should now resemble this.
# /etc/hosts ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost fe80::1%lo0 localhost # Virtual Hosts 127.0.0.1 local.mysite1
Save and close the file.
4. Editing XAMPP’s httpd.conf File
Open/Applications/XAMPP/xamppfiles/etc/httpd.conf. On or around line 487 you should see this.
# Virtual hosts # Include etc/extra/httpd-vhosts.conf
We need to un-comment line 488 like so.
# Virtual hosts Include etc/extra/httpd-vhosts.conf
Save and close the file.
5. Editing XAMPP’s httpd-vhosts.conf File
Virtual hosts in XAMPP are configured in the httpd-vhosts.conf file and not the usual httpd.conf file. So, open the file /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf. You should see this.
# # 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 email@example.com DocumentRoot "/Applications/XAMPP/xamppfiles/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/Applications/XAMPP/xamppfiles/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "logs/dummy-host2.example.com-error_log" CustomLog "logs/dummy-host2.example.com-access_log" common </VirtualHost>
Remove the example virtual host configurations beginning on line 23 then add the following code block at the end of the file.
Note that although I only want to configure a virtual host for local.mysite1, beginning on line 24 I’ve added a virtual host for localhost as the default named sever with the ServerName set to localhost and the DocumentRoot and <Directory> set to /Applications/XAMPP/htdocs. If you don’t, when you point your browser to http://localhost all you’ll see is the directory listing of the first virtual host, in this case local.mysite1, and not the XAMPP start page. You do not need to add a corresponding entry in /etc/hosts as this is included by default.
For the local.mysite1 configuration you can see that beginning on line 37 we are setting the ServerName to the name of virtual host we configured in the /etc/hosts file. The DocumentRoot and <Directory> are set to the site’s root folder.
# # 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. # # localhost [must be included as the default named server] <VirtualHost *:80> ServerName localhost DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs" <Directory "/Applications/XAMPP/xamppfiles/htdocs"> Options Indexes FollowSymLinks Includes execCGI AllowOverride None Require all granted </Directory> </VirtualHost> # mysite1.com <VirtualHost *:80> ServerName local.mysite1 DocumentRoot "/Users/steve/Sites/MySite1" <Directory "/Users/steve/Sites/MySite1"> Options Indexes FollowSymLinks Includes execCGI AllowOverride None Require all granted </Directory> </VirtualHost>
Save and close the file.
6. Testing the Virtual Host
To ensure the virtual host has been configured correctly, restart XAMPP’s Apache server and point your browser to http://local.mysite1
I would recommend backing-up the /etc/hosts, /Applications/XAMPP/xamppfiles/etc/httpd.conf and /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf files whenever you make changes and store them on a separate drive or USB stick. If you later re-install the OS or XAMPP you’ll lose the changes to the respective files. This way you can easily restore your virtual host configuration.