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.
- 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 - Install Dependencies and Compile the Extension
First, install the PHP development package, which includes compilation tools like
phpize.yum install php-devel -yThen, use
phpizeto 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
- Locate the Extension File
After successful compilation, the extension file
rar.sois installed in PHP's extension directory. If you are using an environment like the LNMP one-click installation package whereextension_diris not explicitly set inphp.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.sofile is usually located there. - 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 - 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 restartCreate 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
rarorunraron the server and call them via PHP functions likeexec().