Skip to content

How do I compile PHP extensions on Amazon Linux 2023?

5 minute read
Content level: Advanced
3

Compile PHP extensions (e.g.memcached, redis, imagick) on Amazon Linux 2023 (AL2023)

Overview

Amazon Linux 2023 (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).

Refer to AL2023 packages list for listing of available PHP packages. For other extensions, you can monitor the feature request status at AL2023 GitHub issues page

This article suggests how you can compile your desired PHP extension. Note that not all extensions can be compiled on AL2023.

Update DNF repository

Check that you are using latest DNF repository

 dnf check-release-update

If not, upgrade to latest release version (if available) and disable deterministic upgrade

sudo dnf upgrade --releasever=latest
echo latest | sudo tee /etc/dnf/vars/releasever

This is important as any new PHP extension packages and essential libraries are added to newer AL2023 versions. For example,

Install PHP

To install PHP 8.4

sudo dnf install -y php8.4-*

To get listing of installed PHP extensions

php -m |more

Compile and install PECL extension

Install development tools

sudo dnf install -y php-devel php-pear gcc
sudo pecl update-channels

Option 1: Compile and install PECL extension 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 extension 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 search AL2023 packages list and AL2023 GitHub issues page for status of your desired extension before compiling.

Install PHP and development tools

Adjust PHP version if necessary

phpVersion=php8.4
sudo dnf install -y $phpVersion-*
sudo dnf install -y $phpVersion-pecl-{apcu,igbinary,msgpack,redis6}

sudo dnf install -y php-devel php-pear gcc
sudo pecl update-channels
cd /tmp

You may have to adjust the configuration parameters

imagick

sudo dnf install -y ImageMagick ImageMagick-devel
yes | sudo pecl install imagick
sudo tee /etc/php.d/25-imagick.ini > /dev/null << EOF
extension=imagick.so
EOF

lz4

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

lzf

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

ssh2

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

timezonedb

sudo pecl install timezonedb
sudo tee /etc/php.d/25-timezonedb.ini > /dev/null << EOF
extension=timezonedb.so
EOF

xdebug

sudo pecl install xdebug

Refer to Xdebug documentation for configuration steps

zstd

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

memcached

phpVersion=php8.4
sudo dnf install -y $phpVersion
sudo dnf install -y $phpVersion-pecl-{igbinary,msgpack}
sudo dnf install -y $phpVersion-pecl-{igbinary,msgpack}-devel


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="no" 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

Microsoft Drivers for PHP for Microsoft SQL Server

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

Drivers can also be download from PHP Driver for SQL Server Releases page

Load PHP extension

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