
Compile PHP, Apache, MySQL For OS X
I just emailed Joe the steps I’ve used to get PHP, MySQL, Memcached and Apache working on my OS X machine. He’s the 3rd person in 2 days who’s ask me about how i setup PHP on my Mac so I figured other people would be interested too. So I elaborated upon the steps I sent to Joe in order to post em here for public consumption… hopefully this’ll help a few folks out.
There’s a bunch of solutions to get this stuff set up with a quick double click or a few commands in your terminal but I like things to be “my way”. So, if you’re interested read on.
The Easier Stuff
The first thing is to install MacPorts. This makes things quite a bit easier. Also, Mysql comes pre-packaged & I don’t bother compiling it myself. So grab that over here & install that too.
Recompiling Apache
I’ve found it necessary to recompile Apache as 32 bit only. Some PHP extensions didn’t like having a 64bit Apache around. This has been my experience so your mileage may vary. Anywho download Apache and unzip it somewhere. Personally I keep src in ~/Dev/open-src.
Shut down your stock install of Apache in System Preferences -> Sharing -> Web. Next, fire up your terminal and cd into the unzipped source folder. When you’re in the terminal run the following:
$ CFLAGS="-arch i386" $ ./configure --enable-layout=Darwin --enable-mods-shared=all $ make $ sudo make install
This should only take a couple minutes & has always gone smoothly for me. Now let’s restart Apache and see if it works:
$ sudo apachectl restart
Hopefully you don’t have any errors show up:
- Cruise by
http://localhostto make sure stuff shows up - run
sudo apachectl stopand make sure nothing shows up athttp://localhost(server is “off”) - run
sudo apachectl startand make surehttp://localhostis back up
If you get an error about apr or apr-util:
- take
/opt/localout of your PATH (export PATH=<whatever it is now minus /opt/local) cdback to the Apache sourcemake cleanmakesudo make install
Compiling PHP
Download the PHP source over at http://www.php.net/downloads.php, toss it next to the Apache source, unzip the tar (I guess “untar” is more appropriate) and finally cd into the source folder.
Now open another terminal window cuz you’re going to need to install some stuff via MacPorts as well. What you need from MacPorts depends on what you wanna build into PHP. Usually I enable stuff like GD, MySQLi, freetype, pdo etc. This is my config. and you may want more stuff & I’ll try n help w/ that in a bit.
Here’s all the shit I have installed via Macports:
arin@Halloween.local:~ > port installed The following ports are currently installed: apr @1.3.3_0 (active) apr-util @1.3.4_0 (active) apr-util @1.3.4_1 autoconf @2.62_0 (active) automake @1.10.1_0 (active) bzip2 @1.0.5_1 (active) cclient @2007_0 (active) cyrus-sasl2 @2.1.21_0+kerberos (active) db46 @4.6.21_1 (active) expat @2.0.1_0 (active) fontconfig @2.6.0_0+macosx (active) freetype @2.3.7_1 (active) gettext @0.17_3 (active) gperf @3.0.3_0 (active) help2man @1.36.4_1 (active) ImageMagick @6.4.7-2_0+q16 (active) jpeg @6b_3 (active) libevent @1.4.8_0 (active) libiconv @1.12_0 (active) libpng @1.2.32_0 (active) libtool @1.5.26_0 (active) libxml2 @2.7.2_1+darwin_9 (active) m4 @1.4.11_0 (active) memcached @1.2.6_0 (active) ncurses @5.6_0 (active) ncursesw @5.6_1 (active) neon @0.28.3_0 (active) openssl @0.9.8i_0 (active) p5-locale-gettext @1.05_0 (active) perl5.8 @5.8.8_3+darwin_9 (active) pkgconfig @0.23_1 (active) readline @5.2.012_1 (active) serf @0.2.0_0 (active) sqlite3 @3.6.4_0 (active) subversion @1.5.3_0 (active) tiff @3.8.2_2+darwin_9+macosx (active) zlib @1.2.3_1 (active)
You probably don’t need some of this at all, ever, but I listed them all just in case. I’d recommend you start with:
- bzip2
- expat
- fontconfig
- freetype
- gettext
- gperf
- ImageMagick
- jpeg
- libevent
- libiconv
- libpng
- libtool
- libxml2
- memcached
- openssl
- sqlite3
- tiff
- zlib
So for each of those:
$ sudo /opt/local/bin/port install <INSERT_PORT_NAME_HERE>
After all that’s done it’s time to compile PHP itself. I put PHP in /usr/local/php<version>. So for PHP 5.2.8 I install it to /usr/local/php5.2.8. You’ll see this is a few of the .configure options below. You’ll also see that I enable an ini directory and also designate that same dir as the location for the main php.ini. This is how I like it… adjust as you please. So cd to the un-tar‘ed PHP source folder and run the following commands:
$ ./configure \ --with-apxs2=/usr/sbin/apxs \ --prefix=/usr/local/php5.2.8 \ --with-config-file-scan-dir=/usr/local/php5.2.8/php.d \ --with-config-file-path=/usr/local/php5.2.8/php.d \ --disable-posix \ --enable-cli \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --with-jpeg-dir=/opt/local/include \ --with-png-dir=/opt/local/include \ --enable-gd-native-ttf \ --with-freetype-dir=/usr/local/php5.2.8 \ --with-zlib \ --enable-pdo \ --with-pdo-sqlite \ --with-sqlite=shared \ --enable-sqlite-utf8 \ --with-pdo-mysql=shared,/usr/local/mysql \ --with-xsl \ --enable-soap \ --enable-mbstring \ --with-zlib-dir=/usr \ --with-ldap \ --with-xmlrpc \ --with-iconv-dir=/usr \ --with-libxml-dir=shared,/usr/local/php5.2.8 \ --with-curl \ --with-gd \ --enable-gd-native-ttf \ --with-jpeg-dir=/usr/local/lib \ --with-png-dir=/usr/X11R6 \ --with-freetype-dir=/usr/X11R6 \ --with-xpm-dir=/usr/X11R6 \ --enable-sockets \ --with-pear=/usr/local/php5.2.8/ $ make $ sudo make install
…And hopefully that works out for you
Tourbleshooting… No, It’s Not Science
OK – so if you run into problems during ./configure you’re going to have to carefully look at the message and see what’s missing. For example: if it’s bitching about sqlite then make sure you have installed it via MacPorts using the command /opt/local/bin/port installed. If you don’t see it there, install it. Then run .configure but tack on --with-sqlite=/opt/local/include. Again, if that doesn’t work try the same thing with --with-sqlite=shared… if that doesn’t work try it with --with-sqlite. It can get pretty annoying. The jist is that the compiler needs to know how to find the header files. MacPorts makes this stuff pretty easy cuz you wont have to compile xyz yourself… it just does it & tacking on xxxxx=/opt/local/include usually does the trick. Sometimes you might have to resort to copying the .h file from /opt/local/include to /usr/include. I’ve had it go smooth and I’ve had it be a bitch.
You can follow the above “tip” if you want to add stuff as well. Usually just give .configure the switch, watch it break, see what it wants, install it via MacPorts, add the switch back with xxx=/opt/local/include and move on to the next one. Like I said, it’s not science but you’ll eventually get it working.
PECL Extensions
These always go well for me. Here’s what I’ve got installed:
arin@Halloween.local:/usr/local > which php /usr/local/php5.2.6/bin//php arin@Halloween.local:/usr/local > php -m [PHP Modules] apc ctype curl date dom filter gd gettext hash iconv imagick imap json ldap libxml mbstring memcache mysql mysqli pcre PDO pdo_mysql pdo_sqlite Reflection session SimpleXML soap sockets SPL standard tokenizer xdebug xml xmlreader xmlrpc xmlwriter xsl Zend Debugger zlib [Zend Modules] Xdebug
Some of these are as a result of the PHP compilation but a bunch of them, especially APC, memcache & imagick, were installed via pecl.
You should have pecl installed in your PHP’s bin directory. In the example above the line --prefix=/usr/local/php5.2.8 means that that’s where this install of PHP is. So in there you’ll see a bin folder in there with php, pear & pecl. So, feel free to add /usr/local/php5.2.8 to your $PATH. Anyways… go nuts and install all the pecl extensions you want. If for some reason you don’t have pecl go to http://pear.php.net/go-pear for info on how to install pecl and pear.
Anywho… once that’s settled just go on and type in:
$ sudo pecl install memcache $ sudo pecl install apc $ sudo pecl install imagick
Memcached
Just install this via MacPorts… this one’s easy. Woot
You’re Done
Now that I typed all this shit out it does seem like a bit much but it’s really not that bad. Getting the flow of installing a port then modifying .configure was my “ah ha” moment. Once you “get” that you’ll eventually breeze through. Just make sure to keep notes. I’ve kept notes the first time I went thru this and it’s helped a ton. The first compile I did took me hours of fucking w/ this and tring that till I had my “ah ha” (aka “duh”) moment. I’ve gone thru this process 3 times since and every situation is different, but the notes I compiled the first time w/ my .configure options etc greatly reduced the time it took to do it again. It’s kind of like snowboarding… at about your third trip to the slopes you’ll be able to get down a run w/o falling
Good luck.



1 comment
FYI: i just ran thru the PHP compile instructions above on a my work machine and it compiled w/o any problems at all.