Skip to content

Instantly share code, notes, and snippets.

@martindevnow
Last active April 11, 2018 14:03
Show Gist options
  • Save martindevnow/e3a0f165fbed1a8ccc8c1cb62329654d to your computer and use it in GitHub Desktop.
Save martindevnow/e3a0f165fbed1a8ccc8c1cb62329654d to your computer and use it in GitHub Desktop.
Setup
<h1 id="vagrant-for-various-versions-of-php">Vagrant for Various Versions of PHP</h1>
<h1 id="requirements">Requirements</h1>
<p>To run Homestead, the following software must be installed on your host machine.</p>
<h2 id="vagrant">Vagrant</h2>
<p>Install vagrant by downloading the latest version (currently: <strong>v2.0.1</strong> as of January 29th 2018)</p>
<ul>
<li><a href="https://www.vagrantup.com/downloads.html">https://www.vagrantup.com/downloads.html</a></li>
</ul>
<h2 id="virtual-box">Virtual Box</h2>
<p>Install virtual box by downloading the latest version (currently: <strong>v5.2.6</strong> as of January 29th 2018)</p>
<ul>
<li><a href="https://www.virtualbox.org/wiki/Downloads">https://www.virtualbox.org/wiki/Downloads</a></li>
</ul>
<h3 id="notes">* Notes</h3>
<p>If you are <strong>unable</strong> to install due to an error in the installer, follow these steps:</p>
<ol>
<li>Unmount the <code>VirtualBox.dmg</code></li>
<li>Open <code>System Preferences &gt; Security &amp; Privacy</code></li>
<li>On the first tab, look at the bottom of the panel</li>
<li>Allow the exception</li>
<li>Remount the <code>VitrualBox.dmg</code></li>
<li>Run the installer</li>
</ol>
<h2 id="phpmyadmin-optional">PHPMyAdmin (Optional)</h2>
<p>If you want to use PHPMyAdmin, simply download the <code>tar</code> file from their website and extract it to a directory similar to listed below in the example <code>Homestead.yaml</code> file</p>
<h1 id="homestead">Homestead</h1>
<p>For simplicity sake and support for Laravel based projects, we will be using the laravel/homestead vagrant box.<br>
To install Homestead first clone the repo to your home directory</p>
<pre class=" language-bash"><code class="prism language-bash"><span class="token function">git</span> clone https://github.com/laravel/homestead.git ~/Homestead
<span class="token function">cd</span> ~/Homestead
<span class="token function">git</span> checkout v7.0.1
<span class="token function">bash</span> init.sh
vagrant up
</code></pre>
<h2 id="homestead-up">Homestead up</h2>
<p>If you get the following error, then please visit the link below.</p>
<pre class=" language-bash"><code class="prism language-bash"><span class="token operator">==</span><span class="token operator">&gt;</span> homestead-7: Waiting <span class="token keyword">for</span> machine to boot. This may take a few minutes<span class="token punctuation">..</span>.
homestead-7: SSH address: 127.0.0.1:2222
homestead-7: SSH username: vagrant
homestead-7: SSH auth method: private key
Timed out <span class="token keyword">while</span> waiting <span class="token keyword">for</span> the machine to boot. This means that
Vagrant was unable to communicate with the guest machine within
the configured <span class="token punctuation">(</span><span class="token string">"config.vm.boot_timeout"</span> value<span class="token punctuation">)</span> <span class="token function">time</span> period.
If you <span class="token function">look</span> above, you should be able to see the error<span class="token punctuation">(</span>s<span class="token punctuation">)</span> that
Vagrant had when attempting to connect to the machine. These errors
are usually good hints as to what may be wrong.
If you<span class="token string">'re using a custom box, make sure that networking is properly
working and you'</span>re able to connect to the machine. It is a common
problem that networking isn't setup properly <span class="token keyword">in</span> 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 <span class="token function">timeout</span> <span class="token punctuation">(</span><span class="token string">"config.vm.boot_timeout"</span><span class="token punctuation">)</span> value.
</code></pre>
<blockquote>
<p><a href="https://stackoverflow.com/questions/41064388/laravel-homestead-vagrant-up-times-out">https://stackoverflow.com/questions/41064388/laravel-homestead-vagrant-up-times-out</a></p>
</blockquote>
<h2 id="macos-shortcut">MacOS Shortcut</h2>
<p>To be able to run commands on your homestead box from any directory in terminal, you can add the following to your <code>~/.bash_profile</code> or <code>~/.bashrc</code> file.</p>
<pre class=" language-php"><code class="prism language-php"><span class="token keyword">function</span> <span class="token function">homestead</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">(</span> cd <span class="token operator">~</span><span class="token operator">/</span>Homestead <span class="token operator">&amp;&amp;</span> vagrant $<span class="token operator">*</span> <span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<h1 id="configuration">Configuration</h1>
<h2 id="homesteads-yaml-file">Homestead’s yaml file</h2>
<p>We need to update our Homestead.yaml file. This is used to provision the environment based on the box we selected.</p>
<pre><code>vi ~/Homestead/Homestead.yaml
</code></pre>
<p>Configure the sections of your Homestead.yaml file according to where you keep your code repositories. For myself, that looks like this:</p>
<pre><code>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
</code></pre>
<h2 id="host-oss-hosts-file">Host OS’s Hosts File</h2>
<p>In order to view these newly created projects as they are hosted by Vagrant, you will need to update your <code>/etc/hosts</code> file on mac</p>
<pre><code>sudo vi /etc/hosts
</code></pre>
<p>And add the following lines to the bottom of this file:</p>
<pre><code>192.168.10.10 phpmyadmin.test
192.168.10.10 reliance.test # these should reflect your Homestead.yaml file
</code></pre>
<h1 id="vagrant-commands">Vagrant Commands</h1>
<p>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.</p>
<p>The following 4 commands should cover you in most cases.</p>
<ul>
<li><code>vagrant global-status</code> shows you a list of existing machines and their state. This ID will be used in subsequent commands.</li>
<li><code>vagrant global-status --prune</code> will clear the cache of the list of active machines (useful if you delete your Homestead directory to reinstall)</li>
<li><code>vagrant up</code> should be run from the folder where the VagrantFile you wish to use is saved. (i.e. <code>~/Homestead</code> is where our VagrantFile is. When you run this command in that folder, it will use the Configuration file specified in the VagrantFile.</li>
<li><code>vagrant up --provision</code> will force VirtualBox to apply any changes that were made to the <code>Homestead.yaml</code> or <code>VagrantFile</code></li>
<li><code>vagrant destroy &lt;machine-id&gt;</code> will remove the machine in case of any peculiar behaviour and allow you to rebuild your box</li>
</ul>
<h2 id="reprovisioning">Reprovisioning</h2>
<p>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</p>
<pre><code>vagrant global-status # take note of the ID
vagrant destroy &lt;ID&gt;
</code></pre>
<p>Then, we can bring our machine back online taking in the changes to the Homestead.yaml</p>
<pre><code>cd ~/Homestead
vagrant up --provision
</code></pre>
<h1 id="conclusion">Conclusion</h1>
<p>You should now be able to visit <code>http://phpmyadmin.test</code> (or <code>http://homestead.test</code> if you didn’t install phpmyadmin) in your browser and see the application.</p>

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