Install PHP Extensions on CentOS

This section provides a tutorial example on how to add PHP extensions on CentOS Linux systems by searching and installing the extension using the 'dnf' package manager.

PHP has many extensions. Some of them are pre-installed and some are not. If you are getting errors like "Class ... not found", or "Call to undefined function ...", you need to install the extension that provides the missing class or function.

For example, when I test a new Web application on my CentOS system, I see this error. I know that I need to install the ZIP extension.

PHP Fatal error:  Uncaught Error: Class 'ZipArchive' not found in
   /var/www/html/download.php:361

Stack trace:
#0 download.php(338): zipData()
#1 dowload.php(22): prepareData()
#2 {main}

To fix the problem, I check what PHP extensions have been installed on the system first:

herong$ dnf list installed | grep php

php.x86_64
php-cli.x86_64
php-common.x86_64
php-fpm.x86_64
php-json.x86_64
php-mbstring.x86_64
php-mysqlnd.x86_64
php-pdo.x86_64

There is no ZIP related extensions installed yet. So I search for PHP extensions:

herong$ dnf search php

...
php-opcache.x86_64 : The Zend OPcache
php-pecl-zip.x86_64 : A ZIP archive management extension
php-pecl-apcu.x86_64 : APC User Cache
php-pecl-apcu-devel.x86_64 : APCu developer files (header)

I see that "php-pecl-zip" is available for my system. So I install it:

herong$ sudo dnf install php-pecl-zip
...
Installed:
php-pecl-zip-1.15.3-1.module_el8.1.0+209+03b9a8ff.x86_64
libzip-1.5.1-2.module_el8.1.0+209+03b9a8ff.x86_64
Complete!

Now re-run the Web application. The 'ZipArchive' error is gone!

Table of Contents

 About This Book

 Introduction and Installation of PHP

 Managing PHP Engine and Modules on macOS

Managing PHP Engine and Modules on CentOS

 PHP Version Pre-Installed on CentOS

Install PHP Extensions on CentOS

 PHP Version Pre-Installed on CentOS 6

 Use PHP Composer on CentOS Computers

 cURL Module - Client for URL

 DOM Module - Parsing HTML Documents

 GD Module - Manipulating Images and Pictures

 MySQLi Module - Accessing MySQL Server

 OpenSSL Module - Cryptography and SSL/TLS Toolkit

 PCRE Module - Perl Compatible Regular Expressions

 SOAP Module - Creating and Calling Web Services

 SOAP Module - Server Functions and Examples

 Zip Module - Managing ZIP Archive Files

 References

 Full Version in PDF/EPUB