Blog / Linux/ Compiling and Installing the RAR Extension for PHP on CentOS 6.9

Compiling and Installing the RAR Extension for PHP on CentOS 6.9

在 CentOS 6.9 上为 PHP 编译安装 RAR 扩展

Adding RAR Compression and Extraction Support for PHP on CentOS 6.9

This guide explains how to compile and install the RAR extension for PHP on CentOS 6.9, enabling support for compressing and extracting RAR format files. Ensure the RAR extension version is compatible with your PHP version.

Version Compatibility

Before installation, select the appropriate RAR extension version based on your PHP version:

  • RAR 4.0.0: Requires PHP 5.3.0 or newer, and PEAR 1.4.0 or newer.
  • RAR 3.0.2 and 3.0.1: Requires PHP 5.2.0 or newer, and PEAR 1.4.0 or newer.

Important: Download the extension package that matches your server's actual PHP version; do not blindly follow the version numbers in this example.

Installation Steps

The following steps use RAR 4.0.0 as an example, assuming your PHP installation path is /usr/local/php.

  1. Download and Extract the Extension Source
    wget http://pecl.php.net/get/rar-4.0.0.tgz
    tar -zxvf rar-4.0.0.tgz
    cd rar-4.0.0
  2. Install Dependencies and Compile the Extension

    First, install the PHP development package, which includes compilation tools like phpize.

    yum install php-devel -y

    Then, use phpize to prepare the build environment and compile the extension.

    phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config
    make
    make install

Common Error Resolution

If you encounter the following error during ./configure:

configure: error: C++ preprocessor "/lib/cpp" fails sanity check

This indicates a missing C++ compilation environment. Install the required packages:

yum install glibc-headers gcc-c++ -y

After installation, rerun the ./configure step.

Configuring PHP to Enable the Extension

  1. Locate the Extension File

    After successful compilation, the extension file rar.so is installed in PHP's extension directory. If you are using an environment like the LNMP one-click installation package where extension_dir is not explicitly set in php.ini, the extension is typically installed to:

    /usr/local/php/lib/php/extensions/

    Look for a subdirectory containing "no-debug-non-zts" within this path; the rar.so file is usually located there.

  2. Edit php.ini

    Edit your PHP configuration file (e.g., /usr/local/php/etc/php.ini) and add the following line at the end to enable the extension:

    extension = rar.so
  3. Restart Services and Verify

    Restart your web server (e.g., Nginx or Apache) and PHP-FPM service.

    # Example: Restart PHP-FPM and Nginx
    service php-fpm restart
    service nginx restart

    Create a PHP file containing phpinfo(); and access it via a browser. Search for "rar" in the output page. If you see information about the RAR extension, the installation was successful.

Summary and Notes

  • The core of the process is version matching, resolving compilation dependencies, and correctly configuring php.ini.
  • The CentOS 6.9 system repositories are outdated. If you encounter issues installing dependency packages, you may need to configure the EPEL repository.
  • The RAR extension only provides functionality for reading and extracting RAR files. To create RAR archives, you typically need to install the command-line tools rar or unrar on the server and call them via PHP functions like exec().

Post a Comment

Your email will not be published. Required fields are marked with *.