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 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 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

 Outdated Tutorials

 References

 Full Version in PDF/EPUB