Configuring VirtualHosts in XAMPP on Mac
A few weeks back I rejoined the “ Cult of Mac ” when I replaced my honest-to-god Asus notebook with a MacBook Pro, and since then I ’ ve been busy settling into my fresh OS X work flow. I do all my development locally, so one of the inaugural applications I installed was XAMPP, a traverse chopine Apache/MySQL/PHP push-down storage. While I know that MAMP is identical popular on Mac, I have been using XAMPP for many years then I thought I ’ five hundred adhere with what I know .
Installing XAMPP was a snap, but when I came to create my own Apache VirtualHosts things started getting fiddly. here are the steps I followed to get everything running smoothly .
What are VirtualHosts?
First, some quick background on what we ’ re trying to achieve.
VirtualHosts allow Apache to map a hostname to a directory on the filesystem. You can set up as many VirtualHosts as you need, so that each web site operates under its own hostname. For exercise, you might want to map
/Users/yourusername/mysite. To test your exploitation web site all you would need to do is plug “ hypertext transfer protocol : //mysite.local ” into your browser ’ mho address bar .
The inaugural thing you ’ ll necessitate to do is open the file
/Applications/XAMPP/xamppfiles/etc/httpd.conf in your front-runner text editor. Look for the keep up lines :
# Virtual hosts #Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf
Uncomment the second lineage by removing the hash (
# ), thus that Apache loads your custom-made VirtualHosts shape file :
# Virtual hosts Include /Applications/XAMPP/etc/extra/httpd-vhosts.conf
Create your VirtualHosts
Open the file
/Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf. Towards the bottomland 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 nonpayment named VirtualHost :
ServerName localhost DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs" Options Indexes FollowSymLinks Includes execCGI AllowOverride All Require all granted
This step is necessary to ensure that hypertext transfer protocol : //localhost placid points at XAMPP ’ s
htdocs directory once we ’ ve created our custom-made VirtualHosts. personally I don ’ deoxythymidine monophosphate use the
htdocs directory a bunch, but occasionally it ’ second useful to have somewhere to perform agile tests .
now you are quick to create your own VirtualHosts. After the default localhost that you fair created, add :
# My custom host
ServerName mysite.local DocumentRoot "/Users/yourusername/path/to/your/site" Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all grantedErrorLog "logs/mysite.local-error_log"
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 ’ metric ton conflict with a actual knowledge domain name. Using a .local annex makes it obvious that the site is hosted locally rather than on a public web waiter .
The way to your web site 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 exercise .
Edit your hosts file
once you ’ ve saved your
httpd-vhosts.conf files, the future measure is to edit your OS X hosts file so it knows how to handle your raw
ServerName. The hosts file is used by OS X to map hostnames to IP addresses. In this lawsuit we want to map your newly
ServerName to the IP address 127.0.0.1, which is your localhost.
Fire up a Terminal exemplify, and at the prompt type the comply command :
sudo nano /etc/hosts
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. “ 127.0.0.1 localhost ” ). Use your keyboard ’ sulfur arrow keys to navigate to the bed of the file and add your own mapping :
# XAMPP VirtualHost mappings 127.0.0.1 mysite.local
Save the host file using the key jazz band
control+o, and pressing revert when prompted to choose the filename. Close the charge using
indeed that your changes take effect, restart Apache. This can be done using XAMPP Control, which is found at
/Applications/XAMPP/XAMPP Control.app .
Point your browser at hypertext transfer protocol : //mysite.local ( or whatever ServerName you chose ) and you should see your web site. however, there ’ s a gamble that rather you ’ ll be faced with a…
Because Apache runs as the ‘ cipher ’ user by default, it may not have adequate license 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 locate. similarly, you may find that although you can view your dev locate, 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 drug user. Open
/Applications/XAMPP/xamppfiles/etc/httpd.conf and look for the take after lines :
# User/Group: The name (or #number) of the user/group to run httpd as. # It is usually good practice to create a dedicated user and group for # running httpd, as with most system services. # User daemon Group daemon
User to your OS X username, and save the file :
Restart Apache and you should now be able to navigate your locate without any issues, including manipulating files and folders using PHP .
If you have problems viewing the XAMPP spatter pages at
http://localhost immediately that Apache is running as your exploiter ( e.g. nothing happens when you try to set the speech ), then you ’ ll need to give your user read/write privileges on the charge
Making the change I ’ ve described above carries certain security risks, and if you choose to run Apache as your OS X exploiter then you ’ ll indigence to be quite certain that XAMPP is not accessible from outside your local network. From what I understand, XAMPP ’ second built in security features ensure that this is the subject out-of-the-box, and it is square to beef up security system for extra man of mind .
If you ’ re not convinced that it ’ south condom to let Apache run as your OS X user, another option is to change the permissions of your dev directories so that the ‘ cipher ’ or ‘ _www ’ user can read/write to them.I distrust that I would cursorily tire of setting booklet permissions, which is why I have opted to take the path of least electric resistance !
hopefully this post helps person else to get XAMPP up and running on OS X. I imagine that MAMP Pro streamlines this procedure reasonably ( I know it has a ace for creating VirtualHosts ), but american samoa long as you don ’ deoxythymidine monophosphate mind getting your hands dirty XAMPP is a fantastic way to learn how Apache actually works .