18

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
hakre
  • 193,403
  • 52
  • 435
  • 836
Zied R.
  • 4,964
  • 2
  • 36
  • 67

4 Answers4

45

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
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
  • 9
    Thanks a lot , i deleted the line **extension=mcrypt.so ** in **/etc/php5/mods-available/mcrypt.ini** and the error is disappeared now thnx – Zied R. Feb 21 '14 at 02:06
  • 3
    This happened to me when I upgraded from PHP 5.6.15 to PHP 7.0 on my Debian DigitalOcean droplet. I've had no problems developing with this error showing; however, it is still very annoying when running stuff in the shell. Your answer is very much appreciated :). – Rob Dec 18 '15 at 07:50
  • 2
    I also faced this issue when I upgraded from PHP 5.5.9 to PHP 7.0 on my ubuntu 14.04 machine. And I found solution from https://www.digitalocean.com/community/questions/php-7-0-php-warning-module-mcrypt-already-loaded-in-unknown-on-line-0 so I just remove mcrypt module : sudo apt-get remove php7.0-mcrypt as PHP7.0 from ondrej is already compiled with mcrypt. – Sabeeh Chaudhry Dec 31 '15 at 16:13
3

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.

drookie
  • 179
  • 3
  • 11
2

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.

W.M.
  • 589
  • 1
  • 6
  • 13
1

Open php.ini and find extension=mcrypt.so

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

faye.babacar78
  • 774
  • 7
  • 12