How to Change PHP Extension Directory from Default /usr/lib64/20090626-zts to Custom Path


2 views

When compiling PHP from source, the default extension directory often gets set to paths like /usr/lib64/20090626-zts or similar version-specific locations. While you can specify extension_dir in php.ini, the build system keeps using the original path when generating new extensions via phpize.

The key is to set the correct path during the initial compilation using the --with-libdir and --with-config-file-scan-dir configure options:

./configure \
    --prefix=/usr \
    --libdir=/usr/lib64 \
    --with-libdir=lib64 \
    --with-config-file-scan-dir=/etc/php.d \
    --with-config-file-path=/etc \
    --sysconfdir=/etc \
    --with-pear \
    --enable-mbstring \
    --with-zlib \
    --enable-zip \
    --with-openssl

For already compiled PHP installations, you need to modify these files:

  1. php.ini:
    extension_dir = "/usr/lib64/php/modules"
    
  2. php-config (usually at /usr/bin/php-config):
    # Change this line:
    extension_dir='/usr/lib64/php/modules'
    

To make phpize use your custom path when building new extensions:

# Locate phpize script (usually /usr/bin/phpize)
# Find and modify this line:
phplibdir="/usr/lib64/php/modules"

After making these changes, create a test extension to verify:

cd /tmp
pecl download redis
tar xzf redis*
cd redis-*
phpize
./configure
make
make install

Check the output of make install - it should show your modules being installed to the new directory.

For proper system integration on Linux systems:

# Create the new modules directory
sudo mkdir -p /usr/lib64/php/modules

# Move existing extensions
sudo mv /usr/lib64/20090626-zts/* /usr/lib64/php/modules/

# Update ldconfig
echo "/usr/lib64/php/modules" | sudo tee /etc/ld.so.conf.d/php-modules.conf
sudo ldconfig

Remember to restart your web server or PHP-FPM after making these changes.


When compiling PHP from source, the system defaults to storing extensions in paths like /usr/lib64/20090626-zts, which follows the Zend Thread Safety (ZTS) naming convention. This becomes problematic when:

  • You want a cleaner, more organized directory structure
  • Need to maintain multiple PHP versions
  • Want to follow standard Linux directory conventions

The default extension directory is determined during PHP compilation through these components:

1. The PHP_API_VERSION (e.g., 20090626)
2. Zend Module API no (ZEND_MODULE_API_NO)
3. Whether ZTS is enabled
4. The --libdir parameter during configure

To permanently change the extension directory, you need to recompile PHP with these configure options:

./configure \
--with-config-file-path=/etc/php \
--libdir=/usr/lib64 \
--with-libdir=lib64 \
--prefix=/usr \
--with-extension-dir=/usr/lib64/php/modules

The critical parameter is --with-extension-dir which sets the permanent path for all extensions.

After installation, check with:

php -i | grep extension_dir
# Or more precisely:
php -r "echo ini_get('extension_dir').PHP_EOL;"

For modules already compiled with phpize, you need to either:

  1. Recompile them after changing PHP's extension_dir
  2. Or manually specify the install path during make:
cd /path/to/extension
phpize
./configure
make
sudo make INSTALL_ROOT=/usr/lib64/php/modules install

Even after recompiling, ensure your php.ini reflects the change:

; php.ini
extension_dir = "/usr/lib64/php/modules"

For proper system integration on Linux:

sudo mkdir -p /usr/lib64/php/modules
sudo chmod 755 /usr/lib64/php/modules
echo '/usr/lib64/php/modules' >> /etc/ld.so.conf.d/php.conf
ldconfig