Creating Virtual Host on Linux Machine for your Laravel Applications — The Mighty Guide

Vipul kumar basapati
Mar 23 · 3 min read

I have been doing Javascript programming for last 2 months, and haven’t worked on any Laravel application in that time. And when, on this weekend, I tried installing a laravel application, I forgot how to create the virtual host file for it. Because for every application, I like to use a domain, and NOT localhost:port. And I will tell you why we need to do make a domain for your applications.

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.

Virtual Host

It is just a symlink to your application which can be accessed via your link, in this case, test.site .

You can find these files inside your /etc/apache2/sites-available folder.

For example let’s create a virtual host file for a laravel application kept in /var/www/html/test

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., test.site.conf .

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 admin@domain.com

ServerName and ServerAlias — It needs to be the url you want, i.e., test.site

You can replace the project with the name of the application i.e., test .

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

127.0.0.1 test.site

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.

Conclusion

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.

Happy learning….

Vipul kumar basapati

Written by

Tech Enthusiast, learner by birth. Workout with @laravel and @react and @node

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade