Compiling PHP 5.3.1 with mcrypt on Snow Leopard en

By --MeAngry-- on Thursday 3 December 2009 17:17 - Comments (6)
Categories: PHP, Snow Leopard, Views: 14.565

Today I will be posting my first (useful) blog post ever!

Maybe not such a big deal for you, but definitely for me. Maybe even for others if this helps anybody out.
I've decided that I want to share solutions I find to problems that I face in my daily work as a web developer, so that others might have an easier time figuring things out.

I will discuss compiling PHP 5.3 on Snow Leopard to begin with.
Why? I hear you asking, because Snow Leopard already comes supplied with a pretty decent 5.3 version.
Well, because I do all of my web developing work on OS X and like an up to date version running with all the extensions I need.
One of which is the mcrypt extension, which isn't enabled by Apple by default, but for example, is needed by phpMyAdmin on 64-bit PHP installations.

There are several caveats to overcome when compiling PHP on Snow Leopard, but Apple was so kind to provide patches for some of those problems.

As a side-note: please read the whole tutorial first, before attempting to really perform all the actions I'm mentioning, because you might need some knowledge beforehand.

What do we need?

Preparations

First, the Apple Xcode Developer Tools. This is the free IDE that Apple supplies for building OS X applications. What it also provides is a standard UNIX compatible build environment with all the required compilers, linkers and other tools we need to compile applications for OS X.
You need an Apple account to download it for free, but when you're reading this, chances are you already have one.

Just follow the installation routine, and you should end up with a working development environment. (We're not going to use Xcode itself, but it might be worth looking at if you're a developer.)

Regarding the other source distributions, I suggest creating a folder Source somewhere so you can keep everything you need together and find it back easily.

Place all of the downloaded components in this newly created folder and extract them. The built-in archiver of OS X will put them in separate folders by itself.

Next, we need to open up the Terminal.app. Type:

code:
1
cd <path-to-Sources-folder>


You can do this easily by typing cd, space, and then drag the Source folder you created from the Finder into the Terminal window.

Now you need to gain superuser rights over your own machine to be able to install everything in the right place.


code:
1
sudo -s



Type your password when asked. It's the same password you use to install programs.

Be very careful from here on, as one wrong command could break things seriously or erase all of your files!

Compiling external libraries

Wait for every command to finish before executing the next one.


code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export MACOSX_DEPLOYMENT_TARGET=10.6 \
CFLAGS="-arch x86_64" \
CXXFLAGS="-arch x86_64"

cd libmcrypt-2.5.8/
./configure
make
make install

cd ../jpeg-7/
./configure
make
make install

cd ../libpng-1.2.40/
./configure
make
make install



Now, the libraries we need to build PHP with all the required extensions are in place.

Compiling PHP

Before we can get to compiling PHP itself, we need to apply a fix to the source files of iconv, or we will get linking errors.

Download this patch from Apple:
http://opensource.apple.c...hp-53/patches/iconv.patch

And save it in your Source folder.

Then in Terminal:

code:
1
2
cd ../
patch php-5.3.1/ext/iconv/iconv.c iconv.patch



Older PHP versions have more stuff that needs fixing, but I'm not covering that in this post. Ask questions if you like, but I'm not sure if I have time to look into them.

You might want to check the other patches Apple supplies in this case, as they might fix the specific problem you might have:
http://opensource.apple.c...pache_mod_php-53/patches/

Now we can get to compiling PHP!

In Terminal again:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
./configure \
'--prefix=/usr' \
'--mandir=/usr/share/man' \
'--infodir=/usr/share/info' \
'--sysconfdir=/private/etc' \
'--with-apxs2=/usr/sbin/apxs' \
'--enable-cli' \
'--with-config-file-path=/etc' \
'--with-libxml-dir=/usr' \
'--with-openssl=/usr' \
'--with-kerberos=/usr' \
'--with-zlib=/usr' \
'--enable-bcmath' \
'--with-bz2=/usr' \
'--enable-calendar' \
'--with-curl=/usr' \
'--enable-exif' \
'--enable-ftp' \
'--with-gd' \
'--with-jpeg-dir=/usr/local' \
'--with-png-dir=/usr/local' \
'--enable-gd-native-ttf' \
'--with-ldap=/usr' \
'--with-ldap-sasl=/usr' \
'--enable-mbstring' \
'--enable-mbregex' \
'--with-mysql=mysqlnd' \
'--with-mysqli=mysqlnd' \
'--with-pdo-mysql=mysqlnd' \
'--with-iodbc=/usr' \
'--enable-shmop' \
'--with-snmp=/usr' \
'--enable-soap' \
'--enable-sockets' \
'--enable-sysvmsg' \
'--enable-sysvsem' \
'--enable-sysvshm' \
'--with-xmlrpc' \
'--with-iconv-dir=/usr' \
'--with-xsl=/usr' \
'--with-pcre-regex' \
'--with-mcrypt'

make
make install



If everything has gone as planned you should now have a working PHP 5.3.1 module!

Activate PHP

Now to make PHP active in your Apache configuration. Luckily this isn't very hard.

We're back in the Terminal:

code:
1
nano /etc/apache2/httpd.conf



Press Ctrl + W, and search for 'php' (without quotes)

Now uncomment the first line that comes up. It should look like this:

code:
1
#LoadModule php5_module        libexec/apache2/libphp5.so



Remove the # at the beginning to make it look like this:

code:
1
LoadModule php5_module        libexec/apache2/libphp5.so



Press Ctrl + O to save the file.
Press Ctrl + X to quit nano.

Enable / restart Apache

This is the final step!
Open up System Preferences and click Sharing. Find the tickbox Web Sharing on the left side and untick it, if it is already ticked, and tick it again to enable Apache.

You should now have a working PHP 5.3.1 module with the mcrypt extension running in your Apache installation on Snow Leopard!

Final words

This is my first real tutorial, so I can fully understand if you don't think it's very clear or understandable. Be warned that it's preferred that you have some terminal skills before following this tutorial.

If all of this looks a bit too much like magic for you, consider using MAMP or XAMPP which supply ready to run Apache installations with PHP and a lot of modules.

This tutorial is meant for people that like to keep their PHP installation as close to the original and as integrated with the system as possible.

Comments


By Tweakers user DexterDee, Friday 4 December 2009 10:39

I always consider compiling from source as one of the last options. For decent support of extensions in php 5.3 I use the entropy packages.

http://www2.entropy.ch/download/Entropy%20PHP%205.3.0-3.pkg

This installs a separate PHP 5.3 environment that can be easily linked into apache, so it allows an upgrade path for the build-in PHP environment in (snow) leopard. I am a pretty heavy user of PHP functionality, so I need support for a lot of extensions and compiler options for my projects (pcntl, shmop, msg_queues, memcache, mcrypt, gd, imap, xml, ...). The Entropy package delivers these functionalities and I have yet to see any problem with it.

By Tweakers user --MeAngry--, Friday 4 December 2009 13:43

I am actually familiar with the Entropy packages, but last time I checked these were not compatible with Snow Leopard. The module just wouldn't run, that's why I decided to compile my own binaries. :)

But Entropy is indeed a good suggestion for users of Leopard!

By iongion, Saturday 2 January 2010 12:03

Nothing works, the Snow Leo throws this:

i686-apple-darwin10-gcc-4.2.1: ext/mcrypt/mcrypt.o: No such file or directory
make: *** [libs/libphp5.bundle] Error 1

And if i remove mcrypt from the list of features to be configured, then a big iconv errors appears (i have applied the patch)

By Tweakers user --MeAngry--, Sunday 3 January 2010 18:12

At which point do you get that error? While compiling PHP? If so, which PHP package did you use?

By Marianne Tromp, Monday 1 February 2010 23:12

Thank you for these instructions. It made it possible for me to install a local copy of Zen Cart for development purposes.

By Steve, Wednesday 24 March 2010 02:10

I have the Mac OS X 10.6.2 Snow Leopard, and as far as I know it comes with PHP 5.3.0

Do I still need to download PHP 5.3.1? Sorry I complete n00b here.

Comments are closed