One of the requirements for Magento 2 is mcrpyt, however in PHP7.1 this extension is now officially deprecated. For those who still want to use PHP7.1 for the other benefits it offers (features, performance) or are stuck with it (shared hosting?) and don’t have mcrypt available to use, you’ll have a hard time getting Magento 2 to play ball with the server running PHP7.1.
You’ll come across errors when running bin/magento setup:upgrade
You may also come across similar errors when trying to access your Magento 2 website in general; you have a few options to get around this issue…
Update your Magento 2 version to the latest version:
2.2 at time of writing, is the version i noticed that fixes the mcrypt error (and thankfully a ton of other errors), however in a realistic world, it’s not always feasible to do so. If upgrading Magento to version 2.2 is not an option, this still leaves us with two other possible solutions; suppress the error or change the Encryption/Crypt.php file.
Suppressing the error (or any deprecation errors):
In your index.php file (whichever it serves from), add the following
error_reporting
(E_ALL & ~E_DEPRECATED);
or
error_reporting
(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
This should help suppress any deprecation errors that it throws, however don’t be surprised if when in developer mode deploy:mode:set developer
it changes your preferred error reporting settings.
Change the Encryption/Crypt.php file:
The last solution to the problem is to make some changes in the Crypt.php file itself. I don’t condone “hacking the core”, however if this is your last resort ie, your sites unusable from this error, it might be your only solution. If you’re on Magento 2.1.6 and are soon upgrading to Magento 2.2, the upgrade will override/patch this file to add in the same changes.
Theres an operator in PHP that allows you to individually suppress any errors that come from a piece of code. You can add the @
symbol in front of an expression and “hey presto”, no errors.
We can use this to suppress calls to instances of mcrypt so our Magento 2 store will run again. Go through the Encryption/Crypt.php file and add @
in front of any of the calls to the mcrypt extension.
mcrypt_module_open
will now be @mcrypt_module_open
mcrypt_enc_get_key_size
will now be @mcrypt_enc_get_key_size
mcrypt_module_close
will now be @mcrypt_module_close
… You get the idea
The best solution here, if possible would be to update your Magento 2 version. If that isn’t possible then try the above solutions to get your Magento 2 store back up and running again. If you went down the route of changing the Encryption/Crypt.php file, heres the patched version of it for you to use which should be backwards compatible (tested on Magento 2.1.6 and PHP7.1) Encryption/Crypt.php
Read our other Learn articles to see more from our Magento developers or browse the site to see what else we do with Adobe Commerce, PunchOut Catalog and more.
Looking to optimise your eCommerce site?
Mike Sheward
Technical Director
Mike is our Technical Director who has worked on eCommerce projects since 2014 and has been with Develo since 2015. He became certified in Adobe Commerce in 2023. Mike’s favourite aspect of web development is tackling intricate eCommerce challenges with the bespoke platform Magento/Hyvä, and he tells us his best achievement from working at Develo is fostering a culture of growth and innovation within our team. Outside of work, he can be found spending time with his family, baking and walking his dogs.
Develo is a leading Magento agency and eCommerce web development company based in Birmingham, in the UK, serving clients globally since 2010. We provide our Learn articles to share knowledge of magento web development, web app development and more to the wider web developer community. To extend your team with our white label web developers, collaborate or consult with us on your project contact us here.