DEV Community

MOTY
MOTY

Posted on

Deploy LAMP application on AWS

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 Click here 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. Click here 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 Click here for Windows users.

Image description

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
Enter fullscreen mode Exit fullscreen mode

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

$ chmod 400 <private_file.pem>
Enter fullscreen mode Exit fullscreen mode

c. Connect to the instance

$ ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>
Enter fullscreen mode Exit fullscreen mode

Install Apache and update firewall

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

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

#run apache2 package installation
sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

To ensure the apache2 is installed, use

sudo systemctl status apache2
Enter fullscreen mode Exit fullscreen mode

If the below image is your result then you are on the track. You have launched your web server.

Image description

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

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

 curl http://localhost:80
or
 curl http://127.0.0.1:80 
Enter fullscreen mode Exit fullscreen mode

Image description

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

Image description

Installing MySQL

To install mysql server we use the command below

$ sudo apt install mysql-server
Enter fullscreen mode Exit fullscreen mode

Installing PHP

To install PHP, these 3 packages need to be installed together

$ sudo apt install php libapache2-mod-php php-mysql
Enter fullscreen mode Exit fullscreen mode

once the installation is done use the command below to ensure it is installed and check the version

$ php -v
Enter fullscreen mode Exit fullscreen mode

Image description

Deploy your PHP website

Create a new directory

$ sudo mkdir /var/www/<directory_name>
Enter fullscreen mode Exit fullscreen mode

Assign ownership of the directory to your current system user

$ sudo chown -R $USER:$USER /var/www/<directory_name>
Enter fullscreen mode Exit fullscreen mode

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

$ sudo vi /etc/apache2/sites-available/<directory_name>.conf
Enter fullscreen mode Exit fullscreen mode

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

<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>
Enter fullscreen mode Exit fullscreen mode

To save and close the file, follow the steps below:

  • Hit the esc button on the keyboard
  • Type :
  • Type wq. w for write and q for quit
  • Hit ENTER to save the file

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>

$ cd /var/www/<folder_name>
$ touch index.html
Enter fullscreen mode Exit fullscreen mode

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

<!DOCTYPE html>
<html>
<head>
    <title>01. LAMP stack implementation</title>
</head>
<body>
    <h1>hello world</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

To save and close the file, follow the steps below:

  • Hit the ESC button on the keyboard
  • Type :
  • Type wq!
  • Hit ENTER to save the file

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

Enable PHP on the website

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.

$ sudo vim /etc/apache2/mods-enabled/dir.conf
Enter fullscreen mode Exit fullscreen mode
<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>
Enter fullscreen mode Exit fullscreen mode

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

$ sudo systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

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

$ vim /var/www/<folder_name>/index.php
Enter fullscreen mode Exit fullscreen mode

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

<?php
phpinfo();
Enter fullscreen mode Exit fullscreen mode

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

http://<Public-IP-Address>:80 or http://<Public-IP-Address>
Enter fullscreen mode Exit fullscreen mode

Image description

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

Top comments (0)