The term Virtual Host refers to the practice of running more than one website (such as example1.com and example2.com) on a single machine. A virtual host can be “IP-based”, meaning you have a different IP address for each website, or “name-based”, meaning you have multiple names running on each IP address. The fact that they run on the same physical server is invisible to the end-user.
Apache was one of the first servers to support IP-based virtual hosts. Apache version 1.1 and later support IP-based and name-based virtual hosts (vhosts). The last variant of a virtual host is sometimes also called a host-based virtual host or non-IP.
Virtual Host Configuration on XAMPP
- Open the Apache Virtual Hosts configuration file in the XAMPP installation directory (for example) in “C:xamppapacheconfextrahttpd-vhosts.conf
- Add the following code at the end of the file
<VirtualHost *:80> ServerName www.example1.com DocumentRoot "C:\XAMPP\htdocs" <Directory "C:\XAMPP\htdocs"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost>
Add a setting at the end of a windows host file in “C:\Windows\System32\drivers\etc\host“.
127.0.0.1 www.example1.com
Other Interesting Articles
Virtual Configuration hosts multiple domains with one public IP
- For example, we have 2 domains with www.example1.com and www.example2.com names.
- Add the following code at the end of the httpd-vhosts.conf file
<VirtualHost *:80> ServerName www.example1.com DocumentRoot "C:\XAMPP\htdocs\www.example1.com" <Directory "C:XAMPP\htdocs\www.example1.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost> <VirtualHost *:80> ServerName www.example2.com DocumentRoot "C:\XAMPP\htdocs\www.example2.com" <Directory "C:\XAMPP\htdocs\www.example2.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost>
- Add a setting at the end of a windows host file in “C:WindowsSystem32driversetchost.
127.0.0.1 www.example1.com 127.0.0.1 www.example2.com
Virtual Configuration Of Multiple Domain and IP Hosts
- For example we have 2 domains with www.example1.com names, ip 172.20.30.40 and www.example2.com, ip 172.20.30.50.
- Add the following code at the end of the httpd-vhosts.conf file
<VirtualHost 172.20.30.40:80> ServerName www.example1.com DocumentRoot "C:\XAMPP\htdocs\www.example1.com" <Directory "C:XAMPP\htdocs\www.example1.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost> <VirtualHost 172.20.30.50:80> ServerName www.example2.com DocumentRoot "C:\XAMPP\htdocs\www.example2.com" <Directory "C:\XAMPP\htdocs\www.example2.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost>
- Add a setting at the end of a windows host file in “C:WindowsSystem32driversetchost.
127.0.0.1 www.example1.com 127.0.0.1 www.example2.com
Virtual Host Configuration with Different Ports
- For example we have 2 domains with www.example1.com names, ip 172.20.30.40 and www.example2.com, ip 172.20.30.50 running on port 80 and www.example3com, ip 172.20.30.50 with port 8080.
- Add the following code at the end of the httpd-vhosts.conf file
<VirtualHost 172.20.30.40:80> ServerName www.example1.com DocumentRoot "C:\XAMPP\htdocs\www.example1.com" <Directory "C:\XAMPP\htdocs\www.example1.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost> <VirtualHost 172.20.30.50:80> ServerName www.example2.com DocumentRoot "C:\XAMPP\htdocs\www.example2.com" <Directory "C:\XAMPP\htdocs\www.example2.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost> <VirtualHost 172.20.30.50:8080> ServerName www.example3.com DocumentRoot "C:\XAMPP\htdocs\www.example3.com" <Directory "C:\XAMPP\htdocs\www.example3.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </Directory </VirtualHost>
- Add a setting at the end of a windows host file in “C:\Windows\System32\drivers\etc\host”.
127.0.0.1 www.example1.com 127.0.0.1 www.example2.com 127.0.0.1 www.example3.com
Configure Virtual Host with SSL
Create an SSL Certificate, discussed in the next article.
For example a certificate with the file name “server.crt“, “server.key“- Place the SSL certificate on “C:\XAMPP\apache\conf”
- Add the following code at the end of the httpd-vhosts.conf file
<virtualhost *:443> ServerName www.example1.com DocumentRoot "C:\XAMPP\htdocs\www.example1.com" SSLEngine on SSLCertificateFile "conf/ssl.crt/server.crt" SSLCertificateKeyFile "conf/ssl.key/server.key" <directory "c:\xampp\htdocs\www.example1.com"> Options Indexes FollowSymLinks Includes ExecCGI Order allow,deny Allow from all </directory> </virtualhost>