August 6, 2016 by movefirstblog
When creating a new Laravel project you may want to create a custom URL to allow you to test your new site.
One method by which we can achieve this is to create an Apache VirtualHost.
For example, we may wish that our new site to appear when we enter
mysite.dev in the browser.
We can achieve this by:
Step 1 – Edit your hosts file.
As a Windows users I’ll give some details of finding your hosts file. If you use any other OS then I’m no expert so Google is your friend!
On a Windows machine you host file should be located at c:\windows\system32\drivers\etc\hosts.
You can open this file in Windows in any text editor run as Administrator.
(For some more information on the hosts file on Windows take a look at http://www.makeuseof.com/tag/6-surprising-uses-for-the-windows-hosts-file/).
Add a new line to the hosts file that will redirect browser requests for a particular URL to your local machine.
will ensure that any requests for the URL mysite.dev first looks on your local machine.
Step 2 – Stop your Apache server
To allow the changes you’ll make to take effect we need to stop the Apache server.
In my installation, using XAMPP, we can do this from the XAMPP control panel.
Step 3 – Edit your httpd-vhosts-conf file
To create your new VirtualHost you will need to edit you httpd-vhosts.conf file.
On a Windows system you will find this in your Apache folder. (The location of your Apache folder will vary depending on where it was installed. On my system, which is using XAMPP it’s located at D:\XAMPP\Apache\
Within your Apache folder the file is located within the \conf\extra folder. (So my full path is D:\XAMPP\Apache\conf\extra).
Step 4 – Create your new VirtualHost settings in the httpd-vhosts-conf file
By default (at least on XAMPP) virtual hosting is turned off.
If you look near the top of the httpd-vhosts-conf file you should see a section that looks like the following:
# # Use name-based virtual hosting. # ##NameVirtualHost *:80
We can turn on virtual hosts by removing the two hash symbols before NameVirtualHost *:80 so that it looks like this:
# # Use name-based virtual hosting. # NameVirtualHost *:80
The next step is to actually add our new virtual host configuration.
We do this by adding the following (probably to the bottom of the current file):
Options Indexes FollowSymLinks Includes ExecCGI
Allow from all
In the above code the things to note, that will change for your installation are:
mysite.dev – this is the “name” of your new virtual host and will be the URL that you enter into your browser to access your site. Note that this appears three times in the code.
D:/xampp/htdocs/mysite/public – this is the directory path to your new site. Note that this appears twice in the code.
Step 5 – Restart your Apache server
With all your changes made we now need to restart the Apache server.
Again, in my installation, using XAMPP, we can do this from the XAMPP control panel.
Note: the steps I’ve given here a simply an aide-mémoir – for more details about what’s actually going on here take a look at the Laravel documentation or search online for Laravel VirtualHost.