Silex Hello World with Vagrant

All the source code of this article can be found on GitHub (

I will describe here the elements used to build this “Hello World” application on Vagrant.
First, to make it run, you need to install the 2 dependancies.

Once this sofware installed, you will just have to issue these commands to make it work :

After several lines of text describing all the install steps of the VM, you will have a line telling your the URL where you can get the “Hello World”.

How does this work :
Create a Vagrantfile, at the root of your project. This is the file explaining to Vagrant what to launch. The content is

As you can see, this is very handy. Each time you modify the Vagrantfile, you need to issue the “vagrant reload” command to apply it, rebooting the VM.

So let go into the :
as you can see, we first make some apt-install to get Apache and PHP up and running. We also install Git.

Then we copy an Apache conf template file from the repository to the Apache directory.

As you can see in this file, we put the root of the web server to /vagrant/web.
As we have a shared directory between the VM and the host, I found it very helpful to write access.log and error.log into /vagrant/logs. This way, you can see your file directly into your local IDE. (This is the reason why we needed to make www-data owner of the /vagrant directory).

Next we enable / disable the websites of Apache.

The next part install / update composer to get the dependancies of the project.

Finally, we get the IP’s of the VM and write it dow to a url.txt file.

The usage of this environnement is then very simple, you use on your computer the text editor you love, you save the files and in your browser, you can check your application at the url written in url.txt.
When you finished working on your feature, you commit the result and you can stop the VM with “vagrant halt” command. Next time, when you will issue “vagrant up”, the VM will start and update itself.

When a developer needs to modify a behavior of the VM or a specific parameter of Apache, he modifies the scripts, commit them and the next time you pull and vagrant up, your VM will be up to date.

Major vagrant commands :

Host configuration is code, as application, we are in devops world !
NB : what we do in bootstrap shell would probably benefit of being done throught Chef Recipes, I will probably work on this in the coming weeks !