October 29, 2014

First introduction into Laravel 4 PHP Homestead, getting started from scratch

Update: This post is now deprecated with the release of Homestead 2.0, which runs Laravel 5. A new post is here, and this one will be kept for historical reference.



I've been enjoying using the Laravel PHP framework over the past year or so, but I always found myself referring back to documentation when I start up a new local project. There are always changed dependencies, and lots of other miscellaneous changes, and I would need to update my Vagrantfile and provisioner software accordingly. This Laravel-Vagrant blog post by culttt was a staple of information for me for a long time, but as of the time of this posting, has been deprecated for a while. Luckily for us, there is a new kid on the block, and this one is officially supported and documented!

Enter Homestead, a pre-packaged Vagrant box with all the dependencies provisioned for, to run a new Laravel project. This was a breeze to get going even on my Windows machine (and I prefer using Cygwin with a Console2 shell as well, which usually requires extra effort finding dependencies than Ubuntu's apt-get or OSX's homebrew), and everything simply worked. Just be sure to have the latest version of Vagrant and Virtualbox installed on your computer!

Let's add the Homestead Virtualbox:

vagrant box add laravel/homestead

If you have an older version of Vagrant, you will likely get the error below:

This command was not invoked properly. The help for this command is
available below.

Usage: vagrant box add <name> <url> [--provider provider] [-h]

        --checksum VALUE             Checksum
        --checksum-type VALUE        Checksum type
    -c, --clean                      Remove old temporary download if it exists.
    -f, --force                      Overwrite an existing box if it exists.
        --insecure                   If set, SSL certs will not be validated.
        --cacert certfile            CA certificate
        --cert certfile              The client SSL cert
        --provider provider          The provider that backs the box.
    -h, --help                       Print this help

To fix it, I simply upgraded from the failing version 1.4.3, to the most up-to-date version 1.6.3 as of this writing. Just download the newest installer for Vagrant and set it up, else via Unix command line set it up for a global install as below (substituting for whatever version is most current for you), and then re-run the box add command from before:

wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.6.3_x86_64.deb
sudo dpkg -i vagrant_1.6.3_x86_64.deb

Continue by cloning the official Homestead repository into your folder of choice:

git clone https://github.com/laravel/homestead.git Homestead

Now, in the Homestead.yaml file, you need to set your SSH key info. Chances are, if you've ever used SSH everything is already good to go (it comes with some sane defaults), but if not, check out the documentation on how to do that. Folder mapping is assigned to nginx here as well. If you already have a Laravel project you wish to use, you can just tweak the default folders map, as cloned from the repo, but since I am starting from scratch, I had to do this:

folders:
    - map: ../Homestead
      to: /home/vagrant/Homestead
sites:
    - map: homestead.app
      to: /home/vagrant/Homestead/laravelAuth/public

I'll be making a new Laravel authentication app once this is all set up, so I'm gonna call my project laravelAuth, and preemptively set up the public sites map for it, as done above. Note that you can map several different apps in this development environment.

From here, it is simply vagrant up, and in a short while, you'll have a working Laravel dev environment! In the Homestead.yaml file, our box's ip address is set as 192.168.10.10 by default, so opening http://192.168.10.10/ in your browser should yield... an error? "No input file specified." ...what?

That is because we haven't actually created our Laravel project, so there is nothing for this development environment to show yet! This is easily remedied by entering your box via vagrant ssh, and running:

cd /vagrant
composer create-project laravel/laravel laravelAuth --prefer-dist

Reload your page, and now you have arrived at a brand spanking new Laravel home page. Code away!

No comments :

Post a Comment