⚡ 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 .

Overview

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).
  • 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.
  • After all was done, I pinged the ansible inventory to be sure all configurations are set properly
  • Create a directory for the ansible.
  • Cd into the ansible directory.
  • Created a playbook to install apache, set the time zone to Africa/lagos and install php then ran a check on the playbook.
  • Executed the playbook file.
  • Check the result in the target machine to see if the installations were deployed, and check the apache service to confirm it is working fine.
  • Create and edit an index.php file.
  • Using ansible playbook, I copied 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 my 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

You might also like...
The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

The library provides convenient access to the Epoint.az API from applications written in the PHP language.

Tural/Epoint library/SDK The library provides convenient access to the Epoint.az API from applications written in the PHP language. It includes a pre-

A simple API documentation package for Laravel using OpenAPI and Redoc

Laravel Redoc Easily publish your API documentation using your OpenAPI document in your Laravel Application. Installation You can install this package

It’s a bot using simple feature - jangan keseringan make (haram cok)

It’s a bot using simple feature - jangan keseringan make (haram cok)

It’s a bot using simple feature - jangan keseringan make (haram cok)

Indodax-machine It’s a bot using simple feature - jangan keseringan make (haram cok) Example of Request PHP (API-PANAS) Get Info Sample code below :

Laravel Package for 1APP. Learn how to integrate our APIs to build a web or mobile integration to send and accept payments for your application and businesses.
Laravel Package for 1APP. Learn how to integrate our APIs to build a web or mobile integration to send and accept payments for your application and businesses.

1APP Laravel Library Learn how to integrate our APIs to build a web or mobile integration to accept payments, make payment of Bills and as well custom

It's a PHP Application to simplify working with Google Sheets SDK for php.

About GoogleSheetsPHP It's a PHP Application to simplify working with Google Sheets SDK for php. Note: i used Slim 3 to construct the application but

Embed live members' count from all your Discord server in your WordPress website

Live Members Counter For Discord This is the repository for the WordPress plugin: Live Members Counter for Discord Live Members Counter For Discord is

Generate pretty release changelogs using the commit log and Github API.

zenstruck/changelog Generate pretty release changelogs using the commit log and Github API. Changelog entries are in the following format: {short hash

Owner
Bukola Testimony
HR || Cloud Engineering || Tech is fun😘
Bukola Testimony
A Laravel package to help integrate Shopware PHP SDK much more easier

Shopware 6 Laravel SDK A Laravel package to help integrate Shopware PHP SDK much more easier Installation Install with Composer composer require sas/s

Shape & Shift 16 Nov 3, 2022
A links dashboard which can be integrated via HTML into various other systems.

quickdash Newest QuickDash version. Combines the API and Client repositories. Requirements PHP version 7.4 - https://www.php.net/ Composer - https://g

Hugo Soares 0 Aug 11, 2022
Official repository of the AWS SDK for PHP (@awsforphp)

AWS SDK for PHP - Version 3 The AWS SDK for PHP makes it easy for developers to access Amazon Web Services in their PHP code, and build robust applica

Amazon Web Services 5.7k Jan 1, 2023
AWS Instance Connect PHP

This tool lets you SSH into AWS EC2 instances with nothing but your AWS IAM credentials that you probably already have in your home directory if you work with AWS.

Henry Paradiz 7 Apr 14, 2022
PHP library to use IOTA REST API to help node management and tangle queries

iota.php About PHP library to use IOTA REST API to help node management and tangle queries. Please be aware that this library is in an early developme

IOTA Community 45 Dec 13, 2022
AsyncAws Core - shared classes between all AWS services. It also contains the STS client to handle authentication.

AsyncAws Core The repository contains shared classes between all AWS services. It also contains the STS client to handle authentication. Install compo

Async AWS 54 Dec 14, 2022
Toxiproxy PHP Client - Toxiproxy makes it easy and trivial to test network conditions, for example low-bandwidth and high-latency situations

Toxiproxy makes it easy and trivial to test network conditions, for example low-bandwidth and high-latency situations. toxiproxy-php-client includes everything needed to get started with configuring Toxiproxy upstream connection and listen endpoints.

Adrian Parker 29 Jun 24, 2022
This package help you build your REST API documentation.

Laravel API Doc This package help you build your REST API documentation. Installation You can install the package via composer: composer require axeld

Axel 2 May 19, 2022
OpenDialog is a Conversational Experience Platform - the starting point for your own conversational applications.

OpenDialog - open-source conversational application platform OpenDialog enables you to quickly design, develop and deploy conversational applications.

OpenDialog 252 Dec 28, 2022
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.

DeviceDetector Code Status Description The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv

Matomo Analytics 2.4k Jan 9, 2023