बुधवार, 20 मई 2020

How to configure Virtual Host on XAMPP on Windows 10

Many developers start development in their local environment and then go to a staging environment when the work is done. In many instances, the local environment has minimal resources required for the development. Most of the beginners Laravel developers use php artisan serve to serve site but some developers like to create a website in a localized environment. If you are working with Laravel, setting up virtual hosts and free XAMPP/WAMP stack is a straightforward task.

XAMPP/WAMP stack ensures that when the time comes to take the project live, the database(s) could be moved easily without any hassle.


In this tutorial, I am going to demonstrate how you can set up and configure virtual hosts for Laravel websites with XAMPP running on Windows 10.

Create the Virtual Host in Windows10

TL;DR

Step 1: Open host file from C:\WINDOWS\system32\drivers\etc\ and add following :

            127.0.0.1       localhost
            127.0.0.1       laravel.local
        

Step 2: Open xampp\apache\conf\extra\httpd-vhosts.conf and add following line:

            
            DocumentRoot "C:/xampp/htdocs/laravel/public"
            ServerName laravel.local
            ServerAlias www.laravel.local
            
        

Step 3:Restart XAMPP and now run in your browser : laravel.local

  1. First, you need to navigate to C:/xampp/apache/conf/extra or wherever your XAMPP files are located.
  2. Then, edit httpd-vhosts.conf with any text editor. In my case, I am using VSCode.

    The file looks something like this.
  3. Paste the following line at the end of file.
                
                DocumentRoot "C:/xampp/htdocs/laravel/public"
                ServerName laravel.local
                ServerAlias www.laravel.local
                
            
    In the above code :
    VirtualHost:
    Most web servers use port 80 as their defdlt port. However, you can change the port to 8080, 8081, etc.

    DocumentRoot:
    folder where files of a site will exist. In our case, the folder name is “laravel”. Laravel serve from public folder so folder name is here “laravel/public

    ServerName:
    It is the URL for our virtual host.
  4. Now, go to Windows > Search > Run and paste the following line: C:\Windows\System32\drivers\etc\hosts
  5. Next, open Host file in your text editor. File looks something like this.
    Note: You may need administrator access to edit the file
  6. Add the following line at the end of Host file.
                127.0.0.1 localhost
                127.0.0.1 laravel.local
            
  7. Once you are done, check whether laravel.local opens up in your browser. Before this don’t forget to restart server.

You can see that the domain has been added successfully. You are now free to run the application of your choice in the virtual host. In my case, I will opt for Laravel.

Final Words

That’s it! I hope you have a clear idea of setting up Virtual Hosts for Laravel with XAMPP running on Windows 10. If you have any queries or suggestions, feel free to ask me via the comment section below.

Load comments