First, diagnose whether the mcrypt extension is available on your system.
php -m | grep mcrypt
Interpretation:
mcrypt in the output, the extension is installed but might not be properly enabled in your web server's PHP configuration.Also check your PHP version to determine the correct approach:
php -v
sudo apt update
sudo apt install php-mcrypt -y
sudo yum install php-mcrypt -y
After installation, restart your web server and PHP processor:
# For Apache
sudo systemctl restart apache2
# For Nginx with PHP-FPM
sudo systemctl restart php7.4-fpm # Adjust version if different
sudo systemctl restart nginx
sudo apt install php-sodium -y
sudo yum install php-pecl-sodium -y
# For Apache
sudo systemctl restart apache2
# For Nginx with PHP-FPM
sudo systemctl restart php7.4-fpm # Adjust version if different
If OpenCart still throws mcrypt errors after installing Sodium, you may need to modify its encryption function. First, backup the file!
sudo cp /var/www/html/opencart/system/library/encryption.php /var/www/html/opencart/system/library/encryption.php.backup
sudo nano /var/www/html/opencart/system/library/encryption.php
Find this line (around line 30-40):
$this->key = hash('sha256', $key, true);
Replace it with:
$this->key = sodium_crypto_generichash($key);
Save the file (Ctrl+O, Enter, Ctrl+X) and restart your web server:
sudo systemctl restart apache2 # or nginx
If mcrypt is installed but not being loaded by your web server's PHP, you need to enable it in the correct php.ini file.
# For command-line PHP
php --ini | grep "Loaded Configuration File"
# For web PHP (Apache)
sudo find /etc -name "php.ini" | grep apache2
# For web PHP (Nginx/PHP-FPM)
sudo find /etc -name "php.ini" | grep fpm
# Example for Apache on Ubuntu/Debian
sudo nano /etc/php/7.4/apache2/php.ini
# Example for PHP-FPM on Ubuntu/Debian
sudo nano /etc/php/7.4/fpm/php.ini
# Example for CentOS/RHEL
sudo nano /etc/php.ini
Find the extension=mcrypt.so line (or similar) and uncomment it by removing the semicolon at the beginning:
;extension=mcrypt.so # Change this to:
extension=mcrypt.so
If the line doesn't exist, add it to the extensions section.
# For Apache
sudo systemctl restart apache2
# For Nginx with PHP-FPM
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx
After applying your chosen solution, verify the extension is active:
# Check for mcrypt (if using PHP 7.4 or older)
php -m | grep mcrypt
# Check for sodium (if using PHP 7.2+)
php -m | grep sodium
You should see the extension name in the output. Additionally, you can create a simple PHP info file to check web server configuration:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/test_phpinfo.php
Visit http://yourdomain.com/test_phpinfo.php in your browser and search for "mcrypt" or "sodium". Remember to delete this file after testing:
sudo rm /var/www/html/test_phpinfo.php
| Issue | Solution |
|---|---|
| Mcrypt not installed (PHP 7.4 or older) | sudo apt install php-mcrypt -y (Ubuntu/Debian) |
| Mcrypt missing in PHP 7.2+ | Install Sodium: sudo apt install php-sodium -y |
| Mcrypt installed but not enabled | Add/uncomment extension=mcrypt.so in correct php.ini |
| PHP changes not applied | Restart Apache: sudo systemctl restart apache2 |
| OpenCart still requires mcrypt modification | Edit /system/library/encryption.php to use Sodium functions |
Following the appropriate solution based on your PHP version will resolve the "mcrypt extension needs to be loaded" error in OpenCart!