A LAMP stack is a foundational open-source web platform bundle used to build and host dynamic websites and applications. It serves as a reliable, community-maintained environment powering everything from simple personal blogs to large-scale enterprise eCommerce stores. 

What Does LAMP Stand For?

The acronym stands for the four core layers that build your web environment:

L (Linux): The operating system layer. It acts as the secure, stable, and highly flexible foundation for the entire server.

A (Apache): The web server software. It processes HTTP requests and safely delivers your web pages to user browsers.

M (MySQL): The database layer. It organizes, stores, and retrieves all the data your website uses.

P (PHP): The programming language. It processes the dynamic business logic and interacts seamlessly with the Apache server and MySQL database

This playbook automates installation of:

    Apache Web Server (HTTPD)

    MariaDB Database Server

    PHP and PHP Extensions

    Service Management and Validation

Before running the playbook, ensure Ansible is installed on your system/Instance in any cloud, using the installation guide provided below.

https://docs.ansible.com/projects/ansible/latest/installation_guide/installation_distros.html

ANSIBLE-PLAYBOOK CODE:(file name: lamp.yaml)

---

- name: LAMP SETUP USING ANSIBLE

  hosts: webservers

  become: yes


  vars:

    php_version_stream: "8.1"


  tasks:


    - name: Install APACHE

      ansible.builtin.yum:

        name: httpd

        state: present


    - name: Start & Enable HTTP service

      ansible.builtin.service:

        name: httpd

        state: started

        enabled: yes


    - name: Install MARIADB-SERVER

      ansible.builtin.yum:

        name: mariadb-server

        state: present


    - name: Start & Enable MARIADB-SERVER

      ansible.builtin.service:

        name: mariadb

        state: started

        enabled: yes


    - name: Install and Configure PHP on CentOS 9

      ansible.builtin.debug:

        msg: "Configuring PHP {{ php_version_stream }}"


    - name: Reset the existing PHP module stream

      ansible.builtin.command:

        cmd: yum module reset php -y

      changed_when: false


    - name: Enable the specified PHP module stream

      ansible.builtin.command:

        cmd: "yum module enable php:{{ php_version_stream }} -y"


    - name: Install PHP packages and common extensions

      ansible.builtin.yum:

        name:

          - php

          - php-cli

          - php-common

          - php-fpm

          - php-gd

          - php-mbstring

          - php-xml

          - php-opcache

        state: present


    - name: Start and enable PHP-FPM service

      ansible.builtin.service:

        name: php-fpm

        state: started

        enabled: true


    - name: Verify PHP installation version

      ansible.builtin.command: php -v

      register: php_version_output

      changed_when: false


    - name: Print installed PHP version

      ansible.builtin.debug:

        var: php_version_output.stdout_lines


INVENTORY FILE:(file name: inventory)


all:

  hosts:

    web01:

      ansible_host: 172.31.17.77

    web02:

      ansible_host: 172.31.18.155

    db01:

      ansible_host: 172.31.22.132

  children:

    webservers:

      hosts:

        web01:

        web02:

    dbservers:

      hosts:

        db01:

    dc_oregon:

      children:

        webservers:

        dbservers:

      vars:

        ansible_user: ec2-user

        ansible_ssh_private_key_file: client-key.pem


Run the Playbook:

  ansible-playbook -i inventory lamp.yaml

Verify Services:

  systemctl status httpd

  systemctl status mariadb

  systemctl status php-fpm

  php -v

  


Run the command to check the syntax in the code:

    ansible-playbook lamp.yaml --syntax-check


Perform a Dry Run Before Execution

Before running the playbook on your servers, it is a good practice to perform a dry run. A dry run allows you to see what changes Ansible would make without actually applying them to the target systems.

Use the following command:

    ansible-playbook lamp.yaml -C

or

    ansible-playbook lamp.yaml --check


Note: A dry run does not always perfectly reflect the actual execution. In some cases, a task may fail during a dry run but succeed during normal execution, and vice versa. Therefore, dry runs should be used as a validation tool rather than a guarantee of the final outcome.

Run the Playbook:

      ansible-playbook -i inventory lamp.yaml


This playbook provides a basic automated LAMP setup suitable for lab, development, and beginner production deployments.