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. Right now we’re working with Larabook on homestead. Another time, we may want to work with WordPress on Homestead. 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.

---
ip: "192.168.10.10"
memory: 2048
cpus: 1

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: C:/localdev
      to: /home/vagrant/Code

sites:
    - map: homestead.app
      to: /home/vagrant/Code/Laravel/public
    - map: larabook.dev
      to: /home/vagrant/Code/larabook/public

databases:
    - homestead

variables:
    - key: APP_ENV
      value: local

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.

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost

127.0.0.1       localhost
192.168.10.10   homestead.app
192.168.10.10   larabook.dev

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:

<?php

return [
    'DB_HOST' => 'localhost',
    'DB_NAME' => 'larabook',
    'DB_USERNAME' => 'homestead',
    'DB_PASSWORD' => 'secret'
];

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:


	'driver' => 'smtp',

	/*
	|--------------------------------------------------------------------------
	| SMTP Host Address
	|--------------------------------------------------------------------------
	|
	| Here you may provide the host address of the SMTP server used by your
	| applications. A default option is provided that is compatible with
	| the Mailgun mail service which will provide reliable deliveries.
	|
	*/

	'host' => 'smtp.sendgrid.net',

	/*
	|--------------------------------------------------------------------------
	| SMTP Host Port
	|--------------------------------------------------------------------------
	|
	| This is the SMTP port used by your application to deliver e-mails to
	| users of the application. Like the host we have set this value to
	| stay compatible with the Mailgun e-mail application by default.
	|
	*/

	'port' => 587,

	/*
	|--------------------------------------------------------------------------
	| Global "From" Address
	|--------------------------------------------------------------------------
	|
	| You may wish for all e-mails sent by your application to be sent from
	| the same address. Here, you may specify a name and address that is
	| used globally for all e-mails that are sent by your application.
	|
	*/

	'from' => array('address' => 'admin@larabook.dev', 'name' => 'Larabook Admin'),

	/*
	|--------------------------------------------------------------------------
	| E-Mail Encryption Protocol
	|--------------------------------------------------------------------------
	|
	| Here you may specify the encryption protocol that should be used when
	| the application send e-mail messages. A sensible default using the
	| transport layer security protocol should provide great security.
	|
	*/

	'encryption' => 'tls',

	/*
	|--------------------------------------------------------------------------
	| SMTP Server Username
	|--------------------------------------------------------------------------
	|
	| If your SMTP server requires a username for authentication, you should
	| set it here. This will get used to authenticate with your server on
	| connection. You may also set the "password" value below this one.
	|
	*/

	'username' => 'yourname',

	/*
	|--------------------------------------------------------------------------
	| SMTP Server Password
	|--------------------------------------------------------------------------
	|
	| Here you may set the password required by your SMTP server to send out
	| messages from your application. This will be given to the server on
	| connection so that the application will be able to send messages.
	|
	*/

	'password' => 'yourpassword',

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.