MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


PHP on Mac OS X

by Jay Greenspan and Apple Developer Connection
07/20/2001

The BSD underpinnings of Mac OS X allow PHP to run as an Apache module in its native Unix environment. Using Mac OS X, programmers will see the same stability and extensibility from PHP they have seen in their production servers that run BSD, Linux, Solaris, and other Unix variants.

Like all Unix variants, Mac OS X has its quirks. It's also a young OS, and not all open-source or Unix software projects have had time to test their programs on the Mac and make the necessary alterations. Most major packages that work with PHP can be installed with only minimal difficulty on Mac OS X. There's little doubt that just about everything will eventually work on Mac OS X. Most the changes will be minor, and, as many have pointed out, by the end of this year Mac OS X will be the most widely distributed Unix system on the planet.

Default settings

After the initial installation of Mac OS X, a cursory view of the file system will reveal that some PHP components are pre-installed. Apache is initially configured to allow for DSO modules. These modules provide specific functionality and are loaded into Apache as the Web server is started. A PHP 4 module (libphp4.so) is installed by default in /usr/libexec/httpd, along with a series of other DSO modules. However, this module does not function properly and you will need to re-install PHP in order to get it working.

Note: Mac OS X does not yet support dynamic loading of modules (using the dl() function).

Comment on this articleBringing the power of PHP to the Mac OS X platform has some tremendous implications. What are the possibilities as you see them?
Post your comments

Configuration tips

The following listings show some suggested flags and configuration tweaks that may help you in the process of configuring and installing PHP on Mac OS X. Additionally, there is a rundown of modules that have been successfully built on Mac OS X. The list is not exhaustive; it represents some of the more popular modules that are used with PHP. If you have successfully compiled an additional module, please let us know.

This article focuses on PHP 4.0.6. If you have an earlier version, you should go to php.net and get the latest source.

Suggested flags

The Darwin file system is arranged somewhat differently than some other Unix variants. Running the configure command with the following flags will help keep your system properly organized.

--prefix=/usr \
--sysconfdir=/etc \ 
--localstatedir=/var \ 
--mandir=/usr/share/man \

To have PHP automatically installed as a DSO module that will be available as soon as you restart Apache, use

--with-apxs \

In PHP 4.06, zlib, which is automatically detected on many other systems, must be explicitly included in the configure stage with the --with-zlib flag. If this is not included in your configure command, certain libraries, including MySQL client libraries, will not function properly and Apache will not start. So include:

--with-zlib \

Also, in PHP 4.0.6, PEAR does not install properly, so include the flag:

--disable-pear \

PHP modules

As mentioned earlier, most modules should work on Mac OS X with a few small changes. If you find that a module you wish to install gives an error stating that the configure script cannot recognize the platform, you should update the config.guess and config.sub files that came with the package. Recent versions of these files can be found in /usr/libexec.

MySQL

There have been some reported problems with MySQL version 3.23.39, but these are easily overcome. If you have upgraded Mac OS X to version 10.0.4, it may be necessary to run the make processes as root. Normally in Mac OS X, one runs processes as root by using the sudo command. However, in the case of MySQL 3.23.39, it may be necessary to enable the root user (by using sudo su) and run the make process while logged in as root.

Before you enable root, make sure that MySQL cannot be installed by a non-root user. If you are having trouble, you may opt to use a pre-compiled version of MySQL. This package has a nice installer that will get you up and running with MySQL very quickly.

If you need transaction-safe tables with MySQL, you can configure MySQL to use InnoDB tables. BerkeleyDB tables do not seem to function on Mac OS X at this point.

To use the most recent client libraries with PHP, you should include the path to your PHP installation. Also, make sure your installation of PHP will use zlib.

--with-mysql=[DIR] \
--with-zlib

PostgreSQL

PostgreSQL version 7.1.2 also compiles with little difficulty on Mac OS X. The only bug occurs during the make install process. If make install fails, simply run sudo make install a second time. This will install the files properly.

Before compiling PostgreSQL support into PHP, I had to run the following command:

sudo ranlib /usr/local/libpg.a

This should enable you to include PostgreSQL in your configure script.

--with-pgsql=[DIR]

XML

The --with-xml flag works without any additional configuration.

WDDX

WDDX support works if the --enable-wddx flag is included. XML support must be included for WDDX to function.

GD

GD can be a bit tricky to configure the first time through. A few non-intuitive modifications or flags are needed to get all of the packages involved in GD to compile together. However, Scott Anguish at Stepwise.com has supplied a nice set of instructions that ease the process. Copy his commands into your terminal window, then compile PHP using the following flags:

--with-gd=[DIR]
--with-jpeg-dir=[DIR]
--with-png-dir=[DIR]

PDF

To install PDF support, it is necessary to install the TIFF libraries. Before you can install the TIFF libraries, you will need to replace the config.guess and config.sub files that come with this package. You can find more recent copies of these files that work with OS X in /usr/libexec. You also need the JPEG libraries. Instructions on installing those are included in Stepwise's GD instructions.

Version 3.5.6 of the TIFF library has a nice interactive install process that prompts you as to whether the directories it's found to install in are correct. Currently, the configure process works, but dies during the make process, giving the following error:

/usr/bin/install: /Library/Perl/libpdflib_pl.dylib: No such file or directory
make[1]: *** [install] Error 1

To work around this error, you can copy the files in the pdflib-4.0.1/bind/perl/.libs/libpdflib_pl.dylib directory to /System/Library/Perl. Then create a symbolic link to the directory the make process is looking for:

sudo ln -s /System/Library/Perl/Library

At this point, the make process will work, and you can finish the install. You can compile PDF support into PHP using the following flag:

--with-pdflib=[dir]

cURL

The cURL client libraries offer a way to conduct transactions of https, among other things. The current source compiles and installs into PHP without issue. You can compile cURL then include it in PHP with the following flag:

--with-curl=[dir]

Conclusion

The PHP extensions discussed here are all that most PHP developers will need. However, if you need additional support compiled into PHP, chances are that the module you need will work. If it doesn't work immediately, you can always hit the mailing lists. Those who maintain the code will most likely be happy to help you get it working on the Mac platform.

Editor's Note: This article is the first in a series of cooperative tutorials with the Apple Developer Connection. If you have comments, additions, tips, or clarifications, I encourage you to use our Talk Back feature so that your ideas can be shared with O'Reilly Network readers.

Jay Greenspan is a contributing author for Apple Developer Connection.


Return to the Mac DevCenter.

Copyright © 2009 O'Reilly Media, Inc.