Laravel Homestead

Introduction

Laravel strives to make the entire PHP development experience delightful, including your local development environment. Vagrant provides a simple, elegant way to manage and provision Virtual Machines.

Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, HHVM, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!

Homestead runs on any Windows, Mac, or Linux system, and includes the Nginx web server, PHP 7.0, MySQL, Postgres, Redis, Memcached, Node, and all of the other goodies you need to develop amazing Laravel applications.

Note: If you are using Windows, you may need to enable hardware virtualization (VT-x). It can usually be enabled via your BIOS. If you are using Hyper-V on a UEFI system you may additionally need to disable Hyper-V in order to access VT-x.

Included Software

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

Installation & Setup

First Steps

Before launching your Homestead environment, you must install VirtualBox 5.x or VMWare as well as Vagrant. All of these software packages provide easy-to-use visual installers for all popular operating systems.

To use the VMware provider, you will need to purchase both VMware Fusion / Workstation and the VMware Vagrant plug-in. Though it is not free, VMware can provide faster shared folder performance out of the box.

Installing The Homestead Vagrant Box

Once VirtualBox / VMware and Vagrant have been installed, you should add the laravel/homestead box to your Vagrant installation using the following command in your terminal. It will take a few minutes to download the box, depending on your Internet connection speed:

  1. vagrant box add laravel/homestead

If this command fails, make sure your Vagrant installation is up to date.

Installing Homestead

You may install Homestead by simply cloning the repository. Consider cloning the repository into a Homestead folder within your "home" directory, as the Homestead box will serve as the host to all of your Laravel projects:

  1. cd ~
  2. git clone https://github.com/laravel/homestead.git Homestead

Once you have cloned the Homestead repository, run the bash init.sh command from the Homestead directory to create the Homestead.yaml configuration file. The Homestead.yaml file will be placed in the ~/.homestead hidden directory:

  1. bash init.sh

Configuring Homestead

Setting Your Provider

The provider key in your ~/.homestead/Homestead.yaml file indicates which Vagrant provider should be used: virtualbox, vmware_fusion, or vmware_workstation. You may set this to the provider you prefer:

  1. provider: virtualbox

Configuring Shared Folders

The folders property of the Homestead.yaml file lists all of the folders you wish to share with your Homestead environment. As files within these folders are changed, they will be kept in sync between your local machine and the Homestead environment. You may configure as many shared folders as necessary:

  1. folders:
  2. - map: ~/Code
  3. to: /home/vagrant/Code

To enable NFS, just add a simple flag to your synced folder configuration:

  1. folders:
  2. - map: ~/Code
  3. to: /home/vagrant/Code
  4. type: "nfs"

Configuring Nginx Sites

Not familiar with Nginx? No problem. The sites property allows you to easily map a "domain" to a folder on your Homestead environment. A sample site configuration is included in the Homestead.yaml file. Again, you may add as many sites to your Homestead environment as necessary. Homestead can serve as a convenient, virtualized environment for every Laravel project you are working on:

  1. sites:
  2. - map: homestead.app
  3. to: /home/vagrant/Code/Laravel/public

You can make any Homestead site use HHVM by setting the hhvm option to true:

  1. sites:
  2. - map: homestead.app
  3. to: /home/vagrant/Code/Laravel/public
  4. hhvm: true

If you change the sites property after provisioning the Homestead box, you should re-run vagrant reload —provision to update the Nginx configuration on the virtual machine.

The Hosts File

You must add the "domains" for your Nginx sites to the hosts file on your machine. The hosts file will redirect requests for your Homestead sites into your Homestead machine. On Mac and Linux, this file is located at /etc/hosts. On Windows, it is located at C:\Windows\System32\drivers\etc\hosts. The lines you add to this file will look like the following:

  1. 192.168.10.10 homestead.app

Make sure the IP address listed is the one set in your ~/.homestead/Homestead.yaml file. Once you have added the domain to your hosts file, you can access the site via your web browser:

  1. http://homestead.app

Launching The Vagrant Box

Once you have edited the Homestead.yaml to your liking, run the vagrant up command from your Homestead directory. Vagrant will boot the virtual machine and automatically configure your shared folders and Nginx sites.

To destroy the machine, you may use the vagrant destroy —force command.

Per Project Installation

Instead of installing Homestead globally and sharing the same Homestead box across all of your projects, you may instead configure a Homestead instance for each project you manage. Installing Homestead per project may be beneficial if you wish to ship a Vagrantfile with your project, allowing others working on the project to simply vagrant up.

To install Homestead directly into your project, require it using Composer:

  1. composer require laravel/homestead --dev

Once Homestead has been installed, use the make command to generate the Vagrantfile and Homestead.yaml file in your project root. The make command will automatically configure the sites and folders directives in the Homestead.yaml file.

Mac / Linux:

  1. php vendor/bin/homestead make

Windows:

vendor\bin\homestead make

Next, run the vagrant up command in your terminal and access your project at http://homestead.app in your browser. Remember, you will still need to add an /etc/hosts file entry for homestead.app or the domain of your choice.

Installing MariaDB

If you prefer to use MariaDB instead of MySQL, you may add the mariadb option to your Homestead.yaml file. This option will remove MySQL and install MariaDB. MariaDB serves as a drop-in replacement for MySQL so you should still use the mysql database driver in your application's database configuration:

  1. box: laravel/homestead
  2. ip: "192.168.20.20"
  3. memory: 2048
  4. cpus: 4
  5. provider: virtualbox
  6. mariadb: true

Daily Usage

Accessing Homestead Globally

Sometimes you may want to vagrant up your Homestead machine from anywhere on your filesystem. You can do this by adding a simple Bash alias to your Bash profile. This alias will allow you to run any Vagrant command from anywhere on your system and will automatically point that command to your Homestead installation:

  1. alias homestead='function __homestead() { (cd ~/Homestead && vagrant $*); unset -f __homestead; }; __homestead'

Make sure to tweak the ~/Homestead path in the alias to the location of your actual Homestead installation. Once the alias is installed, you may run commands like homestead up or homestead ssh from anywhere on your system.

Connecting Via SSH

You can SSH into your virtual machine by issuing the vagrant ssh terminal command from your Homestead directory.

But, since you will probably need to SSH into your Homestead machine frequently, consider adding the "alias" described above to your host machine to quickly SSH into the Homestead box.

Connecting To Databases

A homestead database is configured for both MySQL and Postgres out of the box. For even more convenience, Laravel's .env file configures the framework to use this database out of the box.

To connect to your MySQL or Postgres database from your host machine via Navicat or Sequel Pro, you should connect to 127.0.0.1 and port 33060 (MySQL) or 54320 (Postgres). The username and password for both databases is homestead / secret.

Note: You should only use these non-standard ports when connecting to the databases from your host machine. You will use the default 3306 and 5432 ports in your Laravel database configuration file since Laravel is running within the virtual machine.

Adding Additional Sites

Once your Homestead environment is provisioned and running, you may want to add additional Nginx sites for your Laravel applications. You can run as many Laravel installations as you wish on a single Homestead environment. To add an additional site, simply add the site to your ~/.homestead/Homestead.yaml file and then run the vagrant provision terminal command from your Homestead directory.

Configuring Cron Schedules

Laravel provides a convenient way to schedule Cron jobs by scheduling a single schedule:run Artisan command to be run every minute. The schedule:run command will examine the job scheduled defined in your App\Console\Kernel class to determine which jobs should be run.

If you would like the schedule:run command to be run for a Homestead site, you may set the schedule option to true when defining the site:

  1. sites:
  2. - map: homestead.app
  3. to: /home/vagrant/Code/Laravel/public
  4. schedule: true

The Cron job for the site will be defined in the /etc/cron.d folder of the virtual machine.

Ports

By default, the following ports are forwarded to your Homestead environment:

  • SSH: 2222 → Forwards To 22
  • HTTP: 8000 → Forwards To 80
  • HTTPS: 44300 → Forwards To 443
  • MySQL: 33060 → Forwards To 3306
  • Postgres: 54320 → Forwards To 5432

Forwarding Additional Ports

If you wish, you may forward additional ports to the Vagrant box, as well as specify their protocol:

  1. ports:
  2. - send: 93000
  3. to: 9300
  4. - send: 7777
  5. to: 777
  6. protocol: udp

Network Interfaces

The networks property of the Homestead.yaml configures network interfaces for your Homestead environment. You may configure as many interfaces as necessary:

  1. networks:
  2. - type: "private_network"
  3. ip: "192.168.10.20"

To enable a bridged interface, configure a bridge setting and change the network type to public_network:

  1. networks:
  2. - type: "public_network"
  3. ip: "192.168.10.20"
  4. bridge: "en1: Wi-Fi (AirPort)"

To enable DHCP, just remove the ip option from your configuration:

  1. networks:
  2. - type: "public_network"
  3. bridge: "en1: Wi-Fi (AirPort)"