Creating Virtual Host on Linux Machine for your Laravel Applications — The Mighty Guide
Why do we need a domain?
As good as the
php artisan serve command is, it is not going to be responsible for running your application in production mode.
If you have a virtual host setup for your application, let’s say
test.site, it will tell your apache server in your machine to act as your server for your application, and you now don’t need to run the serve command and your application is still running.
And the serve command will try to run your application with it’s own settings, and when you move to a server, you will end up with many requirements that you need in order to host your application, and you wouldn’t know about that until you are actually installing your application on a real server.
So, having a real server on your side and running your application on it, gives you a heads up, on what would you need in order to run your application on a real server.
Let’s start with creating a virtual host file.
It is just a symlink to your application which can be accessed via your link, in this case,
You can find these files inside your
For example let’s create a virtual host file for a laravel application kept in
First go to,
/etc/apache2/sites-available/ , copy
000-default.conf file or create a new file at that location with the name of the domain you want, i.e.,
Now, for the content of this file, I have created a gist, from which you can copy the content and paste in the file. Now you can replace the things as you desire.
ServerAdmin — It can be any email, I keep
ServerAlias — It needs to be the url you want, i.e.,
You can replace the
project with the name of the application i.e.,
Now you can save the file.
Now you need to tell apache that this conf file ready to be served, so you need to run this command:
sudo a2ensite test.site.conf
sudo service apache2 restart
a2ensite generally comes with apache installation. But you can install it externally as well.
And also you need to check if rewrites are enabled on the server. This is not the default setting, so you need to enable it.
You can enable the setting by running below command:
sudo a2enmod rewrite
sudo service apache2 restart
This rewrite module will help run the links on your server. By that I mean, when you don’t run above command, other than the homepage, no other link in your application will work and you will get this error.
When you enable the rewrite module, it checks the
.htaccess file and run all the links.
Now, there is just one step remaining, you need to add an entry in
/etc/hosts file like below
This will tell the server, if any request comes from
test.site you need to find its settings in conf file(if you find any) and run the appropriate application.
That’s it, now you can create a virtual host file of your own and run your applications like the way any server would do it.
Making a virtual host for you laravel applications can be really good for you in the long run. You can easily install your application on any apache server.
You now don’t need to run
php artisan serve . Your application will directly be running and now you have some magic to show to your colleagues.