Installing PHP using Ansible Playbook

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.

In this post, We will automate the PHP installation using Ansible Playbook on ubuntu 18.04 LTS, 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

    Save and exit from the vim editor and execute the follwing command to test node connectivity.

    ansible all -m ping

    Step 2: Creating PHO 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

    You should receive a success message following the installation of PHP with the ansible playbook.

    To check PHP version

    php -v

    To check Installed PHP modules

    php -m


    We have successfully installed PHP using Ansible playbook on ubuntu 22.04 LTS, Kindly report in case you are facing defeculties with follwing details.

    • OS
    • Ansible version
    • Package name
    • Logs – Error / Warning / failed

    Get in touch with our new devops tools based website-

    Installing PHP using Ansible Playbook

    4 thoughts on “Installing PHP using Ansible Playbook

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to top