How to Install Multiple PHP Extensions using PECL via SSH?
introduction
A Linux hosting environment may require installing a particular PHP extension on multiple PHP versions as part of its management. You usually need to run an installer for each version separately. However, you can use the PECL command in your WHM/cPanel environment to install these extensions efficiently, even for multiple versions at once.
What is PECL and Why Use it for Extensions?
PECL (PHP Extension Community Library) is a major repository for PHP extensions. These extensions are like small add-ons that provide the capabilities of the core PHP language. When your hosting uses EasyApache 4, it supports various PHP versions simultaneously. You need to use the specific pecl command location for each version to install an extension correctly. This is to make sure that the extension is compatible with the correct PHP version on your server.
Steps to Install a PHP Extension for One Version
When you are simply required to install a module in one PHP version, you must first find the correct path for that version’s pecl command. Follow these steps for a simple installation.
- Locate the PECL command for the PHP version
The exact location of the pecl command differs based on the specific PHP version you are using. For example, on a server using EasyApache 4, the path looks like this:
- For PHP 7.2: /opt/cpanel/ea-php72/root/usr/bin/pecl
- For PHP 7.3: /opt/cpanel/ea-php73/root/usr/bin/pecl
2. Run the Install Command
After knowing the right path, you use the install argument along with the extension name. You must execute this command via the command line with root access.
For instance, to install the xmldiff module for PHP 7.3, use:
$/opt/cpanel/ea-php73/root/usr/bin/pecl install xmldiff
How to Install Multiple PHP Extensions
It is time-consuming to install the same extension on numerous PHP versions individually. It is possible to do it automatically for the entire range of available PHP versions with a loop command in the terminal.
- Use the Loop Command
The following command will automatically check all the listed PHP versions for the pecl tool. It then runs the installation command for the specified extension on each version it finds.
$ls /opt/cpanel/ea-php{72..80}/root/usr/bin/pecl 2>/dev/null | while read phpversion; do $phpversion install xmldiff; done
2. Understand the Command Logic
This example command does a few things:
- It checks for the pecl binary in the directories for PHP versions from 7.2 (represented as 72) up to 8.0 (represented as 80).
- The output is then piped (|) to a while read loop.
- The loop runs the install xmldiff command with the appropriate pecl path for every PHP version found.
You can use this looping technique as a great example. You can change the extension name like xmldiff to any other PHP extension you need to install on your Linux server.
Cantech’s Features in Developer Control
Our powerful hosting provides you with access to the root, the ability to control your server environment, and EasyApache 4. This configuration enables you to take advantage of command-line tools such as PECL, which will enable you to install and manage custom PHP extensions in all of the versions that you run. You can set up your server in the way that your application needs.
Conclusion
The surest method of installing extensions on your Linux server is to use the special pecl path of each version of PHP. A loop command saves a lot of time and effort to install a single extension in multiple versions. This is the best way to have all your PHP environments always set up with the necessary extensions.
Frequently Asked Questions
What does the PECL command do in simple terms?
The pecl command is a command line tool that downloads and installs PHP extensions from the official PECL repository directly onto your hosting server environment.
What is the reason that PECL command path varies with each version of PHP?
The path is different because each major PHP version is installed in a separate, distinct directory on the server. The pecl tool must be run from its specific location inside that PHP version’s directory to work correctly.
What is the purpose of the while read loop in the multiple installation command?
The while read loop reads each individual pecl path found by the ls command one by one. It then automatically runs the installation command using that specific path for the PHP extension.
Can I use this loop method to install any PECL extension?
Yes, the looping command is a general method for automation. You simply need to replace the extension name, which is xmldiff in the example, with the name of any other PECL extension you wish to install.
What is the meaning of {72..80} in the command?
The {72..80} part is a range expression used in the command line. It tells the system to look in the directories corresponding to PHP versions from 7.2 up to 8.0.