Virtual Host is a configuration in Apache that allows multiple domains to run on one server. There is another concept mentioned in Nginx that has the same functionality as Virtual Host called Server Block. In this article I just want to share with you more about the way to configure a Virtual Host in Windows, here I use XAMPP as the development environment.
You can download XAMPP from: https://www.apachefriends.org
After the installation is complete, you open: xampp_directory/apache/conf/httpd.conf
Find Include conf/extra/httpd-vhosts.conf if you see the # at the beginning of the line, remove the # sign just keep Include conf/extra/httpd-vhosts.conf. This line is just to load the config from another file into the httpd.conf file.
With Xampp, this file is included by default. But I still want to say this step so that if you use other software to create Web Server as Vertrigo, WAMP, App Server, etc, can still use the Virtual Host configuration. In particular, you can also use a file with a different name (This is not important, as long as you include the Virtual Host configuration in the httpd.conf).
Now open the file xampp_directory/apache/conf/extra/httpd-vhosts.conf and copy the code below and paste it into the httpd-vhosts.conf file.
DocumentRoot "<The directory path will be called when accessing yourdomain.com>"
CustomLog "logs/yourdomain.com-access.log" common
<Directory "<directory path>"
Require all granted
- <VirtualHost></VirtualHost>: This is the opening and closing tags for a Vhost declaration. There is a small note that if you do change the port of Apache (8080,81, etc.), then also change the port 80 as you switch to offline.
- ServerAdmin: Email address of admin (Optional).
- DocumentRoot: This is the keyword that declares the path of the directory to be called when the user accesses the configured domain. This path can be in any one partition.
- ServerName: This is the domain you want to set.
- ErrorLog: Specifies the path of the log file when there is an error (not required) xampp_directory/apache/logs is the directory that contains the log file.
- CustomLog: Log all URIs that the user requests are not required xampp_directory/apache/logs is the directory that contains the log file.
- <Directory></Directory>: This is a pair of tags used to grant access to the folder if the folder is not licensed will encounter 403 error when you try to access. Your directory path can be a regular expression. If you do not know about Regex then you can use the same directory path as the declaration in DocumentRoot or refer to http://httpd.apache.org/docs/2.4/en/mod/core.html#directory
- Options FollowSymLinks This option allows you to access symbolic links (like shortcuts in windows). There are also many other options, which you can refer to here: http://httpd.apache.org/docs/2.4/en/mod/core.html#options
- AllowOverride: All allows you to overwrite config files with the htaccess file.
- DirectoryIndex: index.php When you access the directory (no specific file specified), the index.php file will be called.
- Require all granted: is to allow all other machines to access the directory being configured. Note: From 2.2 and earlier, Apache uses Allow from all to replace for Require all granted
Restart Apache by starting XAMPP to update the configuration file.
Ok so now that you’ve configured the Apache part, now just registering DNS for your domain is all done.
You open %windir%/system32/drivers/etc/hosts using notepad or any other editor. Add to the end of the file and save it. If you can not save the hosts file, please turn off readonly and try running again with Administrator mode.
When you type yourdomain.com address, the machine will prefer to find the IP address of that domain in the hosts file of the operating system first.
If not, then it will look for the IP address of this domain through the DNS Server on the Internet. Since you have declared in the hosts file, it will use the IP address declared there. Here the IP address 127.0.0.1 points to the machine itself is access (your machine – if configured on the server, it is the server itself is always). You can use the long IP from 127.0.0.1 -> 127.255.225.254 this is the IP range pointing to your machine.
Now open your browser and type in the address bar replacing the ugly localhost with a domain of your own: http://yourdomain.com
Using Virtual Hosts has several benefits.
- You can access any code directory without having to copy to htdocs in XAMPP.
- If you use the code folder on another partition, you do not need to worry about backing up the folder code each time you reinstall windows.
You can also configure Virtual host with WAMP, APPServer, USBServer configuration is not much different. Because all these software are using Apache, Mysql and PHP. Virtual host is a feature of Apache and not of XAMPP so any software using Apache can configure Virtual host in a similar way, the difference is just another place where to place the httpd.conf file.