16.6 C
Wednesday, December 1, 2021

How to install PHP 7.2/7.3/7.4 or 8.0 on Amazon Linux 2

Must read

While working with an application, I needed PHP 7.4 and 8.0 on Amazon Linux 2 EC2 and Lightsail instance. Here is how to install PHP version 7.2/7.3/7.4 or 8.0 on Amazon Linux 2 using the yum command.

The default PHP version is 5.4.16 on Amazon Linux version 2. However, for Lightsail or EC2 cloud server, I needed either PHP 7.4 or PHP 8.0 as my application works with both versions flawlessly. So, let us see how a developer can install PHP 7.4 or 8.0 on an Amazon Linux 2 virtual machine.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Amazon Linux 2
Est. reading time 5 minutes

Finding PHP version

Run the following yum command:
# yum info php
Here is what I see:

Loaded plugins: kernel-livepatch, langpacks, priorities, update-motd
209 packages excluded due to repository priority protections
Available Packages
Name        : php
Arch        : x86_64
Version     : 5.4.16
Release     : 46.amzn2.0.2
Size        : 1.4 M
Repo        : amzn2-core/2/x86_64
Summary     : PHP scripting language for creating dynamic web sites
URL         : http://www.php.net/
License     : PHP and Zend and BSD
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
            : easy for developers to write dynamically generated web pages. PHP also
            : offers built-in database integration for several commercial and
            : non-commercial database management systems, so writing a
            : database-enabled webpage with PHP is fairly simple. The most common
            : use of PHP coding is probably as a replacement for CGI scripts.
            : The php package contains the module (often referred to as mod_php)
            : which adds support for the PHP language to Apache HTTP Server.

How to enable PHP 7.4 or 8.0 version on AWS EC2 Amazon AMI

First, install the following package, run:
# yum install amazon-linux-extras
Then type the following command to see and manage specific new software. In other words, use the amazon-linux-extras command to enable the latest version of PHP such as 7.4 or 8.0:
# amazon-linux-extras list
# Use the grep command or more command/less command to filter out results #
# amazon-linux-extras list | grep php

Bingo, now we see desired PHP versions:

 15  php7.2                   available    
 17  lamp-mariadb10.2-php7.2  available    
 31  php7.3                   available    
 42  php7.4                   available    [ =stable ]
 51  php8.0                   available    [ =stable ]

How to install PHP 7.x or 8.x on Amazon Linux 2

Now that I found the PHP version, it is time to install it. The commands depend upon the version or per your web application’s needs running on the cloud server.

Use the php-fpm package instead of php when you need to support Nginx instead of Apache.

Installing PHP 7.2 on Amazon Linux 2

Here is how to switch default PHP version from 5.6.x to 7.2 and then install the same:
# amazon-linux-extras enable php7.2
# yum clean metadata
# yum info php
# yum install php

Search for PHP extensions and then install them using the yum command:
# yum search php | more
# yum search php | grep -E 'gd|mysql'

Installing PHP 7.3 on Amazon Linux 2

Run the following commands:
# amazon-linux-extras enable php7.3
# yum clean metadata
# yum info php
# yum install php
# yum search php | more
# yum search php | grep -E 'gd|mysql'

Installing PHP 7.4 on Amazon Linux 2

Execute the following commands when you need PHP version 7.4:
# amazon-linux-extras enable php7.4
# yum clean metadata
# yum info php
# yum install php
# yum search php | more
# yum search php | grep -E 'gd|mysql'

Tip: You can install everything in a single command. For example:
sudo amazon-linux-extras install -y php7.4

Installing PHP 8.0 on Amazon Linux 2

I am going to use PHP 8.0. So I type:
# amazon-linux-extras enable php8.0

Enabling PHP 8.0 on Amazon Linux 2 command

You must clean metadata and search/verify PHP version:
# yum clean metadata
# yum info php

Cleaning yum meta data and looking for PHP 8.0 on Amazon Linux 2

So far, so good. We enabled PHP 8 repo. Thus, we can now install PHP version 8.x for Apache server (httpd will be installed automatically for you), run:
# yum install php

--> Finished Dependency Resolution
Dependencies Resolved
 Package              Arch    Version                 Repository          Size
 php                  x86_64  8.0.8-1.amzn2           amzn2extra-php8.0  3.3 M
Installing for dependencies:
 apr                  x86_64  1.7.0-9.amzn2           amzn2-core         122 k
 apr-util             x86_64  1.6.1-5.amzn2.0.2       amzn2-core          99 k
 apr-util-bdb         x86_64  1.6.1-5.amzn2.0.2       amzn2-core          19 k
 generic-logos-httpd  noarch  18.0.0-4.amzn2          amzn2-core          19 k
 httpd                x86_64  2.4.51-1.amzn2          amzn2-core         1.3 M
 httpd-filesystem     noarch  2.4.51-1.amzn2          amzn2-core          24 k
 httpd-tools          x86_64  2.4.51-1.amzn2          amzn2-core          88 k
 libzip               x86_64  1.3.2-1.amzn2.0.1       amzn2-core          62 k
 mailcap              noarch  2.1.41-2.amzn2          amzn2-core          31 k
 mod_http2            x86_64  1.15.19-1.amzn2.0.1     amzn2-core         149 k
 php-cli              x86_64  8.0.8-1.amzn2           amzn2extra-php8.0  5.0 M
 php-common           x86_64  8.0.8-1.amzn2           amzn2extra-php8.0  1.2 M
Transaction Summary
Install  1 Package (+12 Dependent packages)
Total download size: 11 M
Installed size: 48 M
Is this ok [y/d/N]: y

Then restart the service using the systemctl command:
# systemctl restart httpd.service

PHP-FPM 8.0 for Nginx web server

For Nginx or other servers and app, you may want to install the php-fpm package:
# yum install php-fpm nginx

Installing PHP 8.0 (fpm) for Nginx server

A note about PHP 8 extensions

How about PHP 8 extensions? First, search them and then install them:
# yum search php | more
# yum search php | grep -E 'gd|mysql'

For instance:
# yum install php-gd php-json php-mbstring php-mysqlnd php-opcache php-pdo php-pecl-igbinary

PHP 8 and Nginx config

Edit or append as follows in the

# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/run/php-fpm/www.sock;

Then enable and restart the services using the systemctl command:
# systemctl enable nginx.service
# systemctl enable php-fpm.service
# systemctl restart php-fpm.service
# systemctl restart nginx.service

How to verify PHP version

You can check and print PHP version installed on Linux box by typing the following command:
php --version
php-fpm --version

Here is what I see:

Patreon supporters only guides 🤓

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare
  • Join my Patreon to support independent content creators and start reading latest guides:

Join Patreon

PHP 8.0.8 (fpm-fcgi) (built: Jul  7 2021 17:37:26)
Copyright (c) The PHP Group
Zend Engine v4.0.8, Copyright (c) Zend Technologies

Of course, for Apache server create a test script:

echo '<?php phpinfo(); ?>' > /var/www/html/test.php

For nginx:

echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/test.php

Fire a browser and then run:

Test your PHP based web server (click to enlarge)

Summing up

You learned about setting and installing PHP 7.4 or 8.0 on Amazon Linux 2 using the yum command. For more information, see PHP documentation and Amazon Linux 2 project webpage. You can read about yum using the man command:
man yum


Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

- Advertisement -

Latest article