DEV Community

aurel kurtula
aurel kurtula

Posted on

using virtual box laravel's homestead box

Whilst trying to sep up Virtual Box, Vagrant and Scotch Box I found Scotch Box too heavy for my needs. It offers more then I need and seemed to encourage the use of one box per project - though there were ways around it.

So I have been trying out a different box called homestead, created by Laravel. It offers all the requirements to working with PHP - which is all I really wanted.

Get started

First we need to install the box

vagrant box add laravel/homestead
Enter fullscreen mode Exit fullscreen mode

We would do that only one time, regardless of how many homestead boxes you'll want to initiate

Now go to any directory you'd like to install the initial homestead folder and clone homestead

git clone https://github.com/laravel/homestead.git Homestead
cd Homestead
bash init.sh
Enter fullscreen mode Exit fullscreen mode

The last line will create a Homestead.yaml configuration file within the Homestead folder. This is where you setup all your projects. Inside Homestead.yaml we have something like this

--------
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/code
      to: /home/vagrant/code

sites:
    - map: homestead.test
      to: /home/vagrant/code/public

databases:
    - homestead
Enter fullscreen mode Exit fullscreen mode

The only thing you need to edit to quickly get up and running are the folders and sites properties. The above setting are the default but it's unlikely they'd work for you.

The folders property needs to point to the php project folder you'd want to run with in the homestead box. Then the sites property is where you specify a domain name for the project!

The project folders are completely independent from the homestead configuration folder. (It's what I am loving in comparison to Scotch Box (covered previously))

For the purpose of this demonstration, lets assume that our projects are inside ~/MyWORK/test-projects. The the folders property would reflect that

folders:
    - map: ~/MyWORK/test-projects
      to: /home/vagrant/code
Enter fullscreen mode Exit fullscreen mode

Map points to the directory in our machine, and to points to the directory in the virtual machine. By mapping the two, we ensure that they stay in synchrony. So eventually when we run a website from the virtual machine, it's files mirror the files we have at ~/MyWORK/test-projects

The sites property then, maps a directory from the virtual machine with a custom domain

sites:
    - map: site1.test
      to: /home/vagrant/code/site1
Enter fullscreen mode Exit fullscreen mode

Again /home/vagrant/code/ has been mapped to a directory from our machine, hence everything within ~/MyWORK/test-projects exists inside /home/vagrant/code/, meaning that /site1 exists in ~/MyWORK/test-projects/sites - where we actually write code.

Editing the hosts file

As it's noted in Homestead.yaml the IP address for this virtual box is 192.168.10.10 so, inside our hosts file (/etc/hosts on macs and C:\Windows\System32\drivers\etc\hosts on windows) we need to add this line:

192.168.10.10  site1.test
Enter fullscreen mode Exit fullscreen mode

Hence when we visit site1.test in the browser it will trigger homestead and in turn Homestead.yaml will serve the web project located at /home/vagrant/code/site1

Running vagrant

Now that everything is setup, from the terminal, with Homestead directory we need to start vagrant

vagrant up
Enter fullscreen mode Exit fullscreen mode

As usual, the first time vagrant runs it will take some time as it needs to install the box. But every other time it will be quick to start and stop

Working with Databases

The homestead box comes with MySQL and PostgreSQL already setup and they are running the moment homestead vagrant box is up.

You can connect to those from 127.0.0.1 and port 33060 (MySQL) or 54320 (PostgreSQL). The username and password for both databases is homestead / secret.

Conclusion

This box is a lot easier then Scotch Box. I love the fact that it does encourage us to use one box for all our PHP projects - which is what I wanted.

The thing that I want to know is, how does the mapping work in terms of space, are the projects cloned or just referenced?

Top comments (0)