Setup virtualhosts on XAMPP

  • Project 1 is based on PHP and has the local URL address project.one
  • Project 2 is a bit-academy folder with URL php.web
  • localhost is a directorylisting with URL localhost
  • Project 3 is based on PHP and has the local URL address test.one

Edit httpd.conf

Click on Config -> Apache (httpd.conf) Or you find the file here C:\XAMPP\apache\conf\httpd.conf, C:\XAMPP\apache2\conf\httpd.conf or similar location. Make sure that httpd-vhosts.conf is included:

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

Virtual hosts

Note: By default server documents are located on C:\XAMPP\htdocs that’s fine if you have only one project or many small test files. However, if you need to develop many projects then it suggested separating them by the virtual host, as mentioned earlier.

Setting Virtual host

  1. Create a folder for your projects; I have created one in c:\vhost you can call it projects etc.
  2. In c:\vhost folder we create a sub-folder domain1 or project1 or any other, it is up to you (c:\vhost\project1)
  3. Open httpd-vhosts.conf file C:\XAMPP\apache\conf\extra\httpd-vhosts.conf Add the following code in line depending on how many vhost you want to create:
#this is the default address of XAMPP    
<VirtualHost *:80>
    DocumentRoot "C:/XAMPP/htdocs/"
    ServerName localhost
</VirtualHost>

#this is the first vhost address in XAMPP
<VirtualHost *:80>
    DocumentRoot "C:/XAMPP/htdocs/prj_001"
    ServerName project.one
</VirtualHost>

#this is the second vhost address in XAMPP
<VirtualHost *:80>
    DocumentRoot C:/xampp/htdocs/bit-academy/
    ServerName php.web
</VirtualHost>

#this is the first vhost address in test
<Directory C:/test>
    AllowOverride All
    Require all granted
</Directory>
<VirtualHost *:80>
    DocumentRoot C:/test
    ServerName test.one
</VirtualHost>

Save and close the file

Edit Windows Host file

  1. Stop Apache and MySQL services from XAMPP/WAMP.
  2. Open the hosts file in C:\windows\system32\drivers\etc you need Administrator privilege to edit the file.editor.
  3. Edit according to code below, Save and close the file.
127.0.0.1       localhost
127.0.0.1       php.web
127.0.0.1       project.one
127.0.0.1       test.one

Prevent 403 Access forbidden error

To prevent a 403 Access forbidden error when you browse your site, you add this to your httpd.conf file:

<Directory "c:/test">
    AllowOverride none
    Require all granted
</Directory>

Start/Re-start your Apache and MySQL again and test.

localhost
php.web
project.one
test.one


Addition (Suggestion)

Note: Under your development process you might face a problem having a cache on because you can risk updating something in Laravel and it won’t appear in your browser. Therefore you might need to disable PHP cache under the development process in your local environment.

Open file php.ini under PHP folder in your Apache (XAMPP or WAMP) folder and change opcache.enable to 0 so it looks like this:

[opcache]
zend_extension=php_opcache.dll
; Determines if Zend OPCache is enabled
opcache.enable=0

Addition (Alternative solution)

Important: The following solution is not suitable to test Restful Api, canvas, heavy solutions, etc.it might give you some headaches, so I would suggest a virtual host solution with port 80 as default.

It is possible to deploy a temporary Virtual Server without necessarily configuring XAMPP/WAMP Virtual Host, start the CMD console, and run the following PHP command:

php -S localhost:8001 -t c:\vhost\Laravel-Project\public
  • Port 8001 can be changed to any available port number and be sure not to conflict with other software ports.
  • c:\vhost\Laravel... path should be changed to whatever your project path.
  • It is possible to start multiple servers but should have different port numbers.

In your browser, you need only to write

http://localhost:8001/