Let’s work together

Feel free to send me an email at [email protected]

Research Campus 18
3500 Hasselt
Belgium

Running multiple Homestead boxes next to each other (PHP 7 and PHP 5.6)


Running multiple Homestead boxes next to each other (PHP 7 and PHP 5.6)

Now that PHP 7 is being adopted by more and more web applications in production environments, dealing with legacy code in local development becomes increasingly more difficult because of the different PHP versions out there and deprecated functionality. The reason I’m writing this article is that for a big project, we’re planning a migration from PHP 5.6.15 to PHP 7.x, but had some issues with the legacy mcrypt extension (which we adapted from Laravel 4.2) in local development.

The mcrypt extension has been abandonware for nearly a decade now, and was also fairly complex to use. It has therefore been deprecated in favour of OpenSSL, where it will be removed from the core and into PECL in PHP 7.2.

I’ve been using Laravel homestead for years now because it’s such an easy tool for Laravel development. But it’s not that easy to setup a 2nd Laravel box, with an older PHP version. In this blog I’ll show you how to do it. Hopefully it will help other developers.

I’m assuming you already have a Homestead box up & running. Make a backup of your Homestead.yaml configs and save it someplace safe, because we’re going to start all over just to make sure. All your code is hosted locally anyway so you’re not going to lose anything, unless you made custom changes to the VM which is not a good idea anyway.

Remove all boxes:

vagrant global-status --prune
 
id       name        provider   state   directory
----------------------------------------------------------------------------
2d2e481  homestead56 virtualbox running /Users/Chris/Homestead56
1042da7  homestead7  virtualbox running /Users/Chris/Homestead
 
vagrant destroy [id]

Now let’s setup our new VM’s:

vagrant box add laravel/homestead
vagrant box add laravel/homestead --box-version 0.3.3

Pull in Homestead:

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

Here’s the tricky part, edit ~/Homestead56/init.sh, change the homesteadRoot variable. Otherwise it will try to overwrite the file in the other Homestead folder.

#!/usr/bin/env bash
 
homesteadRoot=~/.homestead56
 
mkdir -p "$homesteadRoot"
 
cp -i src/stubs/Homestead.yaml "$homesteadRoot/Homestead.yaml"
cp -i src/stubs/after.sh "$homesteadRoot/after.sh"
cp -i src/stubs/aliases "$homesteadRoot/aliases"
 
echo "Homestead initialized!"

Next run:

bash Homestead/init.sh
bash Homestead56/init.sh

This will publish the config files to ~/.homestead/ and ~/.homestead56/.
In order for both environments to function properly we’ll have to make a couple of changes to the Homestead.yaml files.

~/.homestead/Homestead.yaml:

--
ip: "192.168.10.10"
name: "homestead7"
memory: 2048
cpus: 1
provider: virtualbox
 
authorize: ~/.ssh/id_rsa.pub
 
keys:
    - ~/.ssh/id_rsa
 
folders:
    - map: ~/Sites
      to: /home/vagrant/Sites
 
sites:
    - map: randomapp.app
      to: /home/vagrant/Sites/randomapp/public
 
databases:
    - homestead

~/.homestead56/Homestead.yaml:

---
ip: "192.168.10.11"
name: "homestead56"
version: 0.3.3
memory: 2048
cpus: 1
provider: virtualbox
 
authorize: ~/.ssh/id_rsa.pub
 
keys:
    - ~/.ssh/id_rsa
 
folders:
    - map: ~/Sites
      to: /home/vagrant/Sites
 
sites:
    - map: randomapp.app
      to: /home/vagrant/Sites/randomapp/public
 
databases:
    - homestead

As you can see we’re setting up different IP’s for the VM’s (duh), specifying an easy-to-remember name, and most important of all, we’re setting the VM versions. These define what PHP version will be installed.

Bonus, symlink your vagrant commands to your VM environments.

vim ~/.bash_profile

# Homestead
function homestead() {
    ( cd ~/Homestead && vagrant $* )
}
function homestead7() {
    ( cd ~/Homestead && vagrant $* )
}
function homestead56() {
    ( cd ~/Homestead56 && vagrant $* )
}

Now you can do something like:

homestead56 up
homestead56 ssh
 
homestead7 up
homestead7 ssh

If you want more information on the available PHP / Homestead versions, try these links:
https://laravel.com/docs/5.4/homestead#old-versions
https://laravel-news.com/using-older-versions-of-homestead