Install Extensions
Note
Unlike the running modes of Apache+PHP or Nginx+PHP, Workerman is run based on PHP CLI PHP CLI and uses a different PHP executable, and the php.ini file used may also differ. Therefore, seeing that a certain extension is installed by printing phpinfo() on a webpage does not necessarily mean that the corresponding extension is also installed for the command line PHP CLI.
How to Determine Which Extensions Are Installed in PHP CLI
Running php -m will list the extensions that are already installed in the command line PHP CLI, with results similar to the following:
~# php -m
[PHP Modules]
event
posix
pcntl
...
How to Determine the Location of the PHP CLI php.ini File
When we install an extension, we may need to manually configure the php.ini file and add the extension, so we need to confirm the location of the PHP CLI's php.ini file. You can run php --ini to find the ini file location of PHP CLI, with results similar to the following (the display results may vary across systems):
~# php --ini
Configuration File (php.ini) Path: /etc/php8/cli
Loaded Configuration File: /etc/php8/cli/php.ini
Scan for additional .ini files in: /etc/php8/cli/conf.d
Additional .ini files parsed: /etc/php8/cli/conf.d/apc.ini,
/etc/php8/cli/conf.d/pdo.ini,
/etc/php8/cli/conf.d/pdo_mysql.ini
...
Install Extensions for PHP CLI (Taking the memcached extension as an example)
Method 1: Install Using apt or yum Command
If PHP is installed via apt or yum commands, then extensions can also be installed via apt or yum.
Method for installing PHP extensions using apt on systems like Debian/Ubuntu (non-root users need to prepend sudo commands)
- Use
apt-cache searchto find the extension package~# apt-cache search memcached php php-apc - APC (Alternative PHP Cache) module for PHP 5 php5-memcached - memcached module for php5 - Use
apt-get installto install the extension package~# apt-get install -y php5-memcached Reading package lists... Done Reading state information... Done ...
Method for installing PHP extensions using yum on systems like CentOS
- Use
yum searchto find the extension package~# yum search memcached php php-pecl-memcached - memcached module for php5 - Use
yum installto install the extension package~# yum install -y php-pecl-memcached Reading package lists... Done Reading state information... Done ...Note:
Installing PHP extensions using apt or yum will automatically configure the php.ini file, and the extensions can be used directly after installation, which is very convenient. The downside is that some extensions may not have corresponding install packages available in apt or yum.
Method 2: Install Using pecl
Use the pecl install command to install the extension.
- Install using
pecl install~# pecl install memcached downloading memcached-2.2.0.tgz ... Starting to download memcached-2.2.0.tgz (70,449 bytes) .... - Configure php.ini
By running php --ini, find the location of the php.ini file, then add extension=memcached.so to the file.
Method 3: Source Compilation Installation (Generally used for installing PHP built-in extensions, taking the pcntl extension as an example)
- Use the
php -vcommand to check the current version of PHP CLI~# php -v PHP 5.3.29-1~dotdeb.0 with Suhosin-Patch (cli) (built: Aug 14 2014 19:55:20) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies - Download the PHP source code based on the version
PHP historical version download page: https://php.net/releases/
- Unzip the source package
For example, if the downloaded package is named php-5.3.29.tar.gz
~# tar -zxvf php-5.3.29.tar.gz
php-5.3.29/
php-5.3.29/README.WIN32-BUILD-SYSTEM
php-5.3.29/netware/
...
- Navigate to the ext/pcntl directory in the source code
~# cd php-5.3.29/ext/pcntl/ - Run the
phpizecommand~# phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 - Run the
configurecommand~# ./configure checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E ... - Run the
makecommand~# make /bin/bash /tmp/php-5.3.29/ext/pcntl/libtool --mode=compile cc ... -I/usr/include/php5 -I/usr/include/php5/main -I/usr/include/php5/TSRM -I/usr/include/php5/Zend... ... - Run the
make installcommand~# make install Installing shared extensions: /usr/lib/php5/20090626/ - Configure the ini file
By running php --ini, find the location of the php.ini file, then add extension=pcntl.so to the file.
Note:
This method is generally used to install PHP built-in extensions, such as the posix extension and the pcntl extension. In addition to using phpize to compile a specific extension, you can also recompile the entire PHP with parameters to add extensions during compilation. For example, run in the root directory of the source code:
~# ./configure --enable-pcntl --enable-posix ...
~# make && make install
Method 4: Install with phpize
If the extension to be installed is not available in the ext directory of the PHP source code, then you need to search and download this extension from https://pecl.php.net.
Taking the libevent extension as an example (assuming the system has installed the libevent-dev library):
- Download the libevent extension file package (the directory in which the package is downloaded is arbitrary)
~# wget https://pecl.php.net/get/libevent-0.1.0.tgz --2015-05-26 21:43:40-- https://pecl.php.net/get/libevent-0.1.0.tgz Resolving pecl.php.net... 104.236.228.160 Connecting to pecl.php.net|104.236.228.160|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 9806 (9.6K) [application/octet-stream] Saving to: “libevent-0.1.0.tgz”
100%[=======================================================>] 9,806 41.4K/s in 0.2s
2. Unzip the extension file package
```shell
~# tar -zxvf libevent-0.1.0.tgz
package.xml
libevent-0.1.0/config.m4
libevent-0.1.0/CREDITS
libevent-0.1.0/libevent.c
....
-
Navigate to the source directory
~# cd libevent-0.1.0/ -
Run the
phpizecommand~# phpize Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 -
Run the
configurecommand~# ./configure checking for grep that handles long lines and -e... /bin/grep checking for egrep... /bin/grep -E checking for a sed that does not truncate output... /bin/sed checking for cc... cc checking whether the C compiler works... yes ... -
Run the
makecommand~# /bin/bash /data/test/libevent-0.1.0/libtool --mode=compile cc -I. -I/data/test/libevent-0.1.0 -DPHP_ATOM_INC -I/data/test/libevent-0.1.0/include ... -
Run the
make installcommand~# make install Installing shared extensions: /usr/lib/php5/20090626/ -
Configure the ini file
By running php --ini, find the location of the php.ini file, then add extension=libevent.so to the file.