Tag Archives: mcrypt

Install mcrypt PHP extension on Mac OS X Lion

Important: This article was originally published in 2011, and I wouldn’t recommend you use mcrypt in your PHP project any more (reasons why). If you absolutely need it… then read on….

mcrypt php extension

If you have a need to install the mcrypt extension for PHP, for example if you need to use phpMyAdmin, Magento or Laravel, then these instructions are for you.

Thankfully, it is becoming simpler to install PHP extensions than with previous versions of OS X.

Xcode

The Xcode package installs the necessary tools which are needed during the PHP extension compilation process. Make sure you have at least Xcode v4.1 installed; the install package (“Install Xcode”) is available in your “Applications” folder.

If you are running Xcode 4.3 or above, please check you have the command line tools installed before proceeding.

Autoconf

The most recent version of Xcode does not include autoconf which is a pre-requisite. To check that you have it, run the following command in your Terminal:


autoconf --version

If you see output similar to the following, then it means you have it installed — skip to the libmcrypt section.


autoconf (GNU Autoconf) 2.61
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software. You may redistribute copies of it under the terms of
the GNU General Public License .
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.

If you get a command not found error, then you will need to perform the following to install autoconf:


cd /tmp
curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar zxvf autoconf-latest.tar.gz
cd autoconf-2.69
./configure; make
sudo make install

libmcrypt

libmcrypt is the library that provides the encryption functions. We need to install this before building the PHP extension.

  • Open up Terminal.app
  • Download libmcrypt from sourceforge
  • Unpack the archive somewhere
  • cd libmcrypt
  • ./configure
  • make
  • sudo make install

PHP Extension

Once we have libmcrypt installed, we can build the PHP extension. One important point: even though PHP 5.3.8 is now the default since Mac OS X 10.7.3, attempting to compile with these sources results in the following error:

php-5.3.8/ext/mcrypt/mcrypt.c:283: error: ‘PHP_FE_END’ undeclared here (not in a function)

Therefore, we need to use PHP 5.3.6 sources which compiles fine and still works with 10.7.3.

You should see output similar to the following:

Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
  • ./configure
  • make
  • sudo make install

On successful install, you will see the following message:

Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20090626/

To enable the module, include the following line in your /etc/php.ini file:

extension = mcrypt.so

Finally, restart your Apache server:

sudo apachectl restart

And that’s it: you should now be able to utilise the functions, as detailed in the PHP mcrypt documentation.

P.S. – Looking for a reliable hosting provider for your PHP projects? I recommend Clook:

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

mcrypt support for PHP on 64 bit Mac OS X 10.5

Please note: The article below refers to installing mcrypt on Mac OS X 10.5. If you have 10.6 Snow Leopard, please see my updated guide: Install mcrypt PHP extension on OS X Snow Leopard

Adding additional functionality to the standard Apple-supplied PHP on Mac OS X 10.5 is a little tricky if you are running a 64 bit processor such as the Intel Core 2 Duo. The reason is that any dynamic extensions that you add will need to be 64 bit, and many shared libraries by default will compile as 32 bit binaries. Trying to use a 32 bit extension with a 64 bit PHP results in the following unfriendly error message:

PHP Warning: PHP Startup: Unable to load dynamic library './mcrypt.so' - (null) in Unknown on line 0

mcrypt is a good example of a useful extension that can be added to PHP with a little bit of effort:

  • Open up your Terminal.app
  • To explicitly build for 64 bit architecture export CFLAGS="-arch x86_64"
  • Download libmcrypt from sourceforge http://sourceforge.net/projects/mcrypt
  • Unpack the archive
  • cd libmcrypt
  • ./configure --disable-shared
  • make
  • sudo make install
  • download PHP 5.2.6 source from http://www.php.net/get/php-5.2.6.tar.bz2/from/a/mirror
  • unpack the archive and go into the php-5.2.6/ext/mcrypt/ dir
  • phpize
  • ./configure
  • make
  • sudo make install
  • verify the extension is 64 bit: file /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so

/usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so: Mach-O 64-bit bundle x86_64

To actually use the extension, you can simply create a symbolic link to it. For example:

  • cd ~/Sites
  • ln -s /usr/lib/php/extensions/no-debug-non-zts-20060613/mcrypt.so

Example code: mcrypt.php

Drop the following code into your ~/Sites directory to verify everything is working:

    <?php
    
    if ( ! extension_loaded('mcrypt') ) {
        dl('mcrypt.so');
    }
    
    $key   = "this is a secret key";
    $input = "Let us meet at 9 o'clock at the secret place.";
    
    $td = mcrypt_module_open('tripledes', '', 'ecb', '');
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted_data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    
    print_r($encrypted_data);
    
    ?>

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn