When we develop projects sometime we need to make the website act as a document root like first party domain. By accomplish this we need to set up virtual hosts on the local server. I’ll explain here about this using XAMPP on MAC OS X LION. XAMPP uses Apache web server so we need to do it by configuring Apache. But who are new in development and MAC OS may fall some problem with it. I’ll discuss on the problems you may face here.
To create virtual hosts on our OS X with XAMPP, we need to do the following:
- Modify the httpd.conf to include the virtual hosts file
- Create a virtual hosts entry for each virtual host
- Modify the hosts file to work the virtual hosts you added.
Modify the httpd.conf
All you need to open and edit the httpd.conf which is located at /Applications/XAMPP/etc/httpd.conf. By default it is locked and you can’t edit it with GUI without rooting. So open your terminal. and add the following command :
sudo nano /Applications/XAMPP/etc/httpd.conf
Uncomment the following line
Save it by Control+X and then Y then enter.
Create a virtual hosts
Now you need to open the httpd-vhosts.conf which is located at /Applications/XAMPP/etc/extra/httpd-vhosts.conf. Same as above open terminal use the following command:
sudo nano /Applications/XAMPP/etc/extra/httpd-vhosts.conf
Comment out or delete everything of the example of virtual hosts then add the following to keep http://localhost working
# this keeps http://localhost working <VirtualHost *:80> DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs" ServerName localhost </VirtualHost>
Now Add your virtual host by adding the following:
# this adds a virtual host called planet.local which will have files located in # /Applications/XAMPP/xamppfiles/htdocs/planet <VirtualHost *:80> DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/planet" ServerName planet.local ServerAlias www.planet.local </VirtualHost>
Save it by Control+X , Y then enter.
Modify the hosts file
Open Terminal, if it’s not already open. Then type this command to edit the hosts file.
sudo nano /etc/hosts
At the end of the file, enter the virtual host and ip here our ip is 127.0.0.1
Use XAMPP Control to stop and then start Apache.
Hope you will able to add virtual host easily with XAMPP and MAC OS X.
I’ll continue with setting up virtual host with SSL as well for local development. Just stay tuned you’ll see …