Hello, it’s been quite a long time since the last time I post in Medium. A couple of weeks back I moved to a new company and here we use Laravel for developing web applications. The consequence, of course, I need to familiarize myself with the environment. And this (hopefully going to be) series aim to be just that, the journey of me familiarizing myself with the environments, learning one thing or two along the way, and hopefully being quite a bit better as a developer.
Okay, let’s start.
Since I’m learning Laravel in my Windows machine, I have several options for me get my machine ready and set up Laravel. I could just install PHP with Composer in my machine and just install Laravel CLI and create a new project (to be honest I don’t know what’s wrong with that approach). But looking around there are two main ways that people do to prepare Their machine:
- Using Laravel Homestead (using Vagrant and Virtual Box)
- Using *AMPP stack (like XAMPP, WAMPP, LAMPP, etc.)
And after reading Bruno Skvorc’s post on Site Point about the right way to start a PHP project, I am sold to using Laravel Homestead Improved (like Laravel Homestead, but with added features, thus the name Improved) for my path to learning. Btw I’m following Site Points tutorial which can be found here.
Words of warning from Laravel Homestead Page: If you are using Windows, you may need to enable hardware virtualization (VT-x). It can usually be enabled via your BIOS. If you are using Hyper-V on a UEFI system you may additionally need to disable Hyper-V in order to access VT-x.
Preparing My Computer
Actually, there are not much that’s needed to be done. You need to install several things first.
- Virtualbox, because we need the tools to host our VM
- Vagrant, because we’ll be using Laravel Homestead, which is a Vagrant Box.
- Git, I personally use Cygwin which also includes many more Linux commands beside git inside it.
There are many articles out there that covers installing those requirements, so I won’t elaborate it here.
I personally use Hyper as my terminal, You could use git bash which comes with Git for Windows, and it should be just fine.
Cloning the Repo
For every project that we’ll make, we need to clone the Laravel Homestead Improved repo and rename the folder to our project name. In short, you simply use these following command to clone the repo.
git clone https://github.com/swader/homestead_improved learn_laravel
Of course, change
learn_laravel to your project name. After you finished cloning, go to your project folder, and create a public folder.
mkdir -p Project/public
in case you’re wondering just as I am the first time seeing the
-pdirective, it stands for parents. That is, it creates both the parent folder
Projectand its child
Publicif the parent folder didn’t exist.
Why do we need to create the public folder? It is to ease the workflow of working with a VM. With the public folder, You can edit the public folder in your host OS and the changes will be reflected in your VM. Neat right?
But before we can use the public folder, we need to link the folder in our host OS to the folder inside our VM. Fear not, because Laravel Homestead Improved has a script to help us do the work. Just execute the following line in your bash (or bash-like) terminal:
And, Poof! It will the Public folder so You can use it as I said before. If You want to link another folder, you can do so manually later from
homestead.yaml. And also Laravel documentation got You covered for configuring shared folder. Next we need to setup our project Sites
So what we’ll do exactly in this part?
We want to develop a website or web apps right? And how do we usually access those? by accessing its address. For example, if we want to use Gmail we will go to
mail.google.com. So in this step, we will make it so that to access our sites or apps we could access it like we’re accessing Gmail.
To do so, there are two things that we need to configure:
- Host file: so our browser knows how to redirect the address
- homestead.yaml: to tell our application exactly to which folder will your website be
Before proceeding please make sure that you don’t have VPN or DNSCrypt running in your machine because it will affect how your browser interprets the sites. Because when writing this blog I encounter a headache inducing problem just because I use DNSCrypt.
Configuring Host file
I just found out about how cool this file is when I learned Laravel. Just imagine, you can redirect any site to any IP address. I bet this file played a big role in blocking my school computer from accessing Friendster in the past. In Windows you can find this file in this folder:
To open and edit the file you need to open notepad as administrator then open the file from inside notepad. I haven’t found any other way to do it besides that. So when you open the file you will be greeted with a bunch of comments and a pair of IP Address and hostname. In short, more or less this will be what you see:
Now what we need to do is add our desired hostname into our host file, into the host file. In this case, I will add
192.168.10.10 learnlaravel.app to the bottom of the file. By adding this file we’re telling computer to go to 192.168.10.10 if we access learnlaravel.app.
Configuring homestead.yaml File
homestead.yaml file contains Laravel Homestead configurations. For starters what we need to configure only the part where it said
sites:. Now what we need to do is telling this file that we want to go to
/home/vagrant/Code/Project/public when we open our site ( in my case
learnlaravel.app ). To do so we need to add the following code inside our
- map: learnlaravel.app
After all that now we’re ready to fire up our Vagrant box.
Fire up The Box!
We will at last use vagrant commands, to start Laravel Homestead all we need to do is call
in the root folder of our project. It will take quite a bit time the first time you run it because it will download the box needed to run Homestead. But when you do it the second time it should take a shorter time, since you don’t need to download the box. After it finishes, you can access the box using the command
after you’re connected you can use it as you use normal ubuntu terminal. But with some functionalities already prepared for you.
Laravel Homestead use
homestead.yamlto provision (more or less another way of saying setting up the system for the first time) the Vagrant box. So if you need to change the
homestead.yamlafter creating the vagrant box using
vagrant upyou need to reprovision the box by using
vagrant reload --provision. Or else the changes won’t be reflected on your VM.
The Hello World Part?
After all those works we haven’t been able to see the result right? now let’s add something that we can see for sure. In your host machine (not the vagrant box) go to
Project/public folder and create a file named
index.php . And write this inside:
<?php echo '<p>Hello World</p>'; ?>
now when you go to
learnlaravel.app you will see the following
LOL, unfortunately, I don’t know what went wrong and still using localhost:8000 to access my site which works like charm.
So this problem conclude the first part of this series. Hopefully I could find the fixes fast :)
Congratulation on setting up your first Laravel Homestead environment! :)
I know it’s not as easy as the *AMPP solution which only requires you to click through the installation steps. Yet it feels more hacker-y doing it this way right? Besides by trying to set up Laravel Homestead personally, I learned a lot of things about VM and networking, and more when I wrote this article. Hope this will help others and hit me up in the comment if You get anything to ask, I’ll be happy to help.