How to compile custom PHP extensions?

Learn how to compile custom PHP extensions step by step. Guide to building, installing, and enabling PHP extensions for optimized server performance

How to compile custom PHP extensions?

⚠️ Issue

🛠️ How to compile custom PHP extensions in CloudLinux with PHP Selector?

When using Alt-PHP (CloudLinux), compiling extensions is slightly different than standard PHP because each PHP version has its own binaries and directories.


🖥️ Environment

  • PHP Selector

  • cPanel

  • PHP 5.x / 7.x (applies also to newer Alt-PHP versions with the same process)


✅ Solution

Follow these steps to build and enable a custom PHP extension:

📥 1. Download & unpack extension

# wget https://example.com/extension.tar.gz tar -xvzf extension.tar.gz cd extension-directory

⚙️ 2. Prepare with phpize

Run the phpize from the corresponding Alt-PHP version (replace XX with version, e.g., 74, 81):

# /opt/alt/phpXX/usr/bin/phpize 

🔧 3. Configure with correct php-config

# ./configure --with-php-config=/opt/alt/phpXX/usr/bin/php-config 

🏗️ 4. Compile the extension

# make 

📂 5. Copy compiled module to Alt-PHP modules dir

For 64-bit servers:

# cp -rp modules/*.so /opt/alt/phpXX/usr/lib64/php/modules/

For 32-bit servers:

# cp -rp modules/*.so /usr/lib/php/modules/

📝 6. Create .ini file for extension

Add an ini config file inside Alt-PHP:

echo "extension=your_extension.so" > /opt/alt/phpXX/etc/php.d.all/your_extension.ini

🔄 7. Register with PHP Selector

Finally, update CloudLinux PHP Selector:

# cagefsctl --setup-cl-selector 

🐞 Cause

This process is needed because:

  • Each Alt-PHP version uses its own binaries (phpize, php-config).

  • Extensions compiled with system PHP will not work with CloudLinux’s Alt-PHP.

  • Manual registration is required for PHP Selector to detect the extension.

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0