Skip to content

⚡ Setting up Apache and PHP on AWS with Ansible Playbook Using Ubuntu 22.04 AMI. Ansible is a simple and powerful automation engine. It is used to help with configuration management, application deployment, and task automation. It makes your applications and systems easier to deploy and maintain .

Notifications You must be signed in to change notification settings

Bukola-Testimony/Setting-up-Apache-and-PHP-on-AWS-with-Ansible-Using-Ubuntu-22.04-AMI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Ansible-Aws-Ubuntu-Apache-PHP


Table of content

Overview

Setting up Apache and PHP on AWS with Ansible Playbook Using Ubuntu 22.04 AMI as the target machine and Ubuntu 20.04 as the control node. Ansible is a simple and powerful automation engine. It is used to help with configuration management, application deployment, and task automation.


In this write up I will show you how to set up an apache server, and host a simple php file on the server using ansible playbook.


This is done using Ubuntu 22.04 AMI (virtual machine) on AWS.



Process

  • Install ansible on the control node(In this case I used an Ubuntu 20.04 VM created with vagrant on my windows machine).
  • Create ssh key pairs.
  • Import the public key to AWS.
  • Creat an EC2 instance using Ubuntu 22.04 (target machine)
  • Connect with the target machine through ssh then exit.
  • Edit the /etc/ansible/hosts file adding the IP address of the target machine.
  • Ping the ansible inventory to be sure all configurations are set properly
  • Create a directory for the ansible.
  • Cd into the ansible directory.
  • Create a playbook to install apache, set the time zone to Africa/lagos and install php then run a check on the playbook.
  • Execute the playbook file.
  • Check the result in the target machine to see if the installations were deployed properly, and check the apache service to confirm it is working fine.
  • Create and edit an index.php file.
  • Using ansible playbook, copy the index.php file from the localhost to the remote server.
  • Edit the apache config file on the remote server.
  • Check the rendered page on your browser.


The following are the links to Ansible playbook and index.php file.



To Install ansible on the control node (In this case I used an Ubuntu 20.04 VM created with vagrant).

Run the following command:

$ sudo apt update
$ sudo apt install -y software-properties-common python-apt 
$ sudo apt install -y ansible
$ ansible --version



Create ssh key pairs

Run the following command :

$ ssh-keygen -m rsa PEM

Note: when prompted give it a passphrase of a minimum of 5 characters (required)

change the private key to an extention of PEM.

$ mv /home/vagrant/.ssh/vagrantkeys /home/vagrant/.ssh/vagrantkeys.pem

Now, log into AWS console and import the public keys.

Click on servicesc, type EC2 in the searchbox and select EC2 from the services menu.

On EC2 dashboard, click on keypairs.

Click on Actions and select "import key pair" from the drop-down menu.

Enter the name of your keys

copy your public keys here.

Click on import key pair

Create an EC2 instance using Ubuntu 22.04 (target machine)

Click on instances

Click on launch instances at the top of the console.

Give your instance a name

Scroll down to Amazon machine images (AMI). Select ubuntu. Leave it at the free tier eligible. (If you are on free-tier, preferable choose machines that have free tier eligibility to save cost.)

Scroll down to Instance type ans choose t2.micro

Scroll down to Key pair (login) Enter the name of our key pair.

Scroll down to Network settings, go to "create security groups"click on it.

Scroll down and check the boxes to allow ssh traffic and other ports.

Scroll down to summary.Enter the number of instances you want to create. Review your choices. Click on Launch instance.

Click on "view all instances"

When your instance has been successfully created, click on "connect" at the top of the dashboard.

Click on SSH client. Copy the example below.

Paste the command on your terminal. This allows you to connect with your EC2 instance using ssh connection.

Note: do this at the ssh path where you have your keypairs stored so that the connecton can discover the keys.

After successful connection. Exit.

Edit the /etc/ansible/hosts file adding the IP address of the target marchine.

First,copy the IP address and the user name of your AWS Ubuntu server.

Now Run the following command:

$ sudo nano /etc/ansible/hosts

Edit your host file with the IP address, user name and the path to the key pairs.



Ping the servers to be sure all configurations are set properly. This should return success.

Run the following command:

$ ansible all -m ping
 



Create an ansible playbook to install apache and php on the remote VM. (using ppa:ondrej/php repository)

Run the following command:

$  nano playbook.yml   


Run a check on the playbook to check if the syntax is written correctly. Run this check at the path where you created the playbook.

Run the following command :

$ ansible-playbook playbook.yml --check


To execute the playbook

run the following command:

$ ansible-playbook playbook.yml

Create an index.php file and edit the file.

$  touch index.php   
$  nano index.php   


With ansible playbook, copy the index.php file from localhost(control node) to remote server(target machine). First create ansible playbook. then run a check to see if your playbook sytax is correct before executing the playbook.

Run the following command:

$  touch indexphp.yml  
#To create an index.php file
$  nano indexphp.yml
#To edit the file
$  ansible-playbook indexphp.yml --check
#To check if the playbook syntax is correct.
$  ansible-playbook indexphp.yml
#To execute the playbook


When the playbook has been executed, check from your control node if it was deployed properly on the target machine.

Run the following command:

$  ansible all -m shell -a "ls -al /var/www/html/" -b  


You can execute the index.php file directly from terminal

Run the following command:

$  php index.php


After executing all the playbooks (For the apache2 server ,PHP and index.php file), Copy the IP address of your remote server (Ubuntu AWS AMI) and paste it on your browser. You should see the default index.html file hosted on the server.


Edit the apache config file to host the index.php file instead of the index.html file.

run the following command:

$ sudo nano /etc/apache2/mods-enabled/dir.conf

from the screenshot, note the order of file extentions.


change the order of file extentions to make index.php file come first. Save and close.



Restart the apache2 service and check the status

run the following command:

$ sudo systemctl restart apache2
$ systemctl status apache2


Refresh the web browser.

Screenshot of the rendered page.



Author

About

⚡ Setting up Apache and PHP on AWS with Ansible Playbook Using Ubuntu 22.04 AMI. Ansible is a simple and powerful automation engine. It is used to help with configuration management, application deployment, and task automation. It makes your applications and systems easier to deploy and maintain .

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages