Last Updated: August 07, 2022
·
36.71K
· pix-art

How to create a LAMP stack with Ansible

This guide wil help you create a basic playbook with roles. I'll be showing you some of the issues i ran into so you don't have to worry about them. You can also download all files used in this example on my github: Pix-art LAMP-playbook

Assumptions

You have installed Ansible on your os. (If you haven't check out Step 1 and 2 on my other Protip)

STEP 1: Create our hosts file

Our host file will contain all info about the places where we want to run our playbook.
Here is an example for a vagrant box:

10.0.0.10   ansible_ssh_user=vagrant ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

STEP 2: Create our playbook.yml

This file should contain at least 2 things:
1) Which hosts we want this playbook to run on
2) Which roles we want to execute on them

---
- hosts: all
  roles:
    - apache2
    - mariadb

By defining roles Ansible will try to look up these roles in the Roles folder. This should be created as following :

playbook.yml
roles/
    <your role>
        tasks
            main.yml
        files
            <your files>

For more info about best practices and why we structure our folders this way i would suggest reading the tips on the Ansible Website.

STEP 3: Create our Apache role

roles/
    apache2
        tasks
            main.yml
        files
            index.php

In our main.yml we'll be listing all our commands to execute for this role.

---
- name: 1. install Apache
  apt: name=apache2 state=present

- name: 2. install PHP module for Apache
  apt: name=libapache2-mod-php5 state=present

- name: 3. start Apache
  service: name=apache2 state=running enabled=yes

- name: 4. install Hello World PHP script
  copy: src=index.php dest=/var/www/html/index.php mode=0664

In our index.php we'll place a simple hello world.

<?php
echo "Hello World!";

STEP 4: Create our MariaDB role

roles/
    mariadb
        tasks
            main.yml
        files
            db.php
            dump.sql

In our main.yml we'll be listing all our commands to execute for this role.

---
- name: 1. Install MariaDB server package
  apt: name=mariadb-server state=present

- name: 2. Start Mysql Service
  service: name=mysql state=started enabled=true

- name: Install python Mysql package #required for mysql_db tasks
  apt: name=python-mysqldb state=present

- name: 3. Create a new database
  mysql_db: name=demo state=present collation=utf8_general_ci

- name: 4. Create a database user
  mysql_user: name=demo password=demo priv=*.*:ALL host=localhost state=present

- name: 5a. Copy sample data
  copy: src=dump.sql dest=/tmp/dump.sql

- name: 5b. Insert sample data
  shell: cat /tmp/dump.sql | mysql -u demo -pdemo demo

- name: 6a. Install MySQL extension for PHP
  apt: name=php5-mysql state=present

- name: 6b. Restart Apache
  service: name=apache2 state=restarted

- name: 7. install Hello World PHP script with database
  copy: src=db.php dest=/var/www/html/db.php mode=0664

In our dump.sql we do a create + insert to have some data

CREATE TABLE IF NOT EXISTS demo (
  message varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO demo (message) VALUES('Hello World!');

In our db.php we'll place a simple hello world with a DB connection.

<?php

$connection = new PDO('mysql:host=localhost;dbname=demo', 'demo', 'demo');
$statement  = $connection->query('SELECT message FROM demo');

echo $statement->fetchColumn(); 

STEP 5: Run our playbook

ansible-playbook -i hosts playbook.yml --sudo

This command has 3 parts.
-i hosts = a link to our inventory(hosts file) so Ansible knows where to run which commands

playbook.yml = this is our playbook file to tell Ansible what commands to run on which hosts

--sudo This tells Ansible to run all commands as Sudo.

This guide should have shown you how to create a basic LAMP setup with Ansible. You also have 2 example files that should work on your servers root. Index.php and DB.php both should show you 'Hello world'.

If you want more info on how to use this playbook on a Digital Ocean droplet checkout my other Post)

All my tips have been moved to my blog www.pix-art.be so come check it out!

3 Responses
Add your response

Thanks for such a nice tutorial/article:
I think the better option is to use like this:

  • name: Install Apache

apt: name={{ item }} state=installed update_cache=yes

with_items:

- apache2

-  libapache2-mod-php5

and also:

  • name: Install MariaDB

apt: name={{ item }} state=installed update_cache=yes

with_items:

- mariadb-server

-  python-mysqldb

Thanks

over 1 year ago ·

You're absolutely right. As always there are many ways to tackle many problems. The reason I'm using it like this is because I based myself on a manual install tutorial and just created the ansible line per install command. You can of course optimize by combining those 2 apt-get's into one. But this was all for the sake of tutoring. I wouldn't recommend using this as an end product, you would be better of using one of the prepared roles on https://galaxy.ansible.com/

over 1 year ago ·

I don't think it is necessary to install and configure apc and mariadb for LEMP stack on Debian. It can be done without it, like here: https://www.cloudways.com/blog/how-to-create-a-lemp-stack-on-debian-server/ The main packages to install are nginx, php, mysql, and phpfpm.

over 1 year ago ·