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: "8.3"

  tasks:

    - name: Install APACHE

      ansible.builtin.apt:

        name: apache2

        state: present

        update_cache: yes


    - name: Start & Enable HTTP service

      ansible.builtin.service:

        name: apache2

        state: started

        enabled: yes


    - name: Install MARIADB-SERVER

      ansible.builtin.apt:

        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 Ubuntu

      ansible.builtin.debug:

        msg: "Configuring PHP {{ php_version }}"


    - name: Add PHP repository

      ansible.builtin.apt_repository:

        repo: ppa:ondrej/php

        state: present

        update_cache: yes


    - name: Refresh package cache after repository addition

      ansible.builtin.apt:

        update_cache: yes


    - name: Install PHP packages and common extensions

      ansible.builtin.apt:

        name:

          - "php{{ php_version }}"

          - "php{{ php_version }}-cli"

          - "php{{ php_version }}-fpm"

          - "php{{ php_version }}-mysql"

          - "php{{ php_version }}-gd"

          - "php{{ php_version }}-mbstring"

          - "php{{ php_version }}-xml"

          - "php{{ php_version }}-opcache"

          - libapache2-mod-php

        state: present


    - name: Start and enable PHP-FPM service

      ansible.builtin.service:

        name: "php{{ php_version }}-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



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


Verify Services:

      systemctl status apache2

      systemctl status mariadb

      systemctl status php-fpm

      php -v