In this article, I am going to explain “How to setup Virtual Host in Xampp server”, “Create Virtual Host” or Setup Virtual host in apache. before setup virtual host you must be aware of XAMPP Server. XAMPP is a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages.
Why we need Virtual Host?
Virtual hosting is a method for hosting multiple domain names on a single server. This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name. Virtual address means IP-based, you can use different IP or different name for each IP.
Read Also: Integrate PayUmoney Payment Gateway
Assume your site resides in http://localhost/client/site1 and when you want to run the site on the browser. Then it’s hard to write complete path manually.
For example: In this case, URL becomes http://localhost/client/site1
and after virtual host implements then URL becomes http://site1.dev.
Using virtual host, you can load assets from the root of your document without any movement of your site files.
Here we need to change in these important files.
httpd-vhosts.conf file located under {xampp folder}\apache\conf\extra
Window Host file located under C:\Windows\System32\drivers\etc
First we need to change apache virtual host configuration file. To create a virtual host for your website, you need to add your code in this file.
1 2 3 4 5 6 7 8 9 10 | <VirtualHost site1.dev> ServerAdmin webmaster@site1.dev DocumentRoot "D:/xampp/htdocs/site1/public" ServerName site1.dev ServerAlias site1.dev ErrorLog "logs/dummy-host2.site1.dev-error.log" CustomLog "logs/dummy-host2.site1.dev-access.log" common </VirtualHost> |
Read Also: Paytm Payment Gateway Integration in Laravel
As you can see in the above code, we have ServerAdmin, DocumentRoot, ServerName, ServerAlias, ErrorLog, CustomLog.
ServerAdmin: Email address that the server includes in error messages sent to the client.
ServerAlias: Alternate names for a host used when matching requests to name-virtual hosts.
ServerName: Hostname and port that the server uses to identify itself.
DocumentRoot: Directory that forms the main document tree visible from the web.
Almost done, as we have made changes in httpd-vhosts.conf files please save it.
Now, it’s time to tell the browser how to handle your new server name. So please open the windows host file. It’s located in this given path C:\Windows\System32\drivers\etc\hosts
Here’s you can add your host, as like given below:
127.0.0.1 site.dev
Like above, you can add as many as your hostname for local websites.
Now, Restart your Apache and visit the site1 using URL http://site1.dev
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.