Automate Deploying a PHP App Using Ansible on Ubuntu 16.04

Leave your reply

Introduction

Ansible is an excellent tool for system administrators who find themselves performing the same tasks over and over. This tutorial will walk through the process of using Ansible to deploy a basic PHP app to a target server.

Requirements

  • One Cloud Server running Linux (Ubuntu 16.04) to serve as the build server, with Ansible installed and running. This server is where the files for this tutorial will be stored, and where commands will be issued.
  • One or more Cloud Server(s) running Linux (Ubuntu 16.04) to serve as the target server. The PHP app will be deployed on this server.
  • Ansible hosts file on the build server configured to include the IP address of the target server.
  • SSH keys set up to allow the build server to SSH to the target server without needing a password.
  • PHP installed and configured on the target server, version 5.4 or higher

Use the command php -v to check your PHP version:

[user@localhost ~]# php -v
PHP 5.4.16 (cli) (built: Nov  6 2016 00:29:02)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

For more information on setting up Ansible, adding an IP address to an Ansible hosts file, and setting up SSH keys to allow passwordless logins, see the article Set Up Ansible on Ubuntu 16.04.

Set Up the File Structure

For this tutorial we will deploy a simple "Hello, World!" app using the Fat-Free Framework for PHP. For more information on using Fat-Free Framework, see our article Install and Use the Fat-Free Framework for PHP.

Begin by creating the file structure on the build server. Move to your home directory:

cd ~

Create a directory for the Ansible playbook:

mkdir fatfree

Move into this directory:

cd fatfree

Create the Ansible playbook and the hosts file, which we will edit later:

touch playbook.yml
touch hosts

Create a roles directory inside the fatfree-helloworld directory:

mkdir roles

Move into this directory:

cd roles

It is an Ansible "best practice" to create a role for each distinct group of commands. Use Ansible's ansible-galaxy command to create a pre-set file structure for each role:

ansible-galaxy init server 
ansible-galaxy init install 

Update the Playbook and Hosts Files

Move to the directory where the playbook is stored:

cd ~/fatfree/

Open the hosts file for editing:

 nano hosts

Add the [helloworld] group and the IP address of the target server:

[helloworld]
192.168.0.2

Save and exit the file.

Open the playbook.yml file for editing:

nano playbook.yml

Add the following content to this file:

- hosts: helloworld

  roles:
    - server
    - install

This tells Ansible to expect two groups of commands (roles): server and install.

Save and exit the file.

Test Ansible's ability to connect to the target server:

ansible-playbook playbook.yml -i hosts -u root -K

Enter the sudo password for the build server. If all is well, the server will respond with:

PLAY [helloworld] **************************************************************

TASK [setup] *******************************************************************
ok: [192.168.0.2]

PLAY RECAP *********************************************************************
192.168.0.2              : ok=1    changed=0    unreachable=0    failed=0

Set Up the Roles

Server Role

We will use the server role to update packages on the target server. Edit the server roles file:

nano ~/fatfree/roles/server/tasks/main.yml

Be sure to leave the first line ---in place. Add the following to the file, so that in total the file reads:

---
- name: Update apt cache
  apt: update_cache=yes cache_valid_time=3600
  sudo: yes

Save and exit the file.

Install Role

The install role will be used for installing Fat-Free Framework and copying over the example "Hello, World!" script. Edit the install roles file:

nano ~/fatfree/roles/install/tasks/main.yml

Be sure to leave the first line ---in place. Add the following to the file, so that in total the file reads:

---
- name: Download the install files
  get_url: url='https://github.com/bcosca/fatfree/archive/master.zip' dest='/var/www/html/master.zip'
  register: result

- unarchive:
    src: /var/www/html/master.zip
    dest: /var/www/html/
    remote_src: True

- copy:
    src: ~/fatfree/roles/install/files/index.php
    dest: /var/www/html/index.php

Save and exit the file.

The ansible-galaxy command created a files directory in the install role directory. This is where we will put the index.php file which we will copy over to the target server.

Create the index.php file and open it for editing:

sudo nano ~/fatfree/roles/install/files/index.php

Add the following content to the file:

<?php
$f3 = require('fatfree-master/lib/base.php');
$f3->route('GET /',
    function() {
        echo 'Hello, world!';
    }
);
$f3->run();
?>

Save and exit the file.

Run the Playbook

Now it's time to see the whole thing in action. Move to the directory where the playbook is stored:

cd ~/fatfree

Run the playbook with the command:

ansible-playbook playbook.yml -i hosts -u root -K

Switch to a browser and visit your domain's root URL (http://example.com). If Fat-Free Framework has been installed correctly, you will see the message "Hello, world!"