Bobcares

Ansible Lamp Debian: Configuration and Setup

by | Sep 28, 2022

 Let us learn more about the ansible lamp Debian and the configuration steps necessary to set it up with the support of our Server Management Support Services at Bobcares.

LAMP and Ansible

Ansible Lamp Debian

The abbreviation LAMP stands for Linux, Apache, MySQL/MariaDB, and PHP stack. This results in a robust stack that drives many of the internet’s websites and online apps.

The LAMP stack is a collection of four open-source projects that, when combined, provide a full solution.

Ansible is an open-source configuration management and orchestration tool that is commonly used by system administrators and cloud users to automate activities and processes.

Setup Pre-requisites for Ansible Lamp Debian

Before we begin, please ensure that we have the following minimal needs to install the ansible lamp on Debian.

  • Debian 10/11 hosts
  • A remote user with Sudo privileges
  • SSH key authentication set up for remote execution
  • Ansible installed on your control node

Environment Preparations

If we don’t have Ansible installed on the machine, we can install it with the commands below. Remember, this shouldn’t be done on the servers we intend to deploy the LAMP stack, but rather on the machine we will use to run the ansible commands.

sudo apt install ansible -y

Configure the setting for the SSH key authentication to the remote server:

ssh-copy-id username@remote-server

Make a directory on the workstation that will serve as the working directory for this configuration.

mkdir ansible_playbook && cd $_

Configure Ansible Playbook and Roles.

We can configure Ansible playbooks and roles to do the following tasks:

  • Install Aptitude, the preferred package manager for Ansible.
  • Will install Apache, MariaDB-server, and PHP.
  • Create and enable a VirtualHost for Apache.
  • Disable the default VirtualHost for Apache.
  • Configure the root password for MariaDB.
  • Configure PHP test file.
  • Allow HTTP traffic through the firewall.

We will establish an ansible role for installing Apache and PHP, as well as another for installing MariaDB. This step acts as one of the initial steps to install the ansible lamp on Debian.

Create Default Variables

Make the default variables file, which will contain information like the domain name, MariaDB root password, and so forth.

We must establish a subfolder called vars in the working directory and add a variables configurations file.

mkdir vars && cd vars
vim default.yml

In the default.yml file, type in the details given below by replacing the variables.

---
mysql_root_password: "P@ssw0rd"
app_user: "apache"
http_host: "lamp.abcd.com"
http_conf: "lamp.abcd.com.conf"
http_port: "80" d
isable_default: true

In the default working directory, create an inventory file for a host. The IP address of the remote server where we plan to deploy the LAMP stack must be provided.

vim hosts
....
[lampstack]
192.168.100.227

Create Apache Role

Configure up a roles sub-directory in the working directory, as well as another for Apache. To accomplish this, use the following code:

mkdir -p roles/apache

Inside the Apache directory, create the main execution job for Apache and PHP.

Create Apache & PHP task

Create the main execution task for Apache and PHP, inside the Apache directory

mkdir tasks && cd tasks
vim main.yml

Type in  the content below in the 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 and PHP Packages 
apt: name={{ item }} update_cache=yes state=latest loop: [ 'apache2', 'php', 'php-mysql', 'libapache2-mod-php' ]

– 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

# PHP Info Page
- name: Sets Up PHP Info Page
template:
src: "files/info.php.j2"
dest: "/var/www/{{ http_host }}/info.php"

- name: Reload Apache
service:
name: apache2 s
tate: reloaded

- name: Restart Apache s
ervice: name: apache2
state: restarted

Create Apache Handlers

Add the Apache Handlers. This is best done in the Apache role directory:

mkdir handlers && cd handlers
vim main.yml

For the Apache handlers, add the following command line;

---
- name: Reload Apache
service:
name: apache2
state: reloaded

- name: Restart Apache
service:
name: apache2
state: restarted

Add Apache Files

Firstly, create the files required to generate the VirtualHost and the info.php index file. This is done in the Apache role directory as well.

mkdir files && cd files

After that create the VirtualHost file by typing in the following command for an easy installation of Ansible lamp on Debian:

vim apache.conf.j2
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 V
irtualHost

Create the PHP test file by typing in the following command:

$ vim info.php.j2;

After that, we have to ensure that the Directory tree looks as shown below:

../apache$ tree

├── files
│   ├── apache.conf.j2
│   └── info.php.j2
├── handlers
│   └── main.yml
└── tasks
└── main.yml

3 directories, 4 files

Create MariaDB Role

Create a MariaDB role for configuring up the Ansible lamp on Debian. In the roles directory, create a directory for the MariaDB task.

mkdir -p mariadb/tasks && cd mariadb/tasks

Following that, we must write a configuration file for the MariaDB task:

vim main.yml -
-- - 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
loop: [ ‘aptitude’ ]

# 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 }}"

Following the process, we must ensure that the structure for the Ansible roles looks like this for ensuring a successful installation of the Ansible lamp on Debian:

../roles$ tree
.
├── apache
│   ├── files

│   ├── apache.conf.j2
│   │   └── info.php.j2
│   ├── handlers
│   │   └── main.yml │
└── tasks
│   └── main.yml
└── mariadb
└── tasks
└── main.yml
6 directories, 5 files

Create the Ansible Playbook

In the parent working directory, construct a playbook that makes use of the roles we just built above.

$ vim lampstack.yml
---
- name: configure lamp
hosts: lampstack
become: yes
become_method: sudo
vars_files:
- vars/default.yml
roles:
- apache
- mariadb

This should apply to the full configuration tree starting with the working directory:

../ansible_playbook$ tree
.
├── hosts
├── lampstack.yml
├── roles
│   ├── apache
│   │   ├── files
│   │   │   ├── apache.conf.j2
│   │   │   └── info.php.j2
│   │   ├── handlers
│   │   │   └── main.yml
│   │   └── tasks
│   │   └── main.yml │
└── mariadb
│   └── tasks │
└── main.yml
└── vars └
── default.yml
directories, 8 files

30 Run Ansible Playbook to install LAMP Stack on Debian

Once the environment is ready, we will run the playbook to install the LAMP stack on Debian:

$ ansible-playbook -i hosts lampstack.yml -u remote-user

Replace the remote-user> parameter with the SSH user we created. The configuring process should begin. If everything goes well, we should get something like this:

PLAY [configure lampl ***********************************************************************************

TASK Gathering Facts ok: [192.168.100.227] ***********************************************************************************

TASK [apache2 : Install prerequisites] ***********************************************************

ok: [192.168.100.227] => (item=aptitude) TASK (apache2 : Install Apache and PHP Packages] ** ok: [192.168.100.227] => (item=apache2) ok: [192.168.100.227] => (item=php) ok: [192.168.100.227] => (item=php-mysql) ok: [192.168.100.227] => (item=libapache2-mod-php) TASK [apache2 : Create document root) ****************************************************************** ok: [192.168.100.227] TASK (apache2 : Set up Apache virtualhost] ********* changed: (192.168.100.227] TASK Tapache2: Enable new sitel *********************************************************************** changed: (192.168.100.227] TASK [apache2 : Disable default Apache site] **************** changed: (192.168.100.227] TASK (apache2 : UFW – Allow HTTP on port 80] ******** changed: (192.168.100.227] TASK Lapache2: Sets Up PHP Info Pagel

***************************************************************** changed: (192.168.100.227] TASK Tapache2: Reload Apachel ************************************************************************* changed: (192.168.100.227] TASK [apache2 : Restart Apachel ************************************************************************ changed: (192.168.100.227] TASK (mariadb : Install prerequisites] **** ok: [192.168.100.227] => (item=aptitude) TASK (mariadb : Install MariaDB Packages] ********** changed: (192.168.100.227] => (item=mariadb-server) changed: (192.168.100.227] => (item=python3-pymysql) TASK mariadb : Start MariaDB servicel ***************************************************************** changed:

(192.168.100.227] TASK mariadb : Sets the root passwordl **************************************************************** [WARNING]: Module did not set no_log for update_password changed: (192.168.100.227] TASK (mariadb : Removes all anonymous user accounts] ****** ok: [192.168.100.227] TASK (mariadb : Removes the MySQL test database] ************* ok: [192.168.100.227] RUNNING HANDLER [apache2 : Reload Apache] ***** changed: (192.168.100.227] PLAY RECAP ***************************************** ************************************************* 192.168.100.227 : ok=18 changed=11 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

Our installation went well. We can now see if the browser can connect to the server.

[Need assistance with similar queries? We are here to help]

Conclusion

To conclude we have now learned more about how to set up the ansible lamp Debian. We have learned more about the necessary requirements and the various configurations steps to set this up with the assistance of our Server management support services.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.