Blog by Daniel Hauck. Sysadmin, Developer, Blogger.


In the last part of the tutorial we got a huge amount of work done. The video was quite long, but we layed the foundation of our development machine.

In this part, I will show you how to install PHP 7 along with some dependencies.


Ubuntu Xenial is already shipped with PHP 7.0, but since we are using Trusty and of course we want the latest shit, we will use Ondrej Surys PPA to get at least PHP 7.1. So it’s a good way to stay up to date, even if we would use Xenial.

Let’s create a new role, where our PHP action can happen. Therefore create a new directory inside roles, called php:

As you can see, I also created the tasks directory. With Ansible we can easily add the PPA to our system. Create the main.yml file with the following content:

It does the same, as if you would run add-apt-repository ppa:ondrej/php. After adding the ppa, we can start with installing PHP and some dependencies, which we will need later in our symfony project. To be able to change the PHP version easily, we just put the version in our group_vars. To do so, just append this part to group_vars/all:

After that we can complete our task for installing PHP requirements in provisioning/roles/php/tasks/main.yml:

Now you learned another cool thing in ansible and that is with_items. With with_items you can simply loop throug a list of items, either right declared here, or declared in the playbook or in your group_vars/all file.

If you want to use MySQL instead of PostgreSQL, be sure to replace pqsql with mysql to get the right package.

I didn’t include PHP-FPM here for a purpose. In my opinion it deserves an own role, because we will to configure some stuff here. So let’s do a new role here:

If you don’t know what these curly braces mean, go check out my post about brace expansion, that’s what it is called.

Let’s write the first task, therefore create the main.yml file:

This will install PHP-FPM, create a default pool config and start the service. Because the standard config won’t work properly with vagrant and we’d have to change things manually, let’s delete it and create our own.

You learned another cool thing here, the file module. You can use it to create, delete and modify files. Now we would have no pool available, so we need to create one. To do so, we first need to talk about templates.

Template time!

Templates are files filled with variables that will be populated or rendered with values either from the playbook, the group or host var or from default values. Ansible uses the Jinja2 templating library, which should be quite familiar if you ever used Django, Symfony with Twig or also Flask with Jinja.

You can easily recognize variables by their opening and closing double curly braces: {{ }}
But the cooler thing are control structures like loops and conditions, with them you can render your templates based on conditions. Control structures alway begin with an opening curly brace, followed by a percent sign and the same in reverse for closing ones: {# #}

Let’s create our template, therefore create a file called pool-template.j2 in provisioning/roles/php-fpm/ and insert this code:

We only have variables in here, no control structures, but wait. I call the variable and item.user, they don’t exist. Let’s create them in group_vars/all:

What’s wrong with me? Why did I call it apps and not item? Don’t worry, we will get to that in a second.

You could just put ansible directly in there, but let’s you want to reuse this role in production, then you are easily able to change the username to app_projectname or so.

And another cool thing here, we just made a list of dicts in yaml called apps, so we can easily iterate over it and create as much pool configs as we want. And that’s why I called it item in the template, so we can now iterate over the apps list with the awesome function with_items. Let’s use it and create our rendering task:

Ain’t that simple? Just give template your template file at src and render it to dest. Imagine what cool things you can do with this 😉

The last thing we need for today is to restart the FPM service, to activate our new configuration. Therefore you create a handler. Handlers in ansible are executed after a sucessful run of the playbook, easy as that. You just need to notify them in a task that changed that requires the handler to run.

In our case it’s the rendering of the pool configs, which needs a restart of php-fpm. Just append this notify to our last task:

Too easy? Ok, of course we need to create this handler to work! We already create d a directory for them, so just create another main.yml inside there and execute the service module there:

Let’s test our work! But don’t forget to amend your new roles first!

To do so open up you playbook.yml and add the php and the php-fpm role:

Since the php role adds the PPA it has to run before the php-fpm role, just keep that in mind.

Let’s hit vagrant provision and see what we get here:


Version everything!

Don’t forget to version your changes!

Wow, we learned a lot of stuff today. We learned how to use services, templates, lists, dicts and a lot more. And just imagine, what badass stuff you can do with this 😉

See you in the next part and don’t forget to subscribe to the newsletter or follow me on any social net. Thanks for reading!

There are no comments.

Leave a Reply