A small PHP project with Homestead

In this post, we are going to initialize a simple PHP project with Laravel Homestead.

Laravel Homestead is a pre-packaged Vagrant box that provides a development environment without requiring us to install PHP, a web server or any other server software on our local machine.

The first thing we have to do is to download and install VirutalBox and Vagrant. When it is done, we can add the laravel/homestead box to our Vagrant installation using the following command in our terminal:

vagrant box add laravel/homestead

Add Homestead box to Vagrant

Now, let’s go to our “home” directory and install Homestead using Git:

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

Clone Homestead

Once the Homestead directory is cloned, we can go into that last one and initialize Homestead:

cd Homestead
bash init.sh

Initialize Homestead

Now we have to configure our installation by opening the Homstead.yaml file in our directory. First, we need to set the provider:

provider: virtualbox

Provider section in Homestead.yaml

We may have to generate our SSH key by doing the following command:

ssh-keygen -t rsa

Generate SSH key

By default, our key will be saved in our “home” directory.

Let’s now set the folders property. That last one lists all of the folders we want to share with our Homestead environment. As files within these folders are changed, they will be kept in sync between our local machine and the Homestead environment.

- map: ~/Code/Php
to: /home/vagrant/Code

Folders section in Homestead.yaml

We are now going to map our sites:

- map: our-project-name.dev
to: /home/vagrant/Code/our-project-name.dev/
- map: second-project.dev
to: /home/vagrant/Code/second-project.dev/web
type: symfony

Sites section in Homestead.yaml

We need to set a few things in our hosts file. On macOS and Linux, this file is located at /etc/hosts. On Windows, it is located at C:\Windows\System32\drivers\etc\hosts. Let’s add the following lines:

# Vagrant our-project-name.dev second-project.dev

Hosts file

We have to make sure that the IP address listed is the one set in our Homestead.yaml file.

We can run our box by doing the folliwng command:

vagrant up —-provision

Provision and run the box

If we make any change in the Homestead.yaml file, we need to update the Nginx configuration on the virtual machine with the following command:

vagrant reload --provision

Reload the box

If everything is alright, we can now access to our-project-name.dev and second-project.dev via our web browser.

We may also need to connect to our machine to install some stuff. If so, we can do it like so from our Homestead directory:

vagrant ssh

Connection using SSH

A homestead database is configured for both MySQL and Postgres out of the box. To connect to our MySQL or Postgres database from our host machine’s database client, we have to connect to and port 33060 (MySQL) or 54320 (Postgres). For both databases, the username is homestead and the password is secret.