Suggestions regarding compiling PHP extensions (e.g.memcached, redis, imagick) on Amazon Linux 2023 (AL2023)
Overview
AL2023 includes many PHP modules that are included in PHP Core. AL2023 does not aim to include all of the packages in the PHP Extension Community Library (PECL). You can browse the feature requests at AL2023 GitHub issues page, or you can compile the desired PHP extension.
Note that not all extensions can be compiled on AL2023.
Install PHP
Install PHP
sudo dnf install -y php8.3-*
To get listing of installed PHP extensions
php -m |more
Compile and install PECL extensions
Install development tools
sudo dnf install -y php-devel php-pear gcc
sudo pecl update-channels
Option 1: Compile and install PECL extensions with the pecl command
To compile a extension using the pecl command where extname
is name of PECL extension
sudo pecl install extname
This will download the source for extname
, compile, and install extname.so
into the extension directory at /usr/lib64/php8.3/modules/
Option 2: Compile and install PECL extensions with the phpize command
Sometimes, the pecl command may not work, or that the extension is unavailable as a PECL-compatible package. In this case, you can use phpize
To download source files from PECL
pecl download extname
tar -xf extname-*.tgz
rm -f extname-*.tgz
cd extname-*
To compile
phpize
./configure
make
sudo make install
For certain extensions, you may need to install additional development libraries. See below for examples.
Load PHP extensions
To load the compiled extension, add extension directive to /etc/php.d
directory.
sudo tee /etc/php.d/extname.ini > /dev/null << EOF
extension=extname.so
EOF
Restart PHP FPM (FastCGI Process Manager)
sudo systemctl restart php-fpm
Restart Apache web server if using mod-php
sudo systemctl restart httpd
Verify PHP extensions
To verify
php -m | grep -i extname
For php-fpm and mod-php, you can use PHP file with phpinfo() in /var/www/html
(Apache) or /usr/share/nginx/html
(Nginx) folder. For security reasons, delete your PHP file after verification.
Example scripts for common PHP extensions
Below are example scripts to install and load popular extensions for applications such as WordPress. Do install PHP and developments tools first, i.e.
sudo dnf install -y php8.3-*
sudo dnf install -y php-devel php-pear gcc
sudo pecl update-channels
You may have to adjust the configuration parameters
sudo pecl install --configureoptions 'enable-apcu-debug="no"' apcu
sudo tee /etc/php.d/40-apcu.ini > /dev/null << EOF
extension=apcu.so
EOF
sudo pecl install igbinary
sudo tee /etc/php.d/25-igbinary.ini > /dev/null << EOF
extension=igbinary.so
EOF
sudo dnf install -y ImageMagick ImageMagick-devel
pecl download Imagick
tar -xf imagick*.tgz
rm -f imagick*.tgz
cd imagick*
phpize
./configure
make
sudo make install
sudo tee /etc/php.d/25-imagick.ini > /dev/null << EOF
extension=imagick.so
EOF
git clone --recursive --depth=1 https://github.com/kjdev/php-ext-lz4.git
cd php-ext-lz4
phpize
./configure
make
sudo make install
sudo tee /etc/php.d/25-lz4.ini > /dev/null << EOF
extension=lz4.so
EOF
sudo pecl install --configureoptions 'enable-lzf-better-compression="no"' lzf
sudo tee /etc/php.d/25-lzf.ini > /dev/null << EOF
extension=lzf.so
EOF
sudo pecl install msgpack
sudo tee /etc/php.d/25-msgpack.ini > /dev/null << EOF
extension=msgpack.so
EOF
sudo dnf install -y libssh2-devel
sudo pecl install --configureoptions 'with-ssh2="yes"' ssh2
sudo tee /etc/php.d/25-ssh2.ini > /dev/null << EOF
extension=ssh2.so
EOF
sudo pecl install timezonedb
sudo tee /etc/php.d/25-timezonedb.ini > /dev/null << EOF
extension=timezonedb.so
EOF
sudo pecl install xdebug
Refer to Xdebug documentation for configuration steps
sudo dnf install -y libzstd-devel
sudo pecl install zstd
sudo tee /etc/php.d/25-zstd.ini > /dev/null << EOF
extension=zstd.so
EOF
Ensure that igbinary
and msgpack
serializer extensions have been compiled and are loaded first. Else specify no
for enable-memcached-igbinary
and enable-memcached-msgpack
sudo dnf install -y memcached-devel libmemcached-awesome-devel zlib-devel cyrus-sasl-devel libevent-devel
sudo pecl install --configureoptions 'with-libmemcached-dir="yes" with-zlib-dir="yes" with-system-fastlz="no" enable-memcached-igbinary="yes" enable-memcached-msgpack="yes" enable-memcached-json="yes" enable-memcached-protocol="yes" enable-memcached-sasl="yes" enable-memcached-session="yes"' memcached
sudo tee /etc/php.d/40-memcached.ini > /dev/null << EOF
extension=memcached.so
EOF
Ensure that igbinary
, msgpack
serializer and lzf
, zstd
compression extensions have been compiled and are loaded first. Else specify no
for enable-redis-igbinary
, enable-redis-msgpack
, enable-redis-lzf
and enable-redis-zstd
sudo dnf install -y redis6-devel lz4-devel libzstd-devel
sudo pecl install --configureoptions 'enable-redis-igbinary="yes" enable-redis-msgpack="yes" enable-redis-lzf="yes" enable-redis-zstd="yes" enable-redis-lz4="yes" with-liblz4="yes"' redis
sudo tee /etc/php.d/40-redis.ini > /dev/null << EOF
extension=redis.so
EOF
These drivers rely on Microsoft ODBC driver for SQL Server to handle the low-level communication with SQL Server, which needs to be installed
sudo dnf install php-odbc unixODBC-devel -y
sudo pecl install sqlsrv
sudo tee /etc/php.d/20-sqlsrv.ini > /dev/null << EOF
extension=sqlsrv.so
EOF
sudo pecl install pdo_sqlsrv
sudo tee /etc/php.d/30-pdo_sqlsrv.ini > /dev/null << EOF
extension=pdo_sqlsrv.so
EOF
Restart php-fpm (sudo systemctl restart php-fpm
) or Apache (sudo systemctl restart httpd
) depending on your configuration.
CloudFormation template
To get an AL2023 EC2 instance with PHP extensions up and running for testing, you can try the CloudFormation template at aws-samples/ec2-lamp-server - Github