Installing Laravel With Homestead

in Laravel

To get started first we need to install Laravel.

Installing Laravel

There are multiple ways you can install Laravel, the easiest way you can install Laravel is by using composer.

composer global require "laravel/installer"

Once Laravel is installed it will have a command of laravel new which you can use to create your new project.

laravel new todo-list

This will add a fresh Laravel installation into the directory you're in.

Local Development Server

With the local development setup you can either use your local machine or go for virtual machines. To use PHP built-in development server you can use the Artisan command serve.

php artisan serve

But you will need to make sure that you have all the required PHP modules installed.

  • PHP >= 5.6.4
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

I would recommend you go for the Homestead approach for your local development as this will create a new local environment using Vagrant, this will install all the required PHP modules and setup your Laravel directory permissions for you.

Install Homestead

Before you're able to install Homestead you first need to setup Vagrant, for more information on setting up Vagrant you can view a previous tutorial about adding Vagrant to the workflow.

Development Workflow Vagrant

Homestead will then create a new virtual machine with the following software installed.

  • Ubuntu 16.04
  • Git
  • PHP 7.0
  • Nginx
  • MySQL
  • MariaDB
  • Sqlite3
  • Postgres
  • Composer
  • Node (With PM2, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd

There are two ways you can setup Homestead either globally or per project. If you're planning on changing the configuration of the box then go for the per project config. Else go for the global box as you can easily reuse this on your other Laravel projects.

First add the homestead box to your vagrant installation.

vagrant box add laravel/homestead

You can then clone the Homestead project from github.

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

Install it into a global location so that you can easily reuse this on your other projects. Once Homestead was installed cd to you Homestead directory.

cd Homestead

Then run the bash init.sh command, this will create your Homestead config file Homestead.yaml in your home directory ~/.homestead.

Now you can open this config folder and setup the Homestead box.

Provider

First you need to select the provider for your Homestead box, if you're using Vagrant like above then make sure this setting is set to virtualbox.

provider: virtualbox

Shared Folders

The shared folder is your local folders that will be shared with your Homestead environment.

folders:
    - map: /var/www/todo-list
      to: /home/vagrant/Code

nginx Sites

To setup the sites on your nginx server you will do change the sites: property on the Homestead.yaml. This will the parameters added to Nginx virtual hosts, for the local domain you're going to use and the document root of the location of the files on the virtual machine.

sites:
    - map: todolist.dev
      to: /home/vagrant/Code/Laravel/public

Hosts

With the nginx sites setup we can add them to our hosts files if you're using Linux or Mac then then your hosts file is located /etc/hosts.

192.168.10.10 todolist.dev

Launch The Vagrant Box

To launch the box and see your site running you now need to navigate to your Homestead folder on the command line and run the vagrant up command.

This will boot up your Vagrant box and install your sites from the information in your config file.

You can now navigate to the domain you put in your Homestead.yaml file and you'll be able to see your Laravel site.

Connecting To The Database

To configure the database connection on your local environment you'll need to edit your .env file, the host for your database is relational to your virtual machine so it will be 127.0.0.1.

The username for your Homestead database is homestead and the password is secret.

With your local environment setup we can now start with the Todo list application project.

Site Setup

With the site setup you can navigate to http://todolist.dev and see the default Laravel page located in /resources/views/welcome.blade.php

laravel-starter-page

The first thing you need to to when setting up Laravel is to set your application key to a random string this is used as the salt for any encrypted data or user sessions your application uses.

php artisan key:generate

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To Newsletter

Get weekly updates to your email