|

Install Larabook On Laravel Homestead

Install Larabook On Laravel Homestead

Ok ladies and gentlemen, you’ll notice we did a lot of work to get familiar with Virtual Box, Vagrant, and Laravel Homestead in the recent episodes. Growing pains aside, this Virtual Machine approach to creating new sites is awesome. Once the initial bootstrapping and setup is out of the way, on subsequent days, the promise of simply typing vagrant up into your terminal is true. Type that one line, and everything just works like magic. Fun stuff indeed. Ok so we have Laravel Homestead working like a charm, what are we going to do with it? Well, the beauty of working with Homestead and Virtual Machines in general, is that you can install as many applications or websites as you like. You might be working on several different projects, and these new toys make it easy to have a local development environment that exactly mimics a real production environment. As the literature at Vagrant’s own site states, Say goodbye to “works on my machine” bugs! With this in mind, why not install a great example of a well architected Laravel Application from the great Jeffrey Way of Laracasts onto our development VM? Sounds like fun, let’s do it.


Clone The Larabook Repository

With vagrant, you can install an application on the local machine *or* the Virtual Machine, the choice is up to you. It doesn’t really matter, since the files and folders get synced bi directionally. In this example, we’ll install the application locally, then when we bring up the virtual machine later, everything will simply sync up. We had created a local folder to house all of our apps in C:/localdev so that is where we’ll go ahead and place the Larabook repository like so:

Chris@CHRIS-PC /c/localdev
$ git clone https://github.com/laracasts/larabook.git larabook

Edit Sites In Homestead.yaml

Now that we have a local repository on our host machine, we can edit the Homestead.yaml file to reflect a new site. We do not need to add a new folder, since we place the larabook folder within our C:/localdev folder. Folder synchronization will take care of all that for us. We do however have to make a new entry under the sites property. Notice that we still have our default Laravel install, and now a second pair of map/to values to account for the larabook app.

Tip: YAML files are notoriously fickle with regard to any odd characters. Make sure you don’t use tabs accidentally, or your virtual machine will choke when you try to launch it. Use only spaces for formatting in the YAML file.


Edit Windows Hosts File

We can add an entry in the Windows hosts file quite easily, simply add a new line after the one we already created for homestead.app. Remember to use notepad.exe as administrator, or you’ll be stuck in the water.


Bring Up The Virtual Machine

If you’ve already done all the legwork, this is as simple as launching Git Bash, then cd into the directory that contains the VM you want to launch (in our case Laravel Homestead), then type vagrant up. If you haven’t already done the legwork to get Homestead running on your machine, you’ll need to read up on how to make this happen, then come back to this post. If you’re good to go, just do this:

Chris@CHRIS-PC /c/homestead
vagrant up

Note: If all goes perfect, everything will launch perfectly and all folders will sync up right away between the host machine and the virtual machine. If you run into troubles, sometimes running vagrant provision will help. You can also vagrant halt, then vagrant up again. If all else fails, you can destroy the Virtual Machine with vagrant destroy and then bring it up fresh again with vagrant up.


Set Configuration Options In The App

We’ll need to set a few config options in the Larabook app. First, in larabook/app/config/app.php we set the url key value to http://larabook.dev. We’ll also want to create database credentials in .env.local.php like so:


Create The Database

On your local machine, you should have some type of application that can connect to the mysql sever on the homestead Virtual Machine. In our case, we used MySQL Workbench to create a new database. Note that MySQL Workbench calls databases, schemas. In MySQL Workbench, you’ll first create a connection. The hostname is 127.0.0.1, the Port to use is 33060, the username is homestead, and the password is secret. You can just as easily apply these credentials to a different tool like phpMyAdmin, SequelPro, or whatever GUI you enjoy. Of course you could also use vagrant ssh to connect to the Virtual Machine from the terminal and interact with the database from the VM terminal if you choose. Here are our connection settings for MySQL Workbench.
laravel homestead mysql workbench

Finally, create the larabook database, and you’re good to go.


SSH into Homestead and Run Migrations

vagrant@homestead:~/Code/larabook$ php artisan migrate
Migration table created successfully.
Migrated: 2014_06_29_203726_create_users_table
Migrated: 2014_07_15_173108_create_statuses_table
Migrated: 2014_07_29_184354_create_follows_table
Migrated: 2014_08_18_182429_create_password_reminders_table
Migrated: 2014_08_29_141342_create_comments_table
vagrant@homestead:~/Code/larabook$

Configure Application To Send Email

Laravel makes it easy as pie to send email, and it works like a champ even from Virtual Machines on your local development environment. In this example we’ll use SMTP with SendGrid. It’s as easy as simply filling out the needed fields in larabook/app/config/mail.php to the values associated with whatever SMTP service you use. Here are the fields to fill out:


Take Larabook For A Test Drive

If you followed all the steps so far, you are ready to test this sucker out! We visit http://larabook.dev in the browser, and there you go. Note we created a user, got a welcome email, posted a couple of statuses, and made a comment or two. Everything works perfect!
larabook installed


What Is The Benefit?

So you might be wondering, what the heck use is it running Larabook on my local development machine. Perhaps you have aspirations of being the next Mark Zuckerburger and were hoping to actually have users sign up to your new creation. Well, actually, I don’t believe that is what this application was intended for. The benefits it does provide however is having a fully functioning, well architected Laravel Application that you can inspect and kick the tires on. Almost all of the source code can be used as a fantastic starting point for a different application you might want to create. Need to handle User Authorization? Flash Messages? Sending Email? Password Resets? Clean Controllers? Social Following and Comments? You got it, it’s all here in this example application. You can pick and choose, mix and match, extend and refactor all day long if you like.

Conclusion

This real world example of installing Larabook on Laravel Homestead has taught us many things. We now know how to bring up virtual machines, add vagrant boxes, ssh into VMs, create a development web appliation, and also learned how to add more than one application to our Laravel Homestead box. Have 10 apps your currently working on? No problem, follow the steps here, and simply wash – rinse – and repeat.