Blog by Daniel Hauck. Sysadmin, Developer, Blogger.

What do we want / need?

Before we can dive deep into symfony in general, we need environment to run our code in. Sure we could install PHP directly on our Mac, Linux or Windows. But if you work on a team and everybody would use a different operating system and web server that would suck hard.

We need to build a environment, that’s easy to use, generic enough to extend and production alike, because we don’t want to be a “It works on my machine”-dickhead.

To accomplish this, we will use an virtual machine, that is able to act as a production server, but also easy to install and maintain. And of course it should run on all major operating systems like Mac, Linux and Windows

Vagrant + Ansible = Love

So to build our cool dev virtual machine, we will create a VirtualBox managed by Vagrant. Of course we do not want to install PHP and Nginx everytime we have a new project or decide to recreate our environment. Therefore we will use ansible, to automate this setup.

First we need to install all the stuff to our local machine, so here you can find the installation instructions for every product:

  1. VirtualBox
  2. Vagrant
  3. Ansible
  4. Install vagrant-hostsupdater plugin:vagrant plugin install vagrant-hostsupdater

Creating our Machine

After installing all the tools and verifying the functionality of them, let’s build the machine.

Therefore create a directory for the vm:

You could now use vagrant init to create a Vagrantfile and so on, but since we will delete most of the Vagrantfiles content, we can also create it by hand. Now fire up your favorite text editor, and let’s create the file:

Now I’ll walk you through the file line by line, or at least the most important parts.
Just look at the comments:

If something isn’t clear, please use the comments function, or head over to the vagrant documentation.

I am still using the trusty release, although xenial is the latest LTS, but the vagrant box has a lot of bugs, that make working with it a PITA.

We are almost done, with our basic setup.

Ansible Setup

Now it’s time to create the necessary file structure for ansible to work. We need the provisioning directory, with a playbook.yml inside it and some additional files.

group_vars: Here lives the dynamic part of our configuration. We will set database parameters and additional packages here.
roles: to stick to ansible best practices, we create one role for each component, e.g. one for php, nginx, postgresql and so on

Let’s create our first role in which we can set the locale for our server. Create another dir inside roles/ called general. We will use the general role, for configuring stuff like the language and the charset. Also for some packages, which are not crucial for the machine to act as a development machine, for example htop and vim.

Inside the tasks directory, you now need to create a main.yml file. Ansible always looks first for a main.yml inside the tasks/ dir. So if we later decide to split up our configuration into different .yml files, just remember to include them into main.yml as a entry point for ansible.

In this file we start defining our first tasks, which is to generate and set the server locale:

Here we see the first dynamic part of our ansible role, the {{ locale }} variable. You can set it dynamically, independent from your role inside the group_vars directory. Therefore let’s create a file called all.

This file will be read used for all vagrant machines provisioned with our Vagrantfile. In our case, just this single machine. Don’t care about multimachine setup now.

Now put the locale variable into the all file:

Ok, so we now generating it, we just need to set it. Therefore we will use another cool feature of ansible, which is lineinfile. Append this to your main.yml inside tasks:

And again we can use our locale variable.

Now we got our variables, created our first role. The only thing that’s missing now, is the playbook itself. Just create a file named playbook.yml:

with the following content, explaned in the comments:

Save the file, and now we should be ready, to do our first boot of the machine and check if our provisioning works:

You will be prompted for the sudo password, since vagrant tries to alter the /etc/hosts and /etc/exports file. After the normal boot of the virtual machine, you can see the start of the provisioning. If not just type:

And you should get a very similar output to this:

Version our config

That’s nearly it for the first part of this little tutorial. The last thing, I want to do is, version our configuration into a repo, so that we can track changes. Which is quite easy.

Initialize your Git Repo:

Add a gitignore:

Add .vagrant/ and playbook.retry to .gitignore:

Add our files to the index and commit:

That’s it

In the next part, I will show you, how to install PHP, nginx and postgresql in your machine.

If you want to stay up to date, be sure to follow me on twitter, instagram or subscribe to the newsletter!


There are no comments.

Leave a Reply