Keywords: PHP 7 | Redis Extension | Installation Configuration
Abstract: This article provides a detailed examination of multiple methods for installing Redis extension in PHP 7 environments, including downloading specific versions via wget, installing official packages through apt-get, using pecl commands, and special considerations for Docker environments. The analysis covers advantages and disadvantages of each approach, with complete installation steps and configuration guidance to help developers select the most appropriate solution for their specific environment.
In PHP development, Redis serves as a high-performance key-value storage system, and installing its PHP extension presents practical challenges for many developers. Particularly in PHP 7 environments, version compatibility requires special attention. This article systematically introduces multiple installation methods based on community best practices.
Downloading Specific Versions Using wget
When official links for specific versions become unavailable, downloading stable releases provides an alternative. The original tutorial's link https://github.com/phpredis/phpredis/archive/php7.zip may no longer work, requiring substitution with version-specific URLs. For instance, version 5.2.2 can be downloaded using:
wget https://github.com/phpredis/phpredis/archive/5.2.2.zip -O phpredis.zip
After downloading, extract and navigate to the directory:
unzip phpredis.zip
cd phpredis-5.2.2
Proceed with standard PHP extension installation:
phpize
./configure
make
sudo make install
Finally, add extension configuration to php.ini:
extension=redis.so
Installing Official Packages via apt-get
For Debian-based systems like Ubuntu, the simplest approach utilizes package management:
sudo apt-get install php-redis
This method automatically handles dependencies and configures the extension. After installation, restart the web server or PHP-FPM service. For Apache:
sudo service apache2 restart
For PHP-FPM:
sudo service php7.0-fpm restart
Verify successful loading using php -m, which should list "redis" among modules.
Installation Using pecl Commands
PECL (PHP Extension Community Library) offers another installation avenue, typically providing the latest versions. Install with:
sudo pecl install redis
During installation, configuration prompts may appear; generally, default values suffice. After completion, add extension configuration to php.ini and restart services. This method facilitates installation of specific versions, such as:
sudo pecl install redis-5.3.4
Docker Environment Installation
Installing Redis extension in Docker containers requires special considerations. For containers based on official PHP images, use this command combination:
pecl install redis
docker-php-ext-enable redis
docker-php-ext-enable is a utility command provided by official PHP Docker images that automatically handles extension enabling and configuration, eliminating manual php.ini editing. This approach proves particularly useful when building Docker images, as these commands can be incorporated into Dockerfiles.
Version Compatibility Considerations
Selecting installation methods requires attention to compatibility between PHP versions and Redis extension releases. PHP 7.0, 7.1, 7.2, 7.3, 7.4, and 8.0 each have specific Redis extension requirements. Generally:
- PHP 7.0-7.2 recommends Redis extension versions 4.x-5.x
- PHP 7.3-7.4 recommends Redis extension version 5.x
- PHP 8.0+ requires Redis extension version 5.3.0 or higher
Check current PHP version with php --version to select appropriate extension versions.
Configuration Verification and Troubleshooting
After installation, perform these verification steps:
- Run
php -m | grep redisto check extension loading - Create a test PHP file to verify connectivity:
<?php
$redis = new Redis();
try {
$redis->connect('127.0.0.1', 6379);
echo "Redis connection successful!";
} catch (Exception $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Common issues and solutions:
- "Class 'Redis' not found" errors indicate improper extension loading; check
php.iniconfiguration - Connection refusals suggest Redis server isn't running or firewall settings need adjustment
- Permission issues require ensuring web server users have access to Redis sockets or ports
Performance Optimization Recommendations
After installation and configuration, consider these optimizations:
- Enable persistent connections to reduce overhead
- Adjust timeout settings based on usage patterns
- Implement Redis clusters or sentinel modes for enhanced availability
- Monitor memory usage of Redis extension
Through these comprehensive method descriptions, developers can select optimal installation approaches for their specific environments. Whether utilizing simple apt-get installations, manual compilation for specific versions, or specialized Docker environment handling, appropriate solutions exist for every scenario.