Skip to content

Instantly share code, notes, and snippets.

@martindevnow
Created April 11, 2018 14:06
Show Gist options
  • Save martindevnow/85963a513ae6b357b9c0cfe093b8d32c to your computer and use it in GitHub Desktop.
Save martindevnow/85963a513ae6b357b9c0cfe093b8d32c to your computer and use it in GitHub Desktop.
Setup

Vagrant for Various Versions of PHP

Requirements

To run Homestead, the following software must be installed on your host machine.

Vagrant

Install vagrant by downloading the latest version (currently: v2.0.1 as of January 29th 2018)

Virtual Box

Install virtual box by downloading the latest version (currently: v5.2.6 as of January 29th 2018)

* Notes

If you are unable to install due to an error in the installer, follow these steps:

  1. Unmount the VirtualBox.dmg
  2. Open System Preferences > Security & Privacy
  3. On the first tab, look at the bottom of the panel
  4. Allow the exception
  5. Remount the VitrualBox.dmg
  6. Run the installer

PHPMyAdmin (Optional)

If you want to use PHPMyAdmin, simply download the tar file from their website and extract it to a directory similar to listed below in the example Homestead.yaml file

Homestead

For simplicity sake and support for Laravel based projects, we will be using the laravel/homestead vagrant box. To install Homestead first clone the repo to your home directory

git clone https://github.com/laravel/homestead.git ~/Homestead
cd ~/Homestead
git checkout v7.0.1
bash init.sh
vagrant up

Homestead up

If you get the following error, then please visit the link below.

==> homestead-7: Waiting for machine to boot. This may take a few minutes...
    homestead-7: SSH address: 127.0.0.1:2222
    homestead-7: SSH username: vagrant
    homestead-7: SSH auth method: private key
Timed out while waiting for the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly
working and you're able to connect to the machine. It is a common
problem that networking isn't setup properly in these boxes.
Verify that authentication configurations are also setup properly,
as well.

If the box appears to be booting properly, you may want to increase
the timeout ("config.vm.boot_timeout") value.

https://stackoverflow.com/questions/41064388/laravel-homestead-vagrant-up-times-out

MacOS Shortcut

To be able to run commands on your homestead box from any directory in terminal, you can add the following to your ~/.bash_profile or ~/.bashrc file.

function homestead() {
    ( cd ~/Homestead && vagrant $* )
}

Configuration

Homestead's yaml file

We need to update our Homestead.yaml file. This is used to provision the environment based on the box we selected.

vi ~/Homestead/Homestead.yaml

Configure the sections of your Homestead.yaml file according to where you keep your code repositories. For myself, that looks like this:

folders:
    - map: ~/Code                # local directory where you keep projects
      to: /home/vagrant/code     # virtual machine

sites:
    - map: phpmyadmin.test
      to: /home/vagrant/code/phpmyadmin
      php: "7.2"

    - map: reliance.test
      to: /home/vagrant/code/reliance
      php: "7.0"
      
databases:
    - reliance

Host OS's Hosts File

In order to view these newly created projects as they are hosted by Vagrant, you will need to update your /etc/hosts file on mac

sudo vi /etc/hosts

And add the following lines to the bottom of this file:

192.168.10.10	phpmyadmin.test
192.168.10.10	reliance.test   # these should reflect your Homestead.yaml file

Vagrant Commands

You will periodically need to make changes to your various Vagrantfiles (or in this case, your Homestead.yaml files). The VagrantFile will pick up these changes for this yaml file when you provision your environment.

The following 4 commands should cover you in most cases.

  • vagrant global-status shows you a list of existing machines and their state. This ID will be used in subsequent commands.
  • vagrant global-status --prune will clear the cache of the list of active machines (useful if you delete your Homestead directory to reinstall)
  • vagrant up should be run from the folder where the VagrantFile you wish to use is saved. (i.e. ~/Homestead is where our VagrantFile is. When you run this command in that folder, it will use the Configuration file specified in the VagrantFile.
  • vagrant up --provision will force VirtualBox to apply any changes that were made to the Homestead.yaml or VagrantFile
  • vagrant destroy <machine-id> will remove the machine in case of any peculiar behaviour and allow you to rebuild your box

Reprovisioning

In the event that you need to change your Homestead.yaml file, you can provision these changes to your environment by following the steps below. First, let's destroy our machine

vagrant global-status # take note of the ID
vagrant destroy <ID>

Then, we can bring our machine back online taking in the changes to the Homestead.yaml

cd ~/Homestead
vagrant up --provision

Conclusion

You should now be able to visit http://phpmyadmin.test (or http://homestead.test if you didn't install phpmyadmin) in your browser and see the application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment