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.
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.
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.
If the instance also runs Apache, add the web server packages and start the service.
Place a small test file in the web root to confirm that Apache and PHP are wired together correctly.
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.
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-extrasis 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.
Related reading
- how to install pip with yum on EC2
- How to Install Postgresql 11 in Amazon Linux AMI?
- How to integrate API Gateway with s3 in CDK
- How to kill/terminate a running AWS Lambda function?
- How to know RDS free storage
- How to label Kubernetes node?
- How to launch local DynamoDB programmatically?
- How to list _all_ objects in Amazon S3 bucket?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.