Wednesday, February 16, 2022

Install LAMP on Ubuntu using Ansible

Description: Here I have explained, How to install LAMP on ubuntu using Ansbile by creating separate roles for each.

Install Ansible on Ubuntu

# apt install ansible

# ansible --version   [To verify the ansible version]


Generate SSH keys and copy it to destination server

To communicate/Authorise Ansible server to destination server need to generate ssh keys to destination server using URL

Setup Host in Ansible host file 

Open host configuration file under /etc/ansible/hosts file and add host as follow:

# vi /etc/ansible/hosts

#[dbservers]
# #db01.intranet.mydomain.net #db02.intranet.mydomain.net # Here's another example of host ranges, this time there are no # leading 0s: #db-[99:101]-node.example.com [web] 20.124.15.109

Verify the Ansible result by ping using below command

# ansible -m ping web


Create Directory for different roles and variable file 

Navigate to folder and create different roles for each components. Here, I am going to create for 3 roles [Apache, PHP and MySQL] using below command 

# ansible-galaxy role init apache
# ansible-galaxy role init php
# ansible-galaxy role init mysql

Run tree command to verify the folder structure for all three roles

# tree


Declare Variables for each role: First I am going to declare variable for each role. For variable I am creating role for group variable using below command 
# ansible-galaxy role init groupvar

Once variable role created navigate to var folder in groupvar and create  variable file as follow 

# cd /home/serverapprunner/Apache/groupvar/vars
# vi main.yml

--- ### Variables for LAMP ### http_host: techserverweb.com http_conf: techserverweb.com.conf http_port: 80 app_user: www-data sudo_user: root mysql_root_password: P@ssw0rd123 php_version: 7.4

Here, I have defined variable like host name, configuration, Port number, etc..

Apache: After setup main variable file, first I am going to create task, handlers and file for Apache 

Navigate to Apache folder under role folder. Open tasks folder and paste below content in main.yml file 

--- - name: Install prerequisites apt: name={{ item }} update_cache=yes state=latest force_apt_get=yes loop: [ 'aptitude' ] #Apache Configuration - name: Install Apache Packages apt: name={{ item }} update_cache=yes state=latest loop: [ 'apache2' ] - name: Create document root file: path: "/var/www/{{ http_host }}" state: directory owner: "{{ app_user }}" mode: '0755' - name: Set up Apache virtualhost template: src: "files/apache.conf.j2" dest: "/etc/apache2/sites-available/{{ http_conf }}" - name: Enable new site shell: /usr/sbin/a2ensite {{ http_conf }} #- name: Disable default Apache site # shell: /usr/sbin/a2dissite 000-default.conf # when: disable_default # notify: Reload Apache # UFW Configuration - name: "UFW - Allow HTTP on port {{ http_port }}" ufw: rule: allow port: "{{ http_port }}" proto: tcp - name: Reload Apache service: name: apache2 state: reloaded - name: Restart Apache service: name: apache2 state: restarted

Open files directory and create apache.conf.j2 file and paste content as follow

<VirtualHost *:{{ http_port }}> ServerAdmin webmaster@localhost ServerName {{ http_host }} ServerAlias www.{{ http_host }} DocumentRoot /var/www/{{ http_host }} ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/{{ http_host }}> Options -Indexes </Directory> <IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule> </VirtualHost>

Open handler directory and create main.yml file, Paste content as follow. To restart the apache2 service after installation
--- - name: Reload Apache service: name: apache2 state: reloaded - name: Restart Apache service: name: apache2 state: restarted

PHP: After setup for Apache, Creating yaml file for defaults, files, tasks and handlers

Open defaults folder and define php modules in default.yml file 

--- # defaults file for php7x php_modules: - fpm - mysql - cli - common - json - opcache - gd - intl - curl - mbstring - zip - xml - soap - bcmath - dev

Open tasks folder and create default.yml file as follow to install PHP7.4 

--- - name: Setting Default Language shell: LC_ALL=en_US.UTF-8 - apt_repository: repo: ppa:ondrej/php - name: Update APT shell: apt-get update - name: Install php{{ php_version }} apt: name: "php{{ php_version }}-{{ item }}" state: present with_items: "{{ php_modules }}" - name: Copy PHP info to Webroot template: src: "files/info.php.j2" dest: "/var/www/{{ http_host }}"

Create info.php.j2 file for phpinfo page copy to virtual host location in files directory as follow

<?php phpinfo();

MySQL: After setup PHP, creating main.yml file under tasks directory as follow

--- - name: Install prerequisites apt: name={{ item }} update_cache=yes state=latest force_apt_get=yes loop: [ 'aptitude' ] #Install MariaDB server - name: Install MariaDB Packages apt: name={{ item }} update_cache=yes state=latest loop: [ 'mariadb-server', 'python3-pymysql' ] # Start MariaDB Service - name: Start MariaDB service service: name: mariadb state: started become: true # MariaDB Configuration - name: Sets the root password mysql_user: name: root password: "{{ mysql_root_password }}" login_unix_socket: /var/run/mysqld/mysqld.sock - name: Removes all anonymous user accounts mysql_user: name: '' host_all: yes state: absent login_user: root login_password: "{{ mysql_root_password }}" - name: Removes the MySQL test database mysql_db: name: test state: absent login_user: root login_password: "{{ mysql_root_password }}"

After setup all three roles, create deploy.yml file at root folder path. In which I have define roles which need to run

--- - name: apply common configuration to all nodes hosts: all become: yes become_method: sudo vars_files: - roles/groupvar/vars/main.yml roles: - apache - mysql - php

Run the ansible playbook by run below command 

# ansible-playbook deploy.yml


Verification: After successfully deploy verify Apache, PHP and MySQL 

MariaDB:


Apache:



PHP:




No comments:

Post a Comment