From charlesreid1

Installation

Preqrequisites

I have built my PHP to work with Apache. I also build PHP to work with phpMyAdmin, which is a very nice and handy browser interface for MySQL databases. Building PHP to work with phpMyAdmin required installing the mcrypt library, which provides PHP with encryption libraries.

The mcrypt utility provides access to cryptographic functions, while libmcrypt is a library containing the actual cryptographic functions themselves.

Mcrypt: http://mcrypt.sourceforge.net/

Libmcrypt: http://freshmeat.net/projects/libmcrypt/

It's also imperative that you have a web server like Apache installed - otherwise you will only be able to use PHP from the command line, which isn't terribly useful. If you are using Apache, you must include the Apache PHP module in your Apache httpd.conf file. However, if you point PHP configure to your Apache installation (see next section), the command to load the PHP module should be automatically added to your httpd.conf file. Visit the Apache#PHP Module page for more details.

Configuration

Version 5.3.8 and Mac Snow Leopard (OS X 10.6)

This version was a pain to get built, as there are several old bugs in PHP that, although "fixed", gave me lots of trouble. See the Errors section below before continuing if you are trying to install PHP 5.3.8 on Mac Snow Leopard.

The MySQL library problem may go away if you do not use Oracle's MySQL binary for Mac. However, building MySQL from source was nearly impossible the last time I tried, so I just stick with binaries.

Not Version 5.3.8 and Not Mac Snow Leopard

The configure process for PHP has two parts. The first part is telling PHP which extensions to build, and the second is actually configuring/building. However, you can't actually do the first step until you do the second step - so you have to build PHP, then build the extensions.

The PHP configure line I use is:

#!/bin/sh
# run configure
# make
# make install

./configure \
 --prefix=$HOME/pkg/php-5.3.0 \
 --with-mysql=/usr/local/mysql \
 --with-apxs2=$HOME/pkg/apache-2.2.11/bin/apxs \
 --disable-cgi \
 --with-zlib \
 \
 --enable-mbstring \
 \
 --enable-pdo \
 --with-pdo-mysql=/usr/local/mysql \
 \
 --with-mcrypt=$HOME/pkg/libmcrypt-2.5.8 \
 \
 --enable-sockets

The mcrypt functionality, as well as the PDO and mbstring functionality, are required to get PHP working with phpMyAdmin. The apxs option is required to get PHP working with Apache. I included sockets because they're fun and interesting.

Then you run make and make install, and if you really want to, make test.

Now that you've installed PHP, you've also installed a utility called phpize, which allows you to install extensions. To build PHP extensions, you have to tell PHP configure that you want to build them. So go to the directory containing your configure file, then to a directory called ext. This contains PHP extensions.

/path/to/php/source/ext $ /bin/ls -1C
bcmath                  libxml                  session
bz2                     mbstring                shmop
calendar                mcrypt                  simplexml
com_dotnet              mssql                   skeleton
ctype                   mysql                   snmp
curl                    mysqli                  soap
date                    mysqlnd                 sockets
dba                     oci8                    spl
dom                     odbc                    sqlite
enchant                 openssl                 sqlite3
ereg                    pcntl                   standard
exif                    pcre                    sybase_ct
ext_skel                pdo                     sysvmsg
ext_skel_win32.php      pdo_dblib               sysvsem
fileinfo                pdo_firebird            sysvshm
filter                  pdo_mysql               tidy
ftp                     pdo_oci                 tokenizer
gd                      pdo_odbc                wddx
gettext                 pdo_pgsql               xml
gmp                     pdo_sqlite              xmlreader
hash                    pgsql                   xmlrpc
iconv                   phar                    xmlwriter
imap                    posix                   xsl
interbase               pspell                  zip
intl                    readline                zlib
json                    recode
ldap                    reflection

These are all the extensions that come with PHP. To build an extension when you build PHP, do this:

$ cd mcrypt
$ phpize
$ cd ../

You can phpize any extensions you want, but you'll have to include the corresponding configure option. For example, I can phpize mcrypt, but if I don't point PHP configure to my mcrypt library, then the phpize is useless. Likewise, if I point PHP configure to my mcrypt library, but don't phpize mcrypt, PHP doesn't do anything with the location to mcrypt.

Once you've phpized the extensions you want, you can go back to the source directory (where your configure script is located), run configure one more time, and then run make and make install. If you didn't run make clean in the meantime, it won't have to re-build PHP, it will only build the extensions.

Once you've built the extensions, you're good to go - you can enable the extensions in your PHP configuration file (below).

Errors

PHP 5.3.8 on Mac

PHP 5.3.8 on Mac is BROKEN in just about every case. If you use the configure lines below you'll get errors like this:

Undefined symbols:
  "_res_9_dn_expand", referenced from:
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _zif_dns_get_mx in dns.o
  "_res_9_search", referenced from:
      _zif_dns_check_record in dns.o
      _zif_dns_get_mx in dns.o
      _zif_dns_get_record in dns.o
  "_res_9_init", referenced from:
      _zif_dns_check_record in dns.o
      _zif_dns_get_mx in dns.o
      _zif_dns_get_record in dns.o
  "_res_9_dn_skipname", referenced from:
      _zif_dns_get_mx in dns.o
      _zif_dns_get_mx in dns.o
      _zif_dns_get_record in dns.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1
make: *** Waiting for unfinished jobs....
Undefined symbols:
  "_res_9_dn_expand", referenced from:
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _php_parserr in dns.o
      _zif_dns_get_mx in dns.o
  "_res_9_search", referenced from:
      _zif_dns_check_record in dns.o
      _zif_dns_get_mx in dns.o
      _zif_dns_get_record in dns.o
  "_res_9_init", referenced from:
      _zif_dns_check_record in dns.o
      _zif_dns_get_mx in dns.o
      _zif_dns_get_record in dns.o
  "_res_9_dn_skipname", referenced from:
      _zif_dns_get_mx in dns.o
      _zif_dns_get_mx in dns.o
      _zif_dns_get_record in dns.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [libs/libphp5.bundle] Error 1

Finding a solution to this was a PAIN. Each solution I found solves this problem differently. It would be nice if PHP would fix these 2-year-old bugs to keep this from happening:

https://bugs.php.net/bug.php?id=49332

https://bugs.php.net/bug.php?id=49267

The solution to this is to modify the Makefile. Look for the line beginning with EXTRA_LIBS:

EXTRA_LIBS = -lmysqlclient -liconv -liconv -lz -lm -lxml2 ......

Now add -lresolv to this flag:

EXTRA_LIBS = -lsresolv -lmysqlclient -liconv -liconv -lz -lm -lxml2 ......


Library not loaded: libmysqlclient.18.dylib

Once I solved the issue mentioned above, I ran into ANOTHER error:

Generating phar.php
dyld: Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/charles/pkg/_sources/php-5.3.8/sapi/cli/php
  Reason: image not found
/bin/sh: line 1:  4759 Trace/BPT trap

I found the solution to this bug here:

http://stackoverflow.com/questions/7250484/php-isnt-linking-mysql-shared-library-correctly-on-build

From the source code directory (the same location from which I run configure), I ran the following command after running configure but before running any make commands:

$ install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib $PWD/sapi/cli/php

I found similar solutions from several Google searches, but again everyone had different solutions, and I could not find a single solution that worked for everyone.

PHP Information

If you're ever unsure about the PHP settings, where configuration files are located, which extensions are installed, etc., then you can create a PHP file with any name (I use info.php), with the following PHP contents:

<?php
info();
?>

Then you can put this in your web directory, and go to http://localhost/info.php. This will output a bunch of information about your PHP install.

PHP Configuration File

The PHP configuration file is called php.ini. If you're not sure where it's located, use the above info() function - it will give you the location of your php.ini file.