PHP 4.3 and Mac OS Xby Adam Trachtenberg, coauthor of PHP Cookbook
With the recent release of PHP 4.3, Macintosh users can now configure and build PHP themselves. This is a great boon. Finally, Mac OS X users have the same level of PHP support that people who run Linux and Windows enjoy.
If you wanted to use earlier versions of PHP on Mac OS X, you either needed to run the pre-compiled module provided by Apple or download a special, hand-built version made by helpful users. Apple only supplies PHP 4.0.6, so it's extremely out of date. Plus, it only supports a minimal set of extensions. Despite the heroism of some, Marc Liyanage in particular, these user-created versions of PHP run only with Apple's version of Apache, and you can't modify them to support new extensions.
But now, thanks to the hard work of many contributors, version 4.3 allows you to customize PHP with your favorite configuration. And, after reading this article, you'll know how to download, configure, and install your very own PHP on Mac OS X.
Note: These instructions have been tested only under Mac OS X 10.2.3. However, it is likely that these steps will work under any Mac OS X 10.2.x version. If you have had success or difficulty with earlier versions, please share your knowledge in the discussion at the end of this article.
Also, before proceeding, you need to install the files on the Developer's Tools CD, which supplements the main Mac OS X distribution. I suggest installing the complete set of recommended files because it's simple and easy. That's what I did. If that CD isn't handy, you can download the files from Apple's Developer Web site.
The first step is to retrieve 4.3.0 from the PHP Web site. To select the closest mirror to you to download with your Web browser, go to www.php.net/downloads.php. Or, to retrieve the file from the main PHP Web site directly from the command line, use
[benlev:~] adam% curl -O http://www.php.net/distributions/php-4.3.0.tar.gz
Now you need to unpack the files. If you used your Web browser, it's possible that StuffIt Expander did this automatically. Otherwise, use
[benlev:~] adam% tar xvfz php-4.3.0.tar.gz
This creates a directory named php-4.3.0. Using Terminal, change into that folder. (If you're unfamiliar with the command line, read Chris Stone's tutorial.)
[benlev:~] adam% cd php-4.3.0
Here's the point in the process where you configure PHP. By default, PHP includes, in addition to its core functions, the session tracking module, basic XML support, and the MySQL extension. However, PHP supports a number of other extensions, including: the Oracle, MSSQL, and PostgreSQL databases; the IMAP, LDAP, and SNMP protocols; and JPEG, PNG, and PDF file formats.
To configure PHP, run the configure program and pass its arguments. Each argument begins with two hyphens followed by a word. To see a list of all possible options, use
[benlev:~/php-4.3.0] adam% ./configure --help | less
For instance, to link in support for reading and writing XML using the DOM, using the FTP module, and communicating with LDAP servers, use the command:
[benlev:~/php-4.3.0] adam% ./configure --with-dom --enable-ftp --with-ldap
If a command argument begins with "with," it means PHP does not directly support a feature natively. Instead, it interfaces with an external program and uses that program's library of functions to perform those tasks. Therefore, in the example above, you need to install both the
libxml extension and a LDAP library, if you want to use these features with PHP. The FTP extension, in contrast, is built-in to PHP.
Luckily, Apple bundles many of these programs with Mac OS X. Unfortunately, in some (or even many) cases, these programs are obsolete. Since PHP 4.3 uses many of a program's latest features, it often requires the latest version. If you need to upgrade, try using fink or another package manager. In some cases, you may even need to install programs by hand.
PHP tries to locate libraries no matter where they're stored on your hard drive. But, if your package manager places its files in a special location, tell PHP where to find them by placing an equal sign and the path after the command name:
This lets PHP find the GD graphics library in the
/sw directory, which is where fink places files.
In addition to configuring PHP to support additional extensions, you also need to tell PHP where to install its files. Apple stores files in a different set of directories than PHP's preferred location. However, you can instruct PHP to override its default settings to conform with Apple's.
To do this, pass in these four settings:
--prefix=/usr --sysconfdir=/etc --localstatedir=/var --mandir=/usr/share/man
Once you set up PHP to install files correctly, add your Web server into the mix. If you don't specifically tell PHP your Web server, PHP builds a generic CGI version. But, if you're running Apache, for instance, PHP can create an Apache module. Apache 1.x support is added by passing in
If you're running Apple's bundled Apache, PHP automatically locates your server and properly configures itself accordingly. (This will probably work even if you've reinstalled Apache.)
Other Web servers have different commands. The experimental PHP Apache 2.x filer is built with
--apxs2. For others, run the help command, as mentioned earlier. It shows a list of Web server options.
One final configuration note. PHP does not install
zlib automatically, and without
zlib, MySQL and other programs won't run. So, explicitly add this extension to your configuration line:
Putting all this together gives us these parameters for a basic Apache and PHP installation:
[benlev:~/php-4.3.0] adam% ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --mandir=/usr/share/man --with-apxs --with-zlib
Hit return and configure will run for a while, printing out a series of scrolling lines, before finally ending with this message:
+--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this | | distribution in the file LICENSE. By continuing this installation | | process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort | | the installation process at this point. | +--------------------------------------------------------------------+ | *** NOTE *** | | The default for register_globals is now OFF! | | | | If your application relies on register_globals being ON, you | | should explicitly set it to on in your php.ini file. | | Note that you are strongly encouraged to read | | http://www.php.net/manual/en/security.registerglobals.php | | about the implications of having register_globals set to on, and | | avoid using it if possible. | +--------------------------------------------------------------------+
Thank you for using PHP.
As you see, PHP now disables the
register_globals configuration directive. This change is effective as of PHP 4.2, but if you're upgrading directly from Apple's installed version (4.0.6), this is the first time the change affects you. In brief, you can no longer access a HTML form variable called "name" as
$name. Instead, you need to check the value of
$_POST['name']. See the online PHP manual for additional information.
If you don't see this message, PHP had a problem during configuration. Look at the error message and fix what's broken. If you cannot solve the problem by yourself, check the Installation PHP mailing list for help.
Now that the laborious configuration process is complete, compile PHP by running
[benlev:~/php-4.3.0] adam% make
This will take a while. Don't be afraid if you see warnings. Warnings are okay; errors are not. There's no fancy announcement when this finishes, so unless you see an obvious error message, everything went OK when the printing stops.
Finally, you want to install PHP. This overwrites any pre-existing copies of PHP on your system. So, if you want to keep them (for testing purposes or nostalgia), now is the time to back them up. If you've chosen to use the directories suggested above, you need administrator privileges to install PHP, so run this command through sudo:
[benlev:~/php-4.3.0] adam% sudo make install
The installer places the Web server module in the correct place, installs the PHP command line version, and also places PEAR onto your system. (A few helper files are also installed.)
If you run Apache and PHP, restart your Web server to use your new copy of PHP:
[benlev:~/php-4.3.0] adam% sudo apachectl graceful
You can verify everything worked OK by viewing a page containing the output from
phpinfo(). Check out the version number, build date, and configuration options to make sure.
However, if you're trying out PHP and Apache for the first time, see Kevin Hemenway's article on Apache and PHP to learn how to configure Apache to support PHP.
The command line interface to PHP (aka PHP CLI) lets you execute PHP scripts without the overhead of requesting them using a Web browser. Recently released versions of PHP had unofficial support for the CLI, but it has gone final with 4.3.0. If you want to use PHP for system maintenance tasks or with PHP-GTK, the CLI is for you. (Note: If you've chosen to install PHP as a CGI instead of a Web server module, then you need to explicitly install the CLI using
make install-cli. This isn't a problem for people installing PHP as an Apache module, as described earlier in this article.)
Congratulations. You now have a working copy of PHP 4.3 on your Macintosh. Enjoy.
O'Reilly & Associates recently released (November 2002) PHP Cookbook .
Beta Sample Chapter 8, Web Basics, is available free online.
You can also look at the Full Description of the book.
For more information, or to order the book, click here.
Adam Trachtenberg is the manager of technical evangelism for eBay and is the author of two O'Reilly books, "Upgrading to PHP 5" and "PHP Cookbook." In February he will be speaking at Web Services Edge 2005 on "Developing E-Commerce Applications with Web Services" and at the O'Reilly booth at LinuxWorld on "Writing eBay Web Services Applications with PHP 5."
Return to the PHP DevCenter.
Copyright © 2009 O'Reilly Media, Inc.