The term Virtual Host refers to the practice of running more than one website such as
on a single machine. Virtual hosts can be “IP-based”, it means that we have a different IP address for every website and “Name-based”, means that we have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.
How to Setup Virtual Host On Laravel
There are two types of Apache virtual host configurations:
- IP-Based Virtual Host
- Name-based Virtual Host
IP-based Virtual Host:
In this configuration, when you are indicating two websites (with different IP-address) to the server that runs Apache, that physical server should have two different IP-address configured.
This means that the server should have two Ethernet cards, each one of them configured to the IP-address of the corresponding website that Apache virtual host will be serving. But it’s not practical when you are serving a large number of websites.
Recommended Post: 16 Reasons you should start Laravel for your Web Projects
Name-Based Virtual Host:
In this configuration, when Apache web server receives a request, it looks for the host-name in the HTTP header and depending on the host-name, it serves different websites. This is very easy, as you need only one IP-address on that physical server, but, you update the DNS with multiple website names specifying to the same IP-address.
Step By Step on How to Setup the Virtual Host
Step 1 :
To set up the virtual host, we need to configure apache’s virtual host. We need to edit the “httpd-vhosts.conf” file. Follow these steps to set up the virtual server:
Navigate to “C:\wamp\bin\apache\apache2.4.23\conf\extra\httpd-vhosts.conf” [ For XAMPP C:\xampp\apache\conf\extra\httpd-vhosts.conf ] and open the file in editor.
Add these lines to the end of the file and save it:
<VirtualHost *:80> DocumentRoot “C:/xampp/htdocs” ServerName localhost </VirtualHost> <VirtualHost *:80> DocumentRoot “C:/xampp/htdocs/project-name/public” ServerName project-name.dev </VirtualHost>
Also, we need to edit “hosts” file. “C:\Windows\System32\drivers\etc\hosts” Remember This file may need Administrator Privilege to edit.
127.0.0.1 localhost 127.0.0.1 raymkindo.dev (ex: 127.0.0.1 raymkindo.dev)
Now, open WAMPP (XAMPP) and restart Apache server. In your browser, navigate to raymkindo.dev
This is how you can set up virtual host in your xamp or wamp server for laravel.
By Doing this you can run your development website by using Virtual Host name without a Problem and no need to run "Php artisan serve Command" each time.