DEV Community

Josip Trbuscic for Bornfight

Posted on

Ansible playbook for LAMP stack on Amazon linux 2 AMI

I've recently written an Ansible playbook which sets up a LAMP stack on your Amazon linux 2 EC2 server.

Packages:

  • Php7.4 fpm
  • MariaDB 10.3
  • Apache

Ansible playbook:

- hosts: all
  vars:
    composer_local: composer.phar
    composer_global: /usr/bin/composer
  tasks:
    - name: Enable php74 and epel repositories
      become: yes
      shell: amazon-linux-extras enable php7.4 epel
    - name: Add MariaDB-10.3 repository
      become: yes
      yum_repository:
        name: MariaDB-10.3
        description: mariadb
        baseurl: http://yum.mariadb.org/10.3/centos7-amd64
        gpgkey: https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
        gpgcheck: yes
    - name: Install yum packages
      become: yes
      yum:
        name:
         - epel-release
         - httpd
         - acl
         - git
         - MariaDB-server
         - MariaDB-client
         - php-common
         - php-fpm
         - php-gd
         - php-intl
         - php-json
         - php-mbstring
         - php-mysqlnd
         - php-xml
        update_cache: yes
    - name: Update php-fpm port
      become: yes
      ini_file:
        path: /etc/php-fpm.d/www.conf
        option: listen
        value: 9000
        section: www
        backup: yes
    - name: Check if composer is installed
      command: which composer
      changed_when: false
      failed_when: composer_installed.rc not in [0, 1]
      register: composer_installed
    - name: Install composer
      script: scripts/install_composer.sh
      when: composer_installed.rc != "0"
    - name: Move Composer globaly
      become: true
      command: "mv {{ composer_local }} {{ composer_global }}"
      when: composer_installed.rc !=  "0"
    - name: Set composer permissions
      become: true
      file:
        path: "{{ composer_global }}"
        mode: "a+x"
      when: composer_installed.rc !=  "0"
    - name: Enable mariadb service
      become: yes
      service:
        name: mariadb
        enabled: yes
        state: started
    - name: Create swap file
      become: yes
      command: dd if=/dev/zero of=/swapfile  bs=1MiB count=2048 creates=/swapfile
    - name: Change swap file permissions
      become: yes
      file: path=/swapfile
            owner=root
            group=root
            mode=0600
    - name: "Check swap file type"
      become: yes
      command: file /swapfile
      register: swapfile
    - name: Make swap file
      become: yes
      command: "sudo mkswap /swapfile"
      when: swapfile.stdout.find('swap file') == -1
    - name: Swapon
      become: yes
      command: "sudo swapon /swapfile"
      when: swapfile.stdout.find('swap file') == -1
    - name: Write swap entry in fstab
      become: yes
      mount: name=none
             src=/swapfile
             fstype=swap
             opts=sw
             passno=0
             dump=0
             state=present

Composer installation script scripts/install_composer.sh (source)

#!/bin/sh

EXPECTED_CHECKSUM="$(wget -q -O - https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_CHECKSUM="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"

if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]
then
    >&2 echo 'ERROR: Invalid installer checksum'
    rm composer-setup.php
    exit 1
fi

php composer-setup.php --quiet
RESULT=$?
rm composer-setup.php
exit $RESULT

If you have any questions about this, feel free to leave me a comment below!

Top comments (1)

Collapse
 
pedrevans profile image
Peter Evans

Is the swap file strictly necessary?