Laravel is a PHP framework that can be used to easily add basic functions and features to a web site. This will take the work of “web site plumbing” off your shoulders and let you focus on the content.
Laravel 4 is focused on using Composer as the setup and installation. But first a development environment needs to be setup. If you are already using Composer and have a web server ready to go, I would suggest using what you already have. Otherwise, Laravel has a pre-packaged setup called Homestead. Homestead is a setup that uses Vagrant and Virtual box to make setup and development easy. A big advantage of this method is that every time it’s setup it’s the same. Homestead contains a set of scripts that once you become familiar with Vagrant other packages can be added automatically. While I’m installing this on a Windows 8 machine, the steps are pretty much the same if using OSX or Linux.
1. Virtual Box and Vagrant
The two packages that will be installed first is Vagrant and Virutalbox. Vagrant will create the virtual machines (VMs) that VirtualBox is the application that will contain.
2. Setting up a Virtual Box
Create a box using Vagrant for Homestead to be placed in.
Vagrant box add laravel/homestead
3. Git Laravel
Laravel is available on GitHub. Since this is used for all the Laravel projects, it’s best to put this in a central space. I put my in a directory with my other Virtual Work machines. Another idea would be to put it close to the projects that you’ll be creating. $DIR would be replaced with what directory the files will be placed, I used Homestead.
git clone https://github.com/laravel/homestead.git $DIR
Bruno Skvorc put up his own version of Homestead, it does not use the SSH that the official uses.
git clone https://github.com/Swader/homestead_improved $DIR
4. SSH key Pair
The SSH key pair is used to make it easier to access the Virtual machine without using passwords. Important step here is to remember where the files are.
The default keys for me were in
C:\Users\Russell\.ssh; Russell would be replaced with your user name. If you leave it as the default name and location, the setup goes easier.
ssh-keygen -t rsa -C "firstname.lastname@example.org"
This step can be skipped if using Bruno’s version.
5. Configure Homestead.yaml
Thanks to using Nginx, the site setup is easy to use. Remember the first
/ means root directory. Note: Don’t use tabs, use spaces
The sites section allows you to use a site name and Nginx will allow you to have multiple apps using the same Homestead package.
sites: - map: myapp.app to: /home/vagrant/Code/myapp/public - map: myapp2.app to: /home/vagrant/Code/myapp2/public
If at a later time other sites needed to be added, edit the file again and while the VM is up run the command
This will add the new sites. Don’t forget to add the new sites to the host file.
The folders section lets you sync multiple folders inside the VM to the outside. Pay close attention here, the folders on the hosts side will be where you work in your projects.
The first part
map: will be the folder in the host machine that will contain the Web Site. The
to: is pointing to inside the VM. Notice that the folder Code is at the end of the to:section.
All folders below will be synced between the two folders.
folders: - map: /dev/Laravel to: /home/vagrant/Code
The authorize and keys section list where the to SSH Files are located. The
~ represents you home directory. For windows this will normally be
authorize: ~/.ssh/id_rsa.pub keys: - ~/.ssh/id_rsa
6. Configure hosts file
This step will allow to easily browse the sites from outside of the Virtual Machine. Rackspace has a nice list of instructions for changing the hosts file. They cover Windows, Linux and OSX.
7. Log into the Virtual Machine
Starting the virtual machine, use the vagrant command
While you don’t need to log into the virtual machine while developing, there are many things that are easier to do from within the VM.
8. Setting up the Database
Homestead comes preloaded with both MySQL and Postgres. From the outside the VM, many applications can be used to access either database. Both databases use homestead and secret as the username and password.
The ports are as follows:
SSH: 2222 -> Forwards To 22 HTTP: 8000 -> Forwards To 80 MySQL: 33060 -> Forwards To 3306 Postgres: 54320 -> Forwards To 5432
9. The site
After going into the VM using the SSH, make your way to the folder for the site. Using the example from step 5, it will be
/home/vagrant/Code. Create the Lavarel site with
composer create-project laravel/laravel your-project-name --prefer-dist
your-project-name will be the project’s and folder’s name. At this point the site should load with a welcome page. Each project you start will need to be created using this command.