Creating your own PHP dev-env in Vagrant: Bonus 1

June 3, 2016

Table of Contents

Part 1

Part 2

Part 3

Bonus 2

Full Code Base on GitHub

In the previous posts we’ve successfully set up a development environment for PHP. In the following posts I will present some bonus things, which you can do, to optimize your work with vagrant. In this post I will show you how to set a virtual host in apache inside your guest system. Because IP addresses can be forgotten quite easily, it’s much more handy to have a short named address under which you can access your web app. In this project we will create a virtual host for our main project directory called So let’s get started!

First we create a config file inside our conf directory with the following content:

This is the virtual host definition. The first line says that it’s listening on port 80, which is the default port. The DocumentRoot is the path to our webspace, which was symlinked to the vagrant synced directory in an earlier post. ServerName is the address, how you can access the page and the ServerAlias is the alternative if the ServerName isn’t accessible. The following two lines are the paths to the access and error log of our virtual host. If anything bad happens, those two files are the place to start troubleshooting. The following lines are some directory options about what the web app is allowed to do and what not.

Next we need a new shell script, which we call Everything required to setup your project(composer update, migrations etc.) will be defined in this script. In this tutorial we won’t do all this, but only activating our virtual host address.

Most of this should be familiar to you from the previous chapters. First we check if the file exists in our temp directory. If it does, we create the directory for our project and set the owner of this directory. Afterwards we move the file to the sites-available directory of apache and enable it. To make sure all settings are active, we restart the apache demon. As you already know, we need to get the mysite.conf into the tmp directory. That’s why we add another file provisioner to our Vagrantfile.

To activate the new provision you either need to execute

Or vagrant destroy and vagrant up again.

The last step is to make the virtual host known to your host system. For this, you need to add a line into your hosts file. On unix like systems you can find the file in the /etc/ directory. The file should begin with the following

After this intro we add the following line

Which maps the IP address of our private network hosted by vagrant to a domain name.

Ok let’s check it out. Type in into the address bar of your browser. You should now see the php info page from the last tutorial. In the next post I want to cover the database access. I will show two ways to you, how you can interact with your database from your host system. The first way will be with MySQL Workbench via an SSH connection. The second is by installing PHPMyAdmin on the guest system and access it via the hosts web browser.

Go to Bonus 2