What Is Laravel?
Laravel is a new framework that has taken the PHP community by storm. Mostly developed by Taylor Otwell, Laravel follows a slightly different approach to PHP development. Using ideas usually found in the .NET environment, such as Facades, and sporting an Inversion of Control Container, Laravel claims to be the future of PHP. Whether or not this is the case is too early to tell, but it certainly is popular.
Laravel is built upon Symfony components and certainly has a lot of really interesting functionality. It’s built upon the S.O.L.I.D principles and does a lot out of the box – although every Laravel application does require quite a rigorous setup procedure. This article intends to ease that pain a little.
What Is Homestead?
PHP development is tricky, to say the least. There are traps and tricks and always more than one way to do something. Code Deployment has its own headaches. Often you need to develop on Windows, test on your project manager’s Macbook and deploy to a Linux server or you could be part of a very large team where some people prefer Windows, others Ubuntu, and more OSX. How would you be sure that in all these cases every developer or tester is getting exactly the same results? Enter Vagrant.
Vagrant is a pre-packaged virtual machine that runs in virtualbox. The beauty of Vagrant is that all setup is defined in one file, called a vagrant file. If you share your code with others, you simply give them the same vagrant file. They update, and instantly everyone has exactly the same environment. If one developer realizes that something in the platform needs to change, he updates his vagrant file and instantly everyone has the same update.
Vagrant takes the hassle of platform awareness away. You decide what the final platform will be, and setup your vagrant box accordingly, then stop worrying about it.
How Does Homestead Differ From Vagrant?
To make sure Laravel development could be as hassle free as possible, the Laravel team took Vagrant a step further by building a vagrant box with Laravel and pretty much everything else you would need to run Laravel pre-installed. This is called Homestead.
Homestead includes Ubuntu 14.04, PHP 5.6, HHVM, Nginx, MySQL, Postgres, Node, Redis, Memcached, Beanstalkd, Laravel Envoy, Fabric and Hipchat Extention.
The idea is you install Homestead and start building your project with as little setup and distractions as possible. We start by installing vagrant.
Getting Homestead Setup
To be sure vagrant is correctly installed you can open your favourite command line or terminal app and type:
$ vagrant –v
The result should be something like:
$ Vagrant 1.6.5
Now that vagrant is installed we can install homestead. In your terminal type:
$ vagrant box add Laravel/homestead
This will trigger quite a few downloads and commands, and eventually will tell you via your terminal app that it’s complete.
Now you need to clone the Laravel Repository and put this apart from your usual projects. As you will see shortly, Homestead will map your local code to the code on your Homestead app without you needing to do anything. You write your code as usual, and everything in your homestead app updates accordingly.
Open the homestead.yaml file. This is the main config file for your homestead app and will be the only file you are ever going to edit directly within Homestead.
The first few lines set your default IP, memory limit and CPU allowance. Going a bit further down, you’ll find an “authorize” key word. This is the setup of your public key. If you are on a mac, it might look like this:
If you don’t have an ssh key, you’ll need to get one. It’s really simple, you type the following in your terminal app:
Ssh-keygen –t rsa –C "[email protected]"
Next, you have to set the path to your computer’s private key. This looks like:
keys: - /Users/[Username]/.ssh/id_rsa.pub
Next, you’ll need to tell Homestead where your projects folder is.
Under folders, the first line is the location of your code on your laptop. The second line is the location of the duplicated code on the homestead machine:
folders: - map: /Users/[Username]/Development/Projects // Or wherever your code is to: /home/vagrant/projects
Now we need to tell homestead where our site is. We do this by mapping under the “sites” line:
sites: - map: laravel.dev to: /home/vagrant/projects/laravel.dev/public
As expected, we need to tell our machines what to do when they hit these urls. We do this, of course, by updating our host files.
On Mac, your host file is located at: /etc/hosts.
On Windows C:WindowsSystem32driversetchosts.
Update this file with the following line of code:
This tells your computer that your site is located at the ip 127.0.0.1, which is your homestead VM.
Navigate your terminal app into your homestead folder, and then run the following command:
Once vagrant is done with its tricks, you will be able to view your Laravel installation at laravel.dev:8000
Need to ssh into your homestead app? Simple! Simply type:
Homestead is great if you love Laravel, and it turns out it’s also great if you don’t. I have run a few frameworks in a Homestead VM without any problems. The only thing I’m not 100% sold on is that Homestead ships with Nginx instead of Apache. Call me old fashioned, because I probably am. And it’s not that hard to get Apache installed once you have SSH’d in anyway, but I would have loved an option in the vagrant file. Other than that, Homestead rocks.