PHP installation
EC2 setup
Amazon Linux
t2.micro instance
AWS tutorial

How to install PHP 7 on EC2 t2.micro Instance running Amazon Linux Distro

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Installing PHP 7 on an EC2 t2.micro instance is mostly an operating-system packaging task, not an EC2-specific one. The important decision is which Amazon Linux generation you are running, because Amazon Linux 2 can still expose PHP 7 packages through extras channels, while Amazon Linux 2023 is aimed at supported PHP 8 releases instead.

Confirm the OS Before Installing Anything

Start by SSHing into the instance and checking the release information. This determines whether a PHP 7 installation path is even realistic.

bash
ssh -i my-key.pem ec2-user@EC2_PUBLIC_DNS
cat /etc/os-release
uname -a

If the machine is Amazon Linux 2, a legacy PHP 7 installation may still be possible. If it is Amazon Linux 2023, the practical answer is usually to upgrade the application to PHP 8 or run the legacy app in a container built from an older base image.

Install PHP 7 on Amazon Linux 2

On Amazon Linux 2, the usual approach is amazon-linux-extras. For a legacy application, PHP 7.4 is the most common branch to target.

bash
1sudo yum update -y
2sudo amazon-linux-extras enable php7.4
3sudo yum clean metadata
4sudo yum install -y php php-cli php-common php-mbstring php-xml php-mysqlnd
5php -v

If the instance also runs Apache, add the web server packages and start the service.

bash
sudo yum install -y httpd
sudo systemctl enable httpd
sudo systemctl start httpd

Place a small test file in the web root to confirm that Apache and PHP are wired together correctly.

bash
echo '<?php phpinfo();' | sudo tee /var/www/html/info.php
curl http://localhost/info.php | head

That test should only be temporary. Remove the info.php file after verification because it exposes configuration details.

Why the Instance Size Matters Less Than You Think

A t2.micro has limited CPU and memory, but PHP installation itself is lightweight. What matters more is what else the instance is doing.

On a small instance, keep the setup conservative:

  • install only the PHP extensions the app actually needs
  • avoid background services you do not use
  • restart only the required daemons after configuration changes

This reduces memory pressure and makes debugging easier.

PHP-FPM Versus Apache Module

Many legacy tutorials assume mod_php with Apache, but some stacks use php-fpm behind Apache or Nginx instead. If your app sits behind Nginx, install php-fpm and configure the web server to pass requests to it.

bash
sudo yum install -y php-fpm
sudo systemctl enable php-fpm
sudo systemctl start php-fpm

The best choice depends on the rest of the stack, not on EC2 itself. The important thing is that the PHP packages and the web server configuration agree on the execution model.

Do Not Ignore Support Status

PHP 7 is end-of-life. That does not stop the package manager from installing it on some older images, but it does change the security discussion. If this is a real application and not a short-lived compatibility task, upgrading to a supported PHP 8 release is the better engineering choice.

That matters even more for Amazon Linux generations. A system that makes PHP 7 easy to install is usually a system that is already older and closer to its own maintenance boundary.

A Safe Legacy Workflow

If the application truly requires PHP 7, the cleanest operational path is often:

  • run it on Amazon Linux 2 only if your organization still supports that image
  • pin the exact PHP branch and extensions you need
  • isolate the workload behind security groups and least-privilege IAM
  • create a migration plan to PHP 8 instead of treating PHP 7 as permanent

That approach acknowledges reality without pretending the legacy runtime is a long-term destination.

Common Pitfalls

The most common mistake is following Amazon Linux 2 instructions on an Amazon Linux 2023 instance. The package naming and availability are different, so the commands fail or install the wrong runtime.

Another issue is installing PHP successfully but forgetting the required extensions. Applications often fail later because mbstring, xml, or database drivers were omitted.

Teams also leave phpinfo() exposed after testing. Remove the file once validation is done.

Finally, do not assume the EC2 instance type is the main constraint. On a t2.micro, the application workload and web server tuning matter more than the package installation itself.

Summary

  • Check the Amazon Linux version first because it determines whether PHP 7 is even available.
  • On Amazon Linux 2, amazon-linux-extras is the usual path for legacy PHP 7 installs.
  • Install only the PHP extensions your application actually needs.
  • Treat phpinfo() as a temporary diagnostic, not a permanent page.
  • If the app still depends on PHP 7, plan the upgrade path instead of normalizing the legacy runtime.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design