Virtual Hosts with XAMPP

This article provides some help to those who needs to set up virtual hosts with XAMPP on macOS.

Edit /etc/hosts macOS system file

First, you need to edit /etc/hosts file.
To do, let’s follow these instructions :

  • Go to : Go > Go to folder : in finder
  • Then type /etc
  • Find hosts file
  • Open file with text editor like Atom, Sublime, Visual Studio Code or even TextEdit

Add to the end of file : KEEP LOCALHOST LINES

127.0.0.1	domainNameofYourFirstSite
127.0.0.1	domainNameofYourSecondSite

... Repeat it as long as you want

Edit httpd-vhosts.conf XAMPP file

Second, you need to edit /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf
You should see something like this :

<VirtualHost *:80>
	ServerAdmin webmaster@dummy-host.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 webmaster@dummy-host2.example.com
	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>

We have to save original way of working, so change the first on to get :

<VirtualHost *:80>
	ServerName localhost
	DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
	<Directory "/Applications/XAMPP/xamppfiles/htdocs">
		Options Indexes FollowSymLinks Includes execCGI
		AllowOverride All
		Require all granted
	</Directory>
	ServerAlias localhost
</VirtualHost>

Then you can add any different hosts by this way :

<VirtualHost *:80>
	ServerName DomainNameofYourSite
	DocumentRoot "PathToYourSiteFiles"
	<Directory "PathToYourSiteFiles">
		AllowOverride All
		Require all granted
	</Directory>
	ServerAlias DomainNameofYourSite
</VirtualHost>

Edit httpd.conf XAMPP file

Thirdly, edit /Applications/XAMPP/xamppfiles/etc/httpd.conf and uncomment (remove # character at beginning) of #Include etc/extra/httpd-vhosts.conf

Then, you have to change User which run server at :

User deamon
Group deamon

to

User yourSessionUsername
Group deamon

Then you can restart Apache Server of XAMPP and if you have an index.[html/php] file at paths of your sites, you can access them with browser by reaching your defined domain names.