Befire installation
- apt list --upgradable # check for php8.3 version installed
- apt remove php8.3-dev php8.3-test... # uninstall all php8.2 extension so that it wll not conflict to php8.2
- apt autoremove
#To start fresh and install the rdkafka extension for PHP 8.2 only, while ensuring that PHP 8.3 is not affected, follow these steps carefully. I'll also include verification steps after each part.
Steps for Fresh Installation of rdkafka Extension for PHP 8.2 (CLI and FPM)
1. Remove any previous rdkafka installations
We will first remove any existing configurations or installations related to the rdkafka extension.
# Remove the current rdkafka.so files
sudo rm -f /usr/lib/php/8.2/cli/rdkafka.so
sudo rm -f /usr/lib/php/8.2/fpm/rdkafka.so
sudo rm -f /etc/php/8.2/cli/conf.d/20-rdkafka.ini
sudo rm -f /etc/php/8.2/fpm/conf.d/20-rdkafka.ini2. Install dependencies
We need to install the required libraries and tools for building PHP extensions and Kafka support.
# Install build dependencies
sudo apt update
sudo apt install -y php8.2-cli php8.2-dev php8.2-fpm build-essential librdkafka-dev
# Install pecl if not already installed
sudo apt install -y php-pear3. Install the rdkafka extension via PECL for PHP 8.2
Use the PECL command to install the rdkafka extension. Since your server has both PHP 8.2 and PHP 8.3, we will ensure to install for PHP 8.2 specifically.
# Install rdkafka extension for PHP 8.2
sudo pecl install rdkafka4. Enable the rdkafka extension in PHP 8.2 for CLI and FPM
Once the extension is installed, we need to enable it for both PHP 8.2 CLI and FPM.
# Enable rdkafka extension for CLI
echo "extension=rdkafka.so" | sudo tee /etc/php/8.2/cli/conf.d/20-rdkafka.ini
# Enable rdkafka extension for FPM
echo "extension=rdkafka.so" | sudo tee /etc/php/8.2/fpm/conf.d/20-rdkafka.ini5. Verify the installation for CLI and FPM
After installation, let's verify that rdkafka is loaded correctly for both PHP CLI and PHP-FPM.
# Verify PHP CLI
php -m | grep rdkafka
# Verify PHP-FPM
sudo systemctl restart php8.2-fpm
# Verify PHP-FPM
sudo php-fpm8.2 -m | grep rdkafka6. Check PHP version
Make sure the correct version of PHP is being used for CLI and FPM.
# Check PHP CLI version
php -v
# Check PHP-FPM version
php-fpm8.2 -v7. Restart PHP-FPM and Apache/Nginx (if applicable)
After enabling the extension, restart PHP-FPM and web server (if you use Apache or Nginx) to make sure the extension is loaded correctly in the web environment.
# Restart PHP-FPM for PHP 8.2
sudo systemctl restart php8.2-fpm
# Restart web server (e.g., Nginx or Apache)
sudo systemctl restart nginx # if using Nginx
# or
sudo systemctl restart apache2 # if using Apache8. Verify the rdkafka extension in your web environment
Now, verify if the rdkafka extension is working with a simple PHP script.
Create a PHP file to check the loaded modules:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.phpVisit http://your_server_ip/phpinfo.php and look for rdkafka in the output to confirm it is loaded.
Summary of the Steps
- Remove previous installations of
rdkafka. - Install required dependencies and PHP 8.2 development packages.
- Install
rdkafkaextension usingpecl. - Enable the extension in the appropriate
.inifiles for CLI and FPM. - Restart PHP-FPM and web server.
- Verify the installation with
php -mandphpinfo().
Final Verification
After following the steps above, you should see the rdkafka extension listed when running php -m, and it should be active both in the CLI and FPM contexts. If everything is successful, the warning should be gone and the extension will be properly loaded. Let me know how it goes!