Install LAMP Stack on AWS EC2 with Amazon Linux from Start to Finish

Last Updated on

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

How to Install LAMP Stack on AWS EC2 with Amazon Linux

Here are quick steps to install LAMP Amazon EC2

  1. Access your EC2 instance via SSH or Instance Connect from terminal
  2. Start by updating yum: yum update
  3. Verify PHP doesn’t currently exist: php -v
  4. Install PHP if it couldn’t be found: sudo yum install php
  5. Verify php is installed: php -v
  6. Install various important php extensions: sudo yum install php-fpm php-mysqli php-json php-deve
  7. Install Apache web server and wget: sudo yum install httpd wget
  8. Install MySQL: sudo yum install -y mariadb-server
  9. Configure your PHP.ini file and add other required extensions
#Update Linux Packages
sudo yum update -y

#Install PHP
sudo yum install php

#Check PHP version
php -v

#Install important PHP extensions
sudo yum install php-fpm php-mysqli php-json php-deve

#Install Apache Web Server and wget
sudo yum install httpd wget

#Install MariaDB server
sudo yum install -y mariadb-server 

The article is well-beyond just a walkthrough. If you’re interested in learning about the downsides of installing a LAMP stack and the better alternatives, stick to this article until the end.

Prerequisites

The article requires readers to.

  1. Create and run an AWS Linux EC2 instance.
  2. Connect to EC2.
  3. Configure the EC2 security group to allow inbound SSH, HTTP and HTTPs traffic.

With all that set, let’s proceed to the main content.

How to Install LAMP Stack on Amazon EC2?

Installing the LAMP stack on EC2 may sound like an excellent idea, but it has some downsides. Learn more about these downsides and the alternatives here.

Now is the time to walk through the steps to install the required packages for the LAMP stack on AWS EC2.

Update Linux Packages

First things first, let’s update Linux packages as follows.

sudo yum update -y

Install PHP on AWS EC2

The following command will install PHP.

sudo yum install php

Check if PHP is Installed

Here’s how to check if PHP is installed.

php -v

Install PHP extensions on AWS EC2

PHP needs some vital extensions to support web-related requests and responses. The following command installs these.

sudo yum install php-fpm php-mysqli php-json php-deve

Install Apache Server on EC2 Instance

Here’s how to install the Apache web server and the wget extension.

sudo yum install httpd wget

Install MariaDB Server on EC2

Now, run the following command to install software packages for MariaDB, a commercially supported fork version of MySQL.

Note: Installing a database server on EC2 is not recommended. Learn more.

 sudo yum install -y mariadb-server 

Voila! These commands install Apache web server, PHP, and MariaDB on Amazon Linux 2 EC2 instance.

How to Start LAMP Stack on Amazon EC2?

Apache Server on Startup

The following command ensures that the Apache server runs on AWS EC2 whenever it boots up.

sudo systemctl enable httpd

Run Apache Server

Now, run the following command to spin up the Apache web server on Amazon Linux 2 EC2 instance.

sudo systemctl start httpd

Check if Apache Server is Running

Now type the public DNS address of the instance. You will see a test page that signifies the server is working fine.

Apache Test Page
Apache Test Page

How to Setup Permission in Apache Web Server?

To upload files to the server, we need permission to access and manipulate the server’s root folder /var/www.

1. Run the following command to add the user (ec2-user) to the apache group.

sudo usermod -a -G apache ec2-user

2. Restart your EC2 instance.

3. Type groups to ensure the ec2-user is in the apache group

Install LAMP Stack on Amazon EC2
Apache Group

4. Run the following command to change the ownership of the apache group to ec2-users.

sudo chown -R ec2-user:apache /var/www

5. Run the following commands to grant read-write permissions on var/www and its sub-directories.

sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;

6. With that, the ec2-user and other members of the apache group can add, delete or edit files in the root directory of the web server. Therefore, we can now add static HTML files or a dynamic PHP application.

How to Test LAMP Stack on Amazon EC2?

1. Run the following command to copy a line of PHP script to index.php.

echo "<?php echo ‘<h1>Hello FuelingPHP</h1>’ ?>" > /var/www/html/phpinfo.php

2. Now, type the EC2 public DNS address as follows.

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

3. Here’s how we do that on our end.

Apache Server
Apache Server

That’s it, and the Apache server is good to go. Let’s move to the last step and secure the MariaDB server.

How to Secure MariaDB Server?

Although, the following walkthrough helps you to secure the existing MariaDB server on your EC2 instance. But it is generally not recommended to host a database server on an EC2 instance. Learn more about the reasons and alternatives.

This walkthrough helps you configure the MariaDB server.

1. Type the following command to start the database server.

sudo systemctl start mariadb

2. Run the following command to begin configuration.

sudo mysql_secure_installation

3. Provide a root password and press Enter.

Install LAMP Stack on Amazon EC2
MariaDB Password

4. Press Y to remove anonymous users.

Anonymous Users
Anonymous Users

5. Again, Press Y to disallow root login remotely.

6. Press Y to remove the test database server.

Install LAMP Stack on Amazon EC2
Test Database

7. Finally, press Y to reload privilege tables and save the secure configuration.

8. Start the MariaDB server with the following command.

sudo systemctl stop mariadb.

9. Run the MariaDB server when the instance boots up with the following command.

sudo systemctl enable mariadb.

Congratulations! We have been successful in installing LAMP Stack on Amazon EC2.

AWS EC2 Instance with Lamp Stack | But is this the best way?

The main idea here is to install Apache, PHP, and MySQL on the same EC2 instance. Though it may work at a tiny scale, this architecture may have downsides once it comes to serious business.

So, why may it not be the best idea to deploy the LAMP stack on an EC2 instance? Especially if you’re deploying a production-grade application. Why is that? The next section provides some insights about that.

Limitations of installing MySQL on the same Server as Apache and PHP

One obvious downside that would suddenly pop up in your mind is that it will take a lot of resources to manage Apache and Database on one instance, and at times one could bottleneck the other. That’s one main downside, but there is even more to this approach.

Let’s overview some of these downsides.

AWS EC2 is just Computing Capacity

Amazon EC2 is neither a fully-managed server nor a database, which means you’ll be liable to take care of maintenance, security, scaling and fault recovery. EC2 won’t scale automatically based on traffic spikes and doesn’t include an automatic server failover policy.

It is a computing capacity in the AWS cloud. AWS fulfils a user’s need for computing capacity and lets them handle the rest, which means more overhead on the user’s end.

Difficult to Start and Scale

LAMP stack doesn’t come built-in with EC2. You are solely responsible for installing the individual components with the right versions. The setup may sound easy, but unfortunately, it adds much overhead when trying to integrate these. On top of that, you will be managing these in the future, and that too frequently as your network load goes up and beyond.

Another factor is scaling these resources. You won’t be stuck with the same computing and storage capacity forever. Scaling is inevitable in the longer run. Now, scaling these resources is yet another challenge. You will need more time and domain expertise to handle your scaling endeavours.

More Administrative Burden

A database does a lot under the hood. It has an engine that runs on the operating systems, and that engine software needs frequent patching, maintenance and updates. If you opt-in for manually managing all these, you will be doing some work that doesn’t directly add value to your business goals. Besides, manually handling a database overhead will cost you time and money.

On top of that, you are liable to handle server and load balancers too. All of these drifts away you and your team from your main goals.

Availability & Security

These are other major concerns for an application. What if a server or a database goes down? What are the alternatives? Do you follow the best security practices? Do you have resources in different regions to serve your multi-regional users?

All these are critical and unavoidable questions. If you’re up to some serious venture and want to be the next – let’s say a successful SaaS startup, these should always be your top concerns. With that said, how do you ensure a failover policy, security and multi-regional availability? Would you do all these manually?

Even if you do so, would it be beneficial and practical as the user base grows?

Data Redundancy & Recovery

Data storage comes with challenges, and one of them is data redundancy. Some architectures have multiple read replicas and must be in sync with the write databases. Besides, every database adds more administrative burden, so you will probably be investing a lot of time and resources just to keep the architecture intact.

So, with a more manual approach, you’ll be scaling, managing, and glueing these databases together. You’ll always be on the watch to keep these databases up to the mark to continue normal operations – and that is a lot of work.

So, what are the Alternatives?

Most of the cloud providers feature fully-managed services. These services are built to automate and manage these non-functional requirements for you and let you focus on your business goals and objectives. We are focusing on AWS, so let’s see what alternatives we have to deploy a PHP application.

EC2 with AWS Relational Database Service (RDS)

We have just seen the overhead of maintaining and scaling a database. So, hosting an Apache and Database server on one instance is not feasible. One innovative approach could be hosting them on two different EC2 instances, but that won’t spare us all the manual work.

Another approach could be installing an Apache server on EC2 and delegating the database to AWS RDS. RDS is a fully-managed database service from Amazon. It helps you set up, operate and scale your database with a few clicks.

Install LAMP Stack on Amazon EC2
AWS RDS

It features six database engines, including Amazon Aurora, MySQL, MariaDB, Oracle, PostgreSQL, and Microsoft SQL Server. Amazon RDS manages database software, updates, patching and security concerns for you. AWS RDS boasts high availability, data redundancy and uptimes with multi-availability deployments, automated backups and database snapshots.

Learn all about AWS RDS features.

Host and Deploy a Laravel PHP App to AWS Elastic Beanstalk

Elastic Beanstalk is AWS’s platform-as-a-service that deploys applications, orchestrating the underlying resources and helping you in effortless deployments. It automates the task of server configuration, scaling, and load balancing, which is otherwise your responsibility if you go with the former approach of using EC2 instances. 

Deploy PHP App to Elastic Beanstalk
AWS Elastic Beanstalk

Elastic Beanstalk supports many platforms, including .NET, PHP, Python, Go, Docker, and Node. One cool thing is that EBS doesn’t have any additional charges but charges for the underlying resources only.

Learn more about deploying PHP applications to Elastic Beanstalk.

Go Serverless with AWS Fargate

AWS Fargate is a serverless computing service that lets you upload dockerized PHP applications using Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS).

AWS Fargate

With serverless, you don’t have to worry about provisioning, monitoring, and scaling your servers, but all you need is to deploy the containers on Fargate, and the rest is not your headache.

Learn more about AWS Fargate.

Deploy using Platform-as-Service (PaaS)

There are a bunch of platform-as-a-service options available that help you deploy effortlessly. They use the computing resources of the popular platforms but provide a layer of abstraction. With PaaS, you don’t have to worry about server configuration, monitoring, management, and scaling because they are already managed for you – much like Elastic Beanstalk.

To name a few – CloudwaysHerokuLaravel Forge, and Kinsta.

Frequently Asked Questions

What is LAMP Stack?

LAMP is a popular open-source web development stack. LAMP is an acronym, and each letter represents a component. Together these form the building blocks of dynamic web applications. 

  • Linux – The underlying operating system.
  • Apache – An HTTP web server for web-based communication.
  • MySQL – A relational database for data persistence.
  • PHP      – A programming language for dynamic web applications
Install LAMP Stack on Amazon EC2
Courtesy: GeeksBot

This article includes a walkthrough of installing the LAMP stack on Amazon EC2. 

How to Connect to EC2 Instance?

We can connect to EC2 in the following ways.

  1. Using EC2 Instance Connect.
  2. Using an SSH Client.

We’ll use the first option using EC2 Instance Connect.

. Select the EC2 instance and click on Actions > Connect

Install LAMP Stack on Amazon EC2
Connect to EC2 Instance

2. In the EC2 Instance Connect, click Connect.

Connect to EC2
Connect to EC2

3. AWS will connect to the instance, and we will be able to use Linux.

Install LAMP Stack on Amazon EC2
Amazon Linux 2

Note: If you are getting an error while trying to connect, ensure you have the right privileges.

Security Groups in EC2? What are they, and why do I need them?

Security groups act as a firewall for your EC2 instance. It determines what type of traffic should go in and out of the EC2. You can configure security groups to include or exclude certain types of traffic.

For instance, the configuration shown in the above image would allow traffic from SSH, HTTPS and HTTP from everywhere. All other traffic types like FTP or SMTP won’t be able to get in.

Wrap Up

This article demonstrates how to install the LAMP stack on Amazon EC2. It walks through the steps to install the LAMP stack (Apache, PHP, MariaDB) on Amazon Linux 2 EC2. However, the LAMP stack on EC2 may not be a bright idea, and the article has a comprehensive section. This section discusses the limitations of this approach and also the alternatives.

Hope you’ve learned something valuable. Stay tuned on FuelingPHP for more such content.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.

Did you find this article helpful?

Join the best weekly newsletter where I deliver content on building better web applications. I curate the best tips, strategies, news & resources to help you develop highly-scalable and results-driven applications.

Build Better Web Apps

I hope you're enjoying this article.

Get the best content on building better web apps delivered to you.