PHP
From charlesreid1
Contents
Old Installation Instructions
These instructions are dated. Use at your own risk. |
Mac users: you will have issues if you try and compile PHP 5.3.8 from source. Use a different version of PHP (or, a binary version) from the PHP releases page: http://www.php.net/releases/ (alternatively, download a binary version). See the #Errors section below before continuing if you decide you must install this version. |
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.
Short Configure Line
A short PHP configure line that 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/std/bin/apxs \
--disable-cgi \
--with-zlib \
\
--enable-mbstring \
\
--enable-pdo \
--with-pdo-mysql=/usr/local/mysql \
\
--with-mcrypt=$HOME/pkg/libmcrypt/std \
\
--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.
Long Configure Line
A long PHP configure line that I use is:
#!/bin/sh
# this is required if linking to version of libpng from ubuntu's aptitude:
sudo ln -fs /usr/lib/i386-linux-gnu/libpng.a /usr/lib/libpng.a
./configure \
--prefix=$HOME/pkg/php/5.3.6 \
--with-mysql=/usr \
--with-apxs2=$HOME/pkg/apache/std/bin/apxs \
--disable-cgi \
--with-zlib=/usr \
\
--disable-ipv6 \
--enable-inline-optimization \
--disable-debug \
--enable-bcmath \
--enable-calendar \
--disable-ipv6 \
--enable-inline-optimization \
--disable-debug \
--enable-bcmath \
--enable-calendar \
\
--enable-ctype \
--enable-dbase \
--enable-discard-path \
--enable-exif \
--enable-ftp \
\
--with-gd \
--with-freetype-dir \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
\
--enable-magic-quotes \
--enable-soap \
--with-pear \
--with-openssl \
\
--enable-mbstring \
--enable-pdo \
--with-pdo-mysql=/usr \
--with-mcrypt=$HOME/pkg/libmcrypt/std \
--enable-sockets \
\
--sysconfdir=$HOME/pkg/php/conf \
Installing Modules
Once you install PHP, you have a utility called phpize which allows you to install extensions.
To build PHP extensions, you first have to tell PHP that you want to build them. Still in the source code directory, go to the ext/
directory, which 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. You'll probably see 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 huge pain. Each solution I found solves this problem differently. It would be nice if PHP made sure old bugs didn't return with new versions. This problem is described in two separate 2-year-old bug reports:
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 ......
Or, add this to your configure script, after you run configure:
sed -ie 's/EXTRA_LIBS = -lmy/EXTRA_LIBS = -lresolv -lmy/' Makefile
(Since Mac's sed is obnoxiously different from GNU's sed, you'll probably have to tinker with the flags).
Library not loaded: libmysqlclient.18.dylib
Once I solved the issue mentioned above, I ran into ANOTHER error, this one caused by MySQL:
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
This problem originates from the binary version of MySQL from Oracle's web page, version 5.5.17, for Mac OS X 10.6, 64-bit. One of the libraries included with this binary of MySQL does not have an absolute path, it has a relative path:
$ otool -L /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib: libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)
I found a solution from the MySQL bug system:
http://bugs.mysql.com/bug.php?id=61243
First, repair the library directly:
$ sudo install_name_tool -id /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib <pre> Then verify that this worked: <pre> $ otool -L /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib: /usr/local/mysql-5.5.17-osx10.6-x86_64/lib/libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1)
Now run:
make clean; make all; make install
This worked like a charm for me.
Libpng Configure Problem
While trying to build PHP with gd/libpng/libjpeg, I kept seeing messages like this about libpng or libjpeg:
checking for GD support... yes checking for the location of libpng... /usr/local/src/libpng-1.2.8/ If configure fails try --with-jpeg-dir=<DIR> configure: error: libpng.(a|so) not found.
I was on Ubuntu Linux and had installed the following libraries through aptitude:
sudo apt-get install libpng3 libpng3-dev libjpeg8 libjpeg8-dev
However, these are not found by PHP's configure because Ubuntu does not put them all in sensible locations. Some of them (libjpeg libraries) go to /usr/lib, but some of them (libpng libraries) go to /usr/lib/i386-linux-gnu.
There's no way to really permanently fix this, unless PHP adds configure flags like --with-png-lib-dir
or --with-jpeg-lib-dir
.
PHP Installation 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.
Alternatively, specify the directory in which to put php.ini using the configure flag --with-config-file-path
.