Exploring Amazon EC2 and Installing a LAMP Server on Amazon Linux 2023

Amudha Balamurugan
5 min readOct 13, 2023

--

Welcome to Week 1 of the 12-Week AWS Workshop Challenge!

In this article, we will kick off your journey into the world of Amazon Web Services (AWS) by delving into Elastic Compute Cloud (EC2) instances and setting up a LAMP (Linux, Apache, MySQL, PHP) server on Amazon Linux 2023. By the end of this article, you’ll have a fundamental understanding of how EC2 works and how to deploy a web server using Amazon Linux.

What is Amazon EC2?

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable computing capacity in the cloud. It allows you to run virtual servers, known as instances, on demand. EC2 instances are highly configurable and can be tailored to meet various computing needs, from small-scale web hosting to complex, high-performance computing tasks.

It provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 reduces hardware costs so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. You can add capacity (scale up) to handle compute-heavy tasks, such as monthly or yearly processes, or spikes in website traffic. When usage decreases, you can reduce capacity (scale down) again.

The following diagram shows the basic architecture of an Amazon EC2 instance deployed within an Amazon Virtual Private Cloud (VPC). In this example, the EC2 instance is within an Availability Zone in the Region. The EC2 instance is secured with a security group, which is a virtual firewall that controls incoming and outgoing traffic. A private key is stored on the local computer and a public key is stored on the instance. Both keys are specified as a key pair to prove the identity of the user. In this scenario, the instance is backed by an Amazon EBS volume. The VPC communicates with the internet using an internet gateway.

Launch an EC2 Instance:

  1. Launch an EC2 instance by logging into the console and choosing the free tier default Linux machine with the proper security group to allow SSH (port 22), HTTP (port 80), and HTTPS (port 443) connections.
  2. Connect to your instance through Instant Connect.

Install LAMP on EC2

Installing the LAMP server on an EC2 instance involves four steps,

  1. Preparing the LAMP server
  2. Testing the LAMP Server
  3. Securing the database server
  4. Installing phpMyAdmin

Prepare the LAMP Server:

This involves updating the instance and installing the latest versions of Apache web server, MariaDB server, PHP packages and its dependencies on Amazon Linux 2023.

Then start the Apache web server and configure it to start at each system boot. Test your web server. In a web browser, type the public DNS address (or the public IP address) of your instance. If there is no content in /var/www/html, you should see the Apache test page, which will display the message "It works!".

This can happen only if the installation is clean and security groups inbound rules to allow HTTP traffic at port 80 was set properly.

Apache httpd serves files that are kept in a directory called the Apache document root. The Amazon Linux Apache document root is /var/www/html, by default owned by the root.

To allow the ec2-user account to manipulate files in this directory, you must modify the ownership and permissions of the directory. There are many ways to accomplish this task. In this tutorial, you add ec2-user to the apache group to give the apache group ownership of the /var/www directory and assign write permissions to the group.

Test the LAMP server:

If your server is installed and running, and your file permissions are set correctly, your ec2-user account should be able to create a PHP file in the /var/www/html directory that is available on the internet.

Create a PHP file in the Apache document root.In a web browser, and type the URL of the file that you just created. This URL is the public DNS address of your instance followed by a forward slash and the file name. For example

http://my.public.dns.amazonaws.com/phpinfo.php

You should see the PHP information page:

Delete the phpinfo.php file. Although this can be useful information, it should not be broadcast to the internet for security reasons. You should now have a fully functional LAMP web server. If you add content to the Apache document root at /var/www/html, you should be able to view that content at the public DNS address for your instance.

Secure the database server:

The default installation of the MariaDB server has several features that are great for testing and development, but they should be disabled or removed for production servers. This can be achieved by setting a root password for MariaDB. Setting a root password for MariaDB is only the most basic measure for securing your database. When you build or install a database-driven application, you typically create a database service user for that application and avoid using the root account for anything but database administration. By doing so you can also remove the anonymous user accounts, disable remote root login, remove the test database, reload the privileged tables and save your changes.

Install phpMyAdmin:

This involves installing the required dependencies for PHP. Restart the Apache server and navigate to the Apache document root at /var/www/html. Select a source package for the latest phpMyAdmin release from https://www.phpmyadmin.net/downloads and install it at /var/www/html . Make sure mariaDB server is up and running.

In a web browser, type the URL of your phpMyAdmin installation. This URL is the public DNS address (or the public IP address) of your instance followed by a forward slash and the name of your installation directory. For example:

You should see the phpMyAdmin login page:

Log in to your phpMyAdmin installation with the root user name and the MySQL root password you created earlier.

Ta Da… phpMyAdmin is up and running !

The detailed installation commands and step-by-step guide are available here for your reference. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2023.html

--

--