Tuesday, August 29, 2023

Install Apache using Ansible

  Installing Apache in Remote hosts Using Ansible Playbook.

Ansible 

Create a yaml file as follows

cat apache.yml
---
 - hosts: localhost
   become: yes
   become_method: sudo
   become_flags: -s
   become_user: root
   tasks:
   - name: Install Apache
     apt:
       name: apache2
       state: present
       update_cache: true 

or

Syntax 2:

cat apache_1.yml
---
 - hosts: localhost
   become: yes
   become_method: sudo
   become_flags: -s
   become_user: root
   tasks:
   - name: Install Apache
     apt: name=apache2 state=present update_cache=yes
 

and run the apache.yml playbook

ansible@ansible-VirtualBox:~/Desktop$ ansible-playbook -v apache.yml
Using /etc/ansible/ansible.cfg as config file

PLAY [localhost] ******************************************************************************************************************************************************************************************

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

TASK [Install Apache] *************************************************************************************************************************************************************************************
changed: [localhost] => changed=true
  cache_update_time: 1619332181
  cache_updated: true
  stderr: ''
  stderr_lines: <omitted>
  stdout: |-
    Reading package lists...
    Building dependency tree...
    Reading state information...
    Suggested packages:
      apache2-doc apache2-suexec-pristine | apache2-suexec-custom
    The following NEW packages will be installed:
      apache2
    0 upgraded, 1 newly installed, 0 to remove and 251 not upgraded.
    Need to get 0 B/95.6 kB of archives.
    After this operation, 542 kB of additional disk space will be used.
    Selecting previously unselected package apache2.
    (Reading database ... (Reading database ... 5%(Reading database ... 10%(Reading database ... 15%(Reading database ... 20%(Reading database ... 25%(Reading database ... 30%(Reading database ... 35%(Reading database ... 40%(Reading database ... 45%(Reading database ... 50%(Reading database ... 55%(Reading database ... 60%(Reading database ... 65%(Reading database ... 70%(Reading database ... 75%(Reading database ... 80%(Reading database ... 85%(Reading database ... 90%(Reading database ... 95%(Reading database ... 100%(Reading database ... 271653 files and directories currently installed.)
    Preparing to unpack .../apache2_2.4.46-1ubuntu1_amd64.deb ...
    Unpacking apache2 (2.4.46-1ubuntu1) ...
    Setting up apache2 (2.4.46-1ubuntu1) ...
    Enabling module mpm_event.
    Enabling module authz_core.
    Enabling module authz_host.
    Enabling module authn_core.
    Enabling module auth_basic.
    Enabling module access_compat.
    Enabling module authn_file.
    Enabling module authz_user.
    Enabling module alias.
    Enabling module dir.
    Enabling module autoindex.
    Enabling module env.
    Enabling module mime.
    Enabling module negotiation.
    Enabling module setenvif.
    Enabling module filter.
    Enabling module deflate.
    Enabling module status.
    Enabling module reqtimeout.
    Enabling conf charset.
    Enabling conf localized-error-pages.
    Enabling conf other-vhosts-access-log.
    Enabling conf security.
    Enabling conf serve-cgi-bin.
    Enabling site 000-default.
    Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service -> /lib/systemd/system/apache2.service.
    Created symlink /etc/systemd/system/multi-user.target.wants/apache-htcacheclean.service -> /lib/systemd/system/apache-htcacheclean.service.
    Processing triggers for systemd (246.6-1ubuntu1) ...
    Processing triggers for man-db (2.9.3-2) ...
    Processing triggers for ufw (0.36-7) ...
  stdout_lines: <omitted>

PLAY RECAP ************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

ansible@ansible-VirtualBox:~/Desktop$ ps -ef |grep apache
root        7629       1  0 11:59 ?        00:00:00 /usr/sbin/apache2 -k start
www-data    7631    7629  0 11:59 ?        00:00:00 /usr/sbin/apache2 -k start
www-data    7632    7629  0 11:59 ?        00:00:00 /usr/sbin/apache2 -k start
ansible     9021    1754  0 12:00 pts/0    00:00:00 grep --color=auto apache


Check the host on which Apache is running by using hostname - I command and then launch a browser and connect to the IP addresses mentioned

ansible@ansible-VirtualBox:~/Desktop$ hostname -I
10.0.2.15 172.17.0.1 

Launched the first IP and have set the following page in the index.html


you can check the status of apache service

ansible@ansible-VirtualBox:~/Desktop$ ps -ef|grep apache
root         639       1  0 10:52 ?        00:00:00 /usr/sbin/apache2 -k start
www-data     640     639  0 10:52 ?        00:00:00 /usr/sbin/apache2 -k start
www-data     641     639  0 10:52 ?        00:00:00 /usr/sbin/apache2 -k start
ansible     4361    1754  0 11:33 pts/0    00:00:00 grep --color=auto apache

You can configure your own page by following the below link

https://ubuntu.com/tutorials/install-and-configure-apache#1-overview

For now I'll set the apache index.html back 

Go to /var/www/html and rename the current index.html and move the original one back into the place

ansible@ansible-VirtualBox:/var/www/html$ ls
index.html  index.html_bak_24Apr21
ansible@ansible-VirtualBox:/var/www/html$ sudo mv index.html index.html_25Apr21
ansible@ansible-VirtualBox:/var/www/html$ sudo mv index.html_bak_24Apr21 index.html

Restart the services:

ansible@ansible-VirtualBox:/var/www/html$ systemctl reload apache2

Relaunch the IPs in the browser and you will see the original apache page

For port configuration, refer to: https://www.scaleway.com/en/docs/how-to-install-apache-on-ansible/