How to install PHP using Ansible Playbook - TechBeginner

Welcome to, Tech Beginner provides technical blog for Online Linux Tutorials, Ubuntu Tutorials, Redhat Tutorials and CentOS 7, Configuration of server like Web Server, Application server, Database server, Window server, networking, html, css, java script, php, wordpress, opencart, magento, joomla and etc.

How to install PHP using Ansible Playbook

Share This


In this post, We will automate the PHP installation using Ansible Playbook on ubuntu 18.04 LTS.


PHP is a server side scripting language, and an integral asset for making dynamic and intelligent Web pages. PHP is a generally utilized, free, and proficient option in contrast to contenders, for example, Microsoft's ASP. PHP 7 is the most recent stable delivery.


We believe, You have already installed Ansible if not you need to click on given link to install Ansible on ubuntu server. 



Step 1: Configure Ansible 


By default ansible host configuration file is /etc/ansible/hosts Here you need to add the Apache web server details like this, Use the following step for the same, In my case i have 1 apache server.


Group Name  - WebServer

IP Address     -

Username      - example

Password       - XXXXXXXXXX

You need to add your server in ansible host file, Syntax is given below, You need to open the /etc/ansible/hosts file with nano or vim text editor.

sudo vim /etc/ansible/hosts

and add the web server entry like this.

[webserver] ansible_connection=ssh ansible_ssh_user=example ansible_ssh_pass=xxxxxxx

Sample output.

ansible all -m ping

Step 2: Creating Anisble Playbook 


You need to create phpplaybook.yml by using the given command.

sudo vim phpplaybook.yml

Paste the given code to automate the PHP7.4 with modules.


- hosts: webserver
  become: yes
    - name: Installation install software-properties-common
      apt: name=software-properties-common

    - name: "Repo PHP 7.4"
    - name: "Updating the repo"
      apt: update_cache=yes

    - name: Installation PHP 7.4
      apt: name=php7.4 state=present

    - name: install lamp stack
          - php7.4-mysql
          - php7.4-curl
          - php7.4-json
          - php7.4-cgi
          - php7.4-xsl
          - php7.4-cgi
          - php7.4-gd
          - php7.4-mbstring
          - php7.4-zip
          - php7.4-xmlrpc
          - php7.4-soap
          - php7.4-intl
          - libapache2-mod-php
        state: present
        update_cache: yes

Save and exit from the vim text editor and execute the given command for the same.

ansible-playbook phpplaybook.yml

If everything is okay so then you will the sample output like this.

Now you need to verify the PHP version by using the following command.

php -v

Sample output.

Use the given command to verify the PHP modules.

php -m 

Sample output.


We have successfully created Ansible playbook for PHP7.4 in our Ubuntu 18.04 server.


  1. if you instan PHP 7.4 version so why its show
    PPH 8.0.1 as per your giving output(screenshot)

    1. Hi, Thanks for the comment, Actually later on tested same script with PHP version 8.0.1.

  2. How if i want to install exactly php7.4 not php8?