Question

When I run a command with PHP, it shows me an error. E.g when I run php -v to see my PHP-version it shows me an error then the informations about PHP:

Module 'mcrypt' already loaded in Unknown on line 0

zied@ubuntu:~$ php -v

PHP Warning:  Module 'mcrypt' already loaded in Unknown on line 0

PHP 5.4.25-1+sury.org~quantal+2 (cli) (built: Feb 12 2014 11:11:30) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
Was it helpful?

Solution

Your php.ini contains two or more of these lines:

extension=mcrypt.so

Remove all except one.

php.ini usually lives in /etc/php.ini or /etc/php5/php.ini. Sometimes additional .ini files are included, you can see all of them with:

$ php -i | grep .ini\$
Loaded Configuration File => /usr/local/etc/php.ini
Additional .ini files parsed => /usr/local/etc/php/extensions.ini
user_ini.filename => .user.ini => .user.ini

OTHER TIPS

Sometimes this happens with php-fpm, and the funny thing is, console php doesn't complans about this using the same set of .ini-files in the same time, proving that mcrypt in fact isn't referenced twice.

As it turns out, php-fpm has a default set of modules built-in that it's trying to load, at least on Linux (since that isn't reproducible on FreeBSD). mcrypt is in this list, so when a user has an additional .ini-file in it's /etc/php.d directory, mcrypt seems to be loaded twice.

A harsh workaround for this is to add the -n switch to the php-fpm on the start, copy pnp.ini to a php-fpm.ini, include all of your modules into the resulting php-fpm.ini except mcrypt and add an additional switch pointing at the correct ini-file, so the whole addition looks like: -n -c /etc/php-fpm.ini.

This way running php-fpm won't complain.

I'm writing this here, because this is most referenced post in search engines about mcrypt issue. I realize the source question was about console php.

Update: I was using this workaround, but it is nasty. Some time ago I have figured out exactly why did this happen. I'll spend some more words to describe this, but this can be boring, since this will describe a certain type of failure. So, in my case this issue was caused by the fact, that I was using a custom php build, made by myself, and occasionally I've added the mcrypt to the list of builtin static modules. And then I added it again as a built module, so it was loaded twice. This happens with a custom build when mcrypt is referenced in the list of modules for the configure script, and isn't listed as shared (this part of the spec can be easily found, since %configure \ is mentioned only once in the spec). In my case the solution was to remove the mcrypt entirely from the configure part, and add it to the build-cgi and build-ztscli stages. One could ask " What about the fpm stage ?" - and it's a good question, but, it turns out, fpm sapi itself is built with a minimum of modules and uses generic shared ones.

I had the same issue and it was due to building PHP from source with ./configure --with-mcrypt option. It seems that if PHP is built with the --with-mcrypt flag, then there is no need to specify extension=mcrypt.so in php.ini. Doing so causes the warning mentioned above.

Open php.ini and find extension=mcrypt.so

Comment it by adding a semicolon before the name extension ;extension=mcrypt.so

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top