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

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


You can now use the official php7 packages. Here an easy to follow guide.

1. Install Apache 2.4 and PHP 7.0 on Amazon Linux AMI

# Remove current apache & php sudo yum remove httpd* php*# Install Apache 2.4sudo yum install httpd24# Install PHP 7.0 # automatically includes php70-cli php70-common php70-json php70-process php70-xmlsudo yum install php70# Install additional commonly used php packagessudo yum install php70-gdsudo yum install php70-imapsudo yum install php70-mbstringsudo yum install php70-mysqlndsudo yum install php70-opcachesudo yum install php70-pdosudo yum install php70-pecl-apcu

2. Modify DirectoryIndex to include index.php

sudo nano /etc/httpd/conf/httpd.conf

find this:

<IfModule dir_module>    DirectoryIndex index.html</IfModule>

and modify it to look like this:

<IfModule dir_module>    DirectoryIndex index.html index.php</IfModule>

If a directory contains an index.html and an index.php, the server will serve the index.html with this setup. If you do not want that to happen, you have the following options:

Reverse the order, so index.php is served when both files exist:

 <IfModule dir_module>    DirectoryIndex index.php index.html </IfModule>

Only use index.php as the DirectoryIndex:

<IfModule dir_module>    DirectoryIndex index.php</IfModule>

3. Start the Apache web server

sudo service httpd start

4. Configure the Apache web server to start at each system boot

sudo chkconfig httpd on

5. Test your installation

Create phpinfo.php:

echo '<?php print phpinfo();' | sudo tee --append /var/www/html/phpinfo.php

Open your browser and enter your instance's public IP in the address bar followed by /phpinfo.php

Example: http://xxx.xxx.xxx.xxx/phpinfo.php

Note: Don't forget to allow incoming connections for HTTP (port 80) in the Security Groups of your instance, else your request will time out.


I got this running on my instance. You need http2.2 for it to work with the webtatic repo. I had so many repos there were a bunch of different version incompatibilities. I didn't keep great notes but looking at my history I basically did this:

# Remove current php & apachesudo service httpd stopsudo yum remove httpd* php* # Remove any third party repos that aren't relevantsudo yum repolistsudo yum remove remi-safe# Install Standard Apache for Amazon AMIsudo yum install httpd   #specify http22 if you get a different version# Download webtaticmkdir -p /tmp/php7cd /tmp/php7wget https://mirror.webtatic.com/yum/el6/latest.rpm# Install webtatic reposudo yum install latest.rpmsudo vi /etc/yum.repos.d/webtatic.repo  'set repo enablessudo yum clean all# Install base php7sudo yum install --enablerepo=webtatic php70wphp -v   #Should say something like  PHP 7.0.2 (cli) (built: Jan  9 2016 16:09:32) ( NTS )sudo yum install php70w-opcachesudo yum install php70w-xmlsudo yum install php70w-pdosudo yum install php70w-mysqlndsudo yum install php70w-gdsudo yum install php70w-apcusudo yum install php70w-pecl-apcusudo yum install php70w-mbstringsudo yum install php70w-imap# Restart apachesudo service httpd restart


Current answer to problem (not original version in question) - worth knowing since this is a bit simpler and php7 isn't available in the standard repos for Amazon Linux 2, and this didn't come up until I searched a bit harder:

amazon-linux-extras install php7.2

The version of extras can be checked with a list command if v7.2 is no longer current:

amazon-linux-extras list