PHP is a widely-used open-source programming language for web development created by Rasmus Lerdorf. It is an HTML-embedded scripting language for creating dynamic web sites.
CentOS 7/6 comes with PHP 5.x by default which is already the end of life.
In this post, we will see how to install PHP 7.4 / 7.3 / 7.2 on CentOS 7 / RHEL 7 & CentOS 6 / RHEL 6.
Add PHP 7.x Repository
Contents
Remi, a third-party repository which offers the latest version of PHP (7.4 / 7.3 / 7.2 / 7.1) for CentOS.
To enable the Remi repository, install the Remi repository auto-configuration package.
### CentOS 7 / RHEL 7 ### yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm ### CentOS 6 / RHEL 6 ### yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm yum install -y https://rpms.remirepo.net/enterprise/remi-release-6.rpm
Install PHP 7.x On CentOS 7 & CentOS 6
Install PHP 7.4 on CentOS 7
Use the below command to install PHP 7.4 package by temporarily enabling Remi PHP 7.4 repository.
Remi doesn’t provide PHP 7.4 packages for CentOS 6
yum install -y --enablerepo=remi-php74 php php-cli
Install PHP 7.3 on CentOS 7 & CentOS 6
Use the below command to install PHP 7.3 package by temporarily enabling Remi PHP 7.3 repository.
yum install -y --enablerepo=remi-php73 php php-cli
Install PHP 7.2 on CentOS 7 & CentOS 6
Use the below command to install PHP 7.2 package by temporarily enabling Remi PHP 7.2 repository.
yum install -y --enablerepo=remi-php72 php php-cli
Install PHP 7.1 on CentOS 7 & CentOS 6
Use the below command to install PHP 7.1 package by temporarily enabling Remi PHP 7.1 repository.
yum install -y --enablerepo=remi-php71 php php-cli
Check PHP Version
Once you have installed the required PHP version, check the PHP version with the following command.
php -v
Output:
PHP 7.4.1 (cli) (built: Dec 17 2019 16:35:58) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies
Install PHP Extensions
PHP extensions are compiled libraries which enables specific functions/support for your code. For example, installing PHP MySQL extension will enable your PHP code to connect with the MySQL database.
PHP extensions package is normally named like php-<extn_name>.
To install MySQL support, you can install php-mysqlnd package.
yum install -y --enablerepo=remi-php74 php-mysqlnd
Once you have installed a particular extension, you can use the below command to verify it.
php -m | grep -i mysql
Output:
mysqli
mysqlnd
pdo_mysql
PHP Extensions for WordPress
The following extensions are good enough to install and run WordPress on your CentOS system. WordPress recommends having PHP v7.3 for the installation.
yum install -y --enablerepo=remi-php73 php-dom php-simplexml php-ssh2 php-xml php-xmlreader php-curl php-date php-exif php-filter php-ftp php-gd php-hash php-iconv php-imagick php-json php-libxml php-mbstring php-mysqlnd php-openssl php-pcre php-posix php-sockets php-spl php-tokenizer php-zlib
PHP Extensions for Joomla
The following extensions are good enough to install and run Joomla on your CentOS system. Joomla requires PHP v7.1 and above.
yum install -y --enablerepo=remi-php73 php-mysqlnd php-zlib php-xml php-pear php-json php-mcrypt
PHP Extensions for Drupal
The following extensions are good enough to install and run Joomla on your CentOS system. Drupal requires PHP v7.1 and above.
yum install -y --enablerepo=remi-php73 php-mysqlnd php-date php-dom php-filter php-gd php-hash php-json php-pcre php-pdo php-session php-simplexml php-spl php-tokenizer php-xml
Conclusion
That’s All. Please share your feedback in the comments section.