Last updated on January 4th, 2023 at 11:20 am

This tutorial covers step by step instruction on installing Apache web server 2.4.51 and configure PHP 7.2 or PHP 8.0 in EC2 instance launched from Amazon Linux 2 AMI . By default if you try to install PHP via yum in Amazon Linux 2 it will install PHP version 5.4 or similar. You need to make use of Amazon Linux Extras repositories to get the latest versions of PHP packages. We will be covering all those in detail below. I am going to install PHP 8.0 since that is the latest version available as of Jan 18th 2022. Same steps can be followed for PHP 7.2 as well.

Table of Contents

Launch EC2 Instance


Launch an EC2 instance by selecting Amazon Linux 2 AMI, I have selected AL 2 with  Kernel 5.10 . This is the highest available at the time this tutorial was documented. Make sure to configure the correct Security Group with port 22 open to your own IP address.

Note: In case if you are unable to login to the instance using SSH even after selecting the option of “My IP” while launching instance inside the security group, then cross check whether the IP you have added matches https://checkip.amazonaws.com/ . This happened to me and I had to eventually add the IP shown by https://checkip.amazonaws.com/ to my security group to login via SSH.

Install Apache


Install HTTPD package (httpd-2.4.51-1.amzn2.x86_64 is the package I installed, latest version available as of Jan 18th 2022)

sudo yum install httpd -y

Start the web server

sudo systemctl start httpd

Check the status and confirm web server is running.

# sudo systemctl status httpd
● httpd.service - The Apache HTTP Server   
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)   
Active: active (running) since Tue 2022-01-18 15:44:03 UTC; 827ms ago     
Docs: man:httpd.service(8) 
Main PID: 3434 (httpd)  
Status: "Processing requests..."   
CGroup: /system.slice/httpd.service           
├─3434 /usr/sbin/httpd -DFOREGROUND           
├─3435 /usr/sbin/httpd -DFOREGROUND           
├─3436 /usr/sbin/httpd -DFOREGROUND           
├─3437 /usr/sbin/httpd -DFOREGROUND           
├─3438 /usr/sbin/httpd -DFOREGROUND           
└─3439 /usr/sbin/httpd -DFOREGROUND
Jan 18 15:44:03 ip-xxx-xx-78-xxx.ec2.internal systemd[1]: Starting The Apache HTTP Server...

Let us check the version details of the Apache server installed

# httpd -V
Server version: Apache/2.4.51 ()
Server built:   Oct  8 2021 22:03:47
Server's Module Magic Number: 20120211:118
Server loaded:  APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture:   64-bitServer 
MPM:     prefork  
threaded:     no   
forked:     yes (variable process count)
Server compiled with.... 
-D APR_HAS_SENDFILE 
-D APR_HAS_MMAP

Install PHP

Run the command to install PHP 8.0 using amazon-linux-extras command (Output truncated)

# sudo amazon-linux-extras install php8.0

Installing php-pdo, php-fpm, php-mysqlnd, php-cli
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-kernel-5.10 amzn2extra-php8.0
Transaction Summary
Installed:  
php-cli.x86_64 0:8.0.8-1.amzn2                    php-fpm.x86_64 0:8.0.8-1.amzn2  
php-mysqlnd.x86_64 0:8.0.8-1.amzn2                php-pdo.x86_64 0:8.0.8-1.amzn2

Dependency Installed:  libzip.x86_64 0:1.3.2-1.amzn2.0.1               php-common.x86_64 0:8.0.8-1.amzn2    

Note: If you would like to install PHP 7.2 replace php8.0 in the above command with php7.2, The command looks like “sudo amazon-linux-extras install php7.2”  

Confirm PHP version

# php -v
PHP 8.0.8 (cli) (built: Jul  7 2021 17:35:32) ( NTS )Copyright (c) The PHP GroupZend Engine v4.0.8, Copyright (c) Zend Technologies

If you have a Ubuntu server and like to install SSL certificate in EC2 instance running on Ubuntu, Check this out.

Enable PHP

Restart Apache for the PHP configuration to take effect.

#sudo systemctl restart httpd

Test PHP

Now let us try to create a file named php_info.php and run it to see whether PHP 8.0 is configured with Apache 

[root@ip]# cd /var/www/html/
[root@]# cat php_info.php
<?php
phpinfo();
?>
[root@ip]#

Load the above file by hitting http://<YOUR_INSTANCE_PUBLIC_IP>/php_info.php .
You should get a PHP version web page like below describing your PHP 8.0 information details.

If you have installed PHP 7.2 instead of PHP 8.0 then that specific PHP version details will be shown.
Note: PHP is installed as fast CGI. In case if you are facing issues while loading the above PHP page make sure to check status of php-fpm using systemctl.

Leave a Reply

Your email address will not be published. Required fields are marked *