How to install PHP Version 7.0 to 7.4 in CentOS 7

How to install PHP Version 7.0 to 7.4 in CentOS 7

PHP (Hypertext Preprocessor) is a server-side scripting language used by most developers all over the world. PHP is used to create web applications and websites. It almost supports every type of operating system such as Windows, Linux, macOS, etc.

Feature of PHP

  • PHP is easy to learn and understand.
  • Applications and Websites based on PHP are very fast.
  • PHP code can be embedded into HTML

Prerequisites

  • CentOS 7 Operating System
  • Root user or another user with Sudo privileges

PHP Modules

PHP modules are the building blocks of PHP. PHP modules are very important, it connects other application to PHP. For example, the PHP module mysqli is used to connect PHP with MySQL. The List of mostly used PHP modules is as follows:-

hash
mysqlnd
mbstring
ldap
curl
imap
zip
json
mysqli
gd
fpm
cli
imagick
zlib
intl
opcache
xml

Update the server

Update the server packages and repositories with the following command.

yum update -y

Enable the EPEL Repository

  • To install the PHP versions, we need to enable the EPEL repository.
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
  • To install PHP 7.0 with their modules, run the following command.
yum install php70-php php70-php-opcache php70-php-xml php70-php-hash php70-php-mysqlnd php70-php-mbstring php70-php-ldap php70-php-curl php70-php-imap php70-php-zip php70-php-json php70-php-mysqli php70-php-intl php70-php-gd php70-php-fpm php70-php-cli php70-php-imagick php70-php-zlib -y
  • To install PHP 7.1 with their modules, run the following command.
yum install php71-php php71-php-opcache php71-php-xml php71-php-hash php71-php-mysqlnd php71-php-mbstring php71-php-ldap php71-php-curl php71-php-imap php71-php-zip php71-php-json php71-php-mysqli php71-php-intl php71-php-gd php71-php-fpm php71-php-cli php71-php-imagick php71-php-zlib -y
  • To install PHP 7.2 with their modules, run the following command.
yum install php72-php php72-php-opcache php72-php-xml php72-php-hash php72-php-mysqlnd php72-php-mbstring php72-php-ldap php72-php-curl php72-php-imap php72-php-zip php72-php-json php72-php-mysqli php72-php-intl php72-php-gd php72-php-fpm php72-php-cli php72-php-imagick php72-php-zlib -y
  • To install PHP 7.3 with their modules, run the following command.
yum install php73-php php73-php-opcache php73-php-xml php73-php-hash php73-php-mysqlnd php73-php-mbstring php73-php-ldap php73-php-curl php73-php-imap php73-php-zip php73-php-json php73-php-mysqli php73-php-intl php73-php-gd php73-php-fpm php73-php-cli php73-php-imagick php73-php-zlib -y
  • To install PHP 7.4 with their modules, run the following command.
yum install php74-php php74-php-opcache php74-php-xml php74-php-hash php74-php-mysqlnd php74-php-mbstring php74-php-ldap php74-php-curl php74-php-imap php74-php-zip php74-php-json php74-php-mysqli php74-php-intl php74-php-gd php74-php-fpm php74-php-cli php74-php-imagick php74-php-zlib -y
  • To check the PHP version, run the following command
php -v
  • If the above command shows an error that the command is not found, then you can change the default PHP version for a session with the following command.
scl enable php71 bash
  • To check the installed PHP modules of the default PHP version, then run the following command.
php -m
  • If the server has multiple PHP versions and to check the installed PHP versions on the server, run the following command.
yum list installed | grep php

Set the default PHP version

To change the PHP versions permanently, we need to set up a symbolic link of the PHP binaries.

  • Change your current working directory to /bin/
cd /bin/
  • Run the following command to filter the PHP version with their binaries.
ls -l | grep php
Check PHP versions and binaries
  • Run the following command to delete the existing PHP symbolic link.
rm -rf /bin/php
  • Run the following command to create a symbolic link for PHP 7.3
ln -s /opt/remi/php73/root/usr/bin/php /bin/php
  • Now check the PHP version.
php -v
Check PHP version

Conclusion

In this tutorial, we have learned about PHP and its features, we have also learned how we can install, check and change the default PHP versions.

If you guys have any queries or issues, then let me know in the comments sections.