Deploy LAMP application on AWS
Tags: devops, linux, php
Published: 2023-10-04 13:32:10

In this article, you will learn how to implement a robust and secure LAMP (Linux, Apache, MySQL, PHP) stack on AWS, a cloud computing platform that provides powerful infrastructure and scalability, enabling you to deploy and manage web applications with ease and efficiency. I'll guide you through each step of setting up this popular web development environment on AWS, from configuring the Linux server to installing Apache, MySQL, and PHP, ensuring your web applications run smoothly and securely in the cloud. Whether you are a seasoned developer or just starting your journey into web development and cloud computing, this comprehensive guide will help you harness the full potential of AWS for hosting your LAMP-based projects.

## Overview
**LAMP** is an acronym of four software technologies that developers use for building websites and web applications. It is denoted from LINUX an operating system, Apache a web server, mySQL a database server, and PHP, Python, and Perl a server-side programming language. LAMP is a stack

**AWS** means Amazon Web Services (AWS) is a secure cloud services platform, offering compute power and database storage and it helps Running web and application servers in the cloud to host dynamic websites. I'll be using a cloud server called Ec2 server which will enable us to use a virtual server and it offers a free tier account that we are going to leverage for this project.

**EC2** means elastic compute cloud. it is a virtual server that provides secure, resizable computing capacity in the cloud.

**Apache server** An open-source web server software that serves web content to clients, handling requests and delivering web pages, making it a fundamental component of web hosting and website delivery.

**Security group** acts as a firewall that controls the traffic allowed to and from the resources in your virtual private cloud (VPC).

**DBMS** It means Database Management System, which is software that efficiently and securely manages the storage, retrieval, and organization of data in a database.

## Prerequisites
In order to proceed, I believe the reader has an AWS account because you will need an AWS account and a virtual server with Ubuntu Server OS else <a href="https://portal.aws.amazon.com/billing/signup#/start/email">Click here</a> to create an account. Stay calm and Let's make our hands dirty.

**Create a Ec2 instance**
Let's create a virtual server and install an Ubuntu OS on it. To create an instance below is the process.

Firstly, we log on to our account, create an ec2 instance, install Ubuntu OS, and save your key pair(.pem) securely. <a href="https://www.techtarget.com/searchcloudcomputing/tutorial/How-to-create-an-EC2-instance-from-AWS-Console">Click here</a> for more details. or follow this below.

After, an Ec2 instance is created then let's connect to the instance. if you are using Windows you connect using a tool called Putty and Mac users use their terminals to connect to their instance using SSH command. I'm a Mac user <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/putty.html">Click here</a> for Windows users.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orbu7ehhikax8lorosyz.png)

## Connect to Ec2 instance
a. Open a terminal and locate the downloaded key pair file using this Linux command cd and your file directory.

```
$ cd Downloads
```


b. Change permissions for the private key file, otherwise you can get an error

```bash
$ chmod 400 <private_file.pem>
```

c. Connect to the instance
```bash
$ ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>
```

## Install Apache and update firewall

To install the Apache server we will use the Ubuntu package manager `apt`



```bash
#update a list of packages in the package manager
sudo apt update

#run apache2 package installation
sudo apt install apache2
```
To ensure the apache2 is installed, use

```bash
sudo systemctl status apache2
```
If the below image is your result then you are on the track. You have launched your web server. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fakmfdb649toqa0lmh13.png)

Let's update the firewall by configuring our security group inbound rules on the ec2 instance.

## Configure EC2 instance  security inbound rules

When the instance is created, we have a default TCP rule on port 22 opened which is useful for SSH connection to a terminal. In order to ensure that our webpage are being accessed on the internet, we need to open a TCP port 80 inbound rule.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49dce03to7gxt5m8mbt6.gif)

To check the accessibility of our web server on the internet, we curl the IP address/DNS name of our localhost.

```bash
 curl http://localhost:80
or
 curl http://127.0.0.1:80 
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri0ltk43f5y8vprxeryb.png)

To see if our web application server can respond to requests, use the public ip address of our instance on a web browser. http://<Public_IP_Address>:80 or without the port 'http://<Public_IP_Address>'

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eg3b7b42odvef83tfomn.png)

## Installing MySQL
To install mysql server we use the command below

```bash
$ sudo apt install mysql-server
```

## Installing PHP
To install PHP, these 3 packages need to be installed together
```bash
$ sudo apt install php libapache2-mod-php php-mysql
```
once the installation is done use the command below to ensure it is installed and check the version 

```bash
$ php -v
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zw9ga7bckpphzt0om1du.png)


## Deploy your PHP website

Create a new directory <directory_name>

```bash
$ sudo mkdir /var/www/<directory_name>
```

Assign ownership of the directory to your current system user
```bash
$ sudo chown -R $USER:$USER /var/www/<directory_name>
```

Create and open a new configuration file in Apache’s sites-available directory using your preferred command-line editor

```bash
$ sudo vi /etc/apache2/sites-available/<directory_name>.conf
```

The command above will create a new file and you will paste this code into it 

```bash
<VirtualHost *:80>
    ServerName proj<directory_name>ectlamp
    ServerAlias <directory_name> 
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/<directory_name>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```

To save and close the file, follow the steps below:
<ul>
<li>Hit the esc button on the keyboard</li>
<li>Type :</li>
<li>Type wq. w for write and q for quit</li>
<li>Hit ENTER to save the file</li>
</ul>

You can use `ls` to list the document in the directory 

Run esc `:wq` to save and terminate vi editor.

Run `sudo a2ensite projectlampstack` to activate the server block.

Run `sudo a2dissite 000-default` to deactivate the default web server block that comes with Apache on default.

Reload the apache2 server `sudo systemctl reload apache2`


Create an index file inside your directory `/var/www/<folder_name>`
```bash
$ cd /var/www/<folder_name>
$ touch index.html
```

Write your HTML code into the file using `sudo vi <filename>`

```bash
<!DOCTYPE html>
<html>
<head>
    <title>01. LAMP stack implementation</title>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>
```
To save and close the file, follow the steps below:
<ul>
<li>Hit the ESC button on the keyboard</li>
<li>Type :</li>
<li>Type wq!</li>
<li>Hit ENTER to save the file</li>
</ul>

Go to your browser and try to open your website URL using your public IP address:

`http://<Public-IP-Address>:80` or `http://<Public-IP-Address>`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0l0r2gitnthnu3380vrg.png)

<h3>Enable PHP on the website</h3>

By default, in Apache, if both an index.html and an index.php file exist in a directory, the server will prioritize displaying the index.html file over the index.php file. This feature can be advantageous when configuring maintenance pages for PHP applications. You can achieve this by temporarily creating an index.html file with informative content for visitors. Since the index.html page takes precedence over the index.php page, it becomes the primary landing page for your application during maintenance. Once the maintenance work is completed, you can either rename or delete the index.html file from the document root, restoring the regular application page.

If you wish to modify this behavior and give priority to the index.php file, you'll need to edit the `/etc/apache2/mods-enabled/dir.conf file`. Inside this file, you can adjust the order in which the index.php file is listed within the DirectoryIndex directive.

```bash
$ sudo vim /etc/apache2/mods-enabled/dir.conf
```


```bash
<IfModule mod_dir.c>
     #Change this:
    #DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
    #To this:
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
```


After we save the file, we need to reload the Apache

```bash
$ sudo systemctl reload apache2
```

Create a PHP file to test if PHP is set correctly and configured 

```bash
$ vim /var/www/<folder_name>/index.php
```

Write your PHP code and save. Below is a sample code 

```bash
<?php
phpinfo();
```
Go to your browser and open your website URL using your public IP address:

```bash
http://<Public-IP-Address>:80 or http://<Public-IP-Address>
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0xc42zxixrabppqntdyl.png)

Congratulations on successfully deploying your LAMP stack on AWS! You've achieved a significant milestone.