Update: The below installation still works on Snow Leopard with PHP 5.3. You can use the latest and greatest of each library/project below and it will still work. However, there is one gotcha. There is a bug somewhere (PHP blames Apple and vice versa) when compiling PHP. To get around this, run the following on the command line before the ./configure string:
# export EXTRA_LIBS=-lresolv;
I will update this with a new tutorial once I compile everything with Clang.
Warning: This installation was not done on a virgin machine. Everything has been tested and works on my heavily hacked system.
Ok. We’re going to compile Apache with PHP on OSX 10.5.6. To make things interesting, we’re going to do a 64-bit install. If you don’t know if your machine is 64-bit or not, follow this rule: Core Duo == 32 bit, Core 2 Duo == 64 bit. I haven’t found a way to reliably display the chip architecture from the OS X command line, but on most GNU/Linux variants, you can use lshw. On the mac, you can try `uname -p`, `arch`, `system_profiler SPHardwareDataType` but all of this will make you think you are running 32 bit. As the man page for `arch` states:
The most common use is to select the 32-bit architecture on a 64-bit processor, even if a 64-bit architecture is available.
So everything is defaulted to 32-bit. What we need to do is set some environment variables so gcc knows the architecture we want to compile against. Another thing, I’m going to do all of this as root (back off unix weenies – I know what sudo is and use it regularly, I’m not here to argue about this). You can do this with the following (the # represents the prompt):
# MACOSX_DEPLOYMENT_TARGET=10.5 # CFLAGS="-arch x86_64 -g -Os -pipe -no-cpp-precomp" # CCFLAGS="-arch x86_64 -g -Os -pipe" # CXXFLAGS="-arch x86_64 -g -Os -pipe" # LDFLAGS="-arch x86_64 -bind_at_load" # export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
I’m also going to ratchet up the complexity a bit because I need a GD library, IMAP, iconv, and OpenSSL. So here goes:
1) Grab All The Tarballs
Here are the links to the tarballs needed. Most of these you can grab with `curl -O` or `curl -O -L` to follow a redirect:
- Apache – http://mirror.olnevhost.net/pub/apache/httpd/httpd-2.2.11.tar.gz
- PHP – http://us3.php.net/get/php-5.2.8.tar.gz/from/this/mirror
- IMAP – ftp://ftp.cac.washington.edu/mail/imap.tar.gz
- libiconv – http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
- OpenSSL – http://www.openssl.org/source/openssl-0.9.8j.tar.gz
- FreeType – http://savannah.inetbridge.net/freetype/freetype-2.3.8.tar.gz
- libjpeg – http://freshmeat.net/redir/libjpeg/5665/url_tgz/jpegsrc.v6b.tar.gz
- libpng (with config) – ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng-1.2.34.tar.gz
- t1lib – ftp://sunsite.unc.edu/pub/Linux/libs/graphics/t1lib-5.1.2.tar.gz
I’m not going full bore with the GD Library here, I just want to create some basic graphs, manipulate images, etc. so I’m not getting XBM or WBMP. I’m going to download all this stuff into /usr/local/src/.
2) Compile The GD Libraries
First we’ll start with the GD libraries as these are pretty straight forward and shouldn’t give you any problems. When on the `make` step of each of these libraries, you’ll see the familiar “-arch x86_64″, this means we’re doing it right.
FreeType
# tar -xzvf freetype-2.3.8.tar.gz # cd freetype-2.3.8 # ./configure # make # make install
libjpeg
# tar -xzvf jpegsrc.v6b.tar.gz # cd jpeg-6b # ./configure # make # make install
libpng
# tar -xzvf libpng-1.2.34.tar.gz # libpng-1.2.34 # ./configure # make # make install
t1lib
You will need to modify the “configure” file after you untar the file. So open “configure” and modify line 8656 to read:
archive_cmds='$CC -arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -dynamiclib $allow_undefined_flag -o $lib $libobjs $deplibs $compiler_flags -install_name $rpath/$soname $verstring'
The architecture types must be explicitly stated for configure to work properly. There is an alternate method of modifying the resulting Makefile, but I won’t go into that. Commands are:
# tar -xzvf t1lib-5.1.2.tar.gz
# cd t1lib-5.1.2
# vi configure (make changes above)
# ./configure
# make without_doc
# make install
Some of these installs might give you lip about certain directories not being found. Make sure you have the following directories on your system:
/usr/local/include /usr/local/bin /usr/local/lib /usr/local/man/man1
3) Compile OpenSSL
Since many components rely on this. We want to get it compiled and in place fairly early.
# tar -xzvf openssl-0.9.8j.tar.gz # cd openssl-0.9.8j # ./config --prefix=/usr/local/ssl # make # make install
4) Compile IMAP
This is pretty easy, but some extra steps need to be taken because make install doesn’t seem to exist:
# tar -xzvf imap.tar.gz # cd imap-2007e # make oxp # mkdir /usr/local/imap # cp -pr c-client /usr/local/imap/
5) Compile libicon
A very useful library that doesn’t seem to get much love. This will convert between character sets (UTF-8, etc.). Maybe dealing with Japanese characters for so long gave me this appreciation.
# tar -xzvf libiconv-1.12.tar.gz # cd libiconv-1.12 # ./configure # make # make install
6) Compile Apache
This assumes you already have MySQL on your machine. The 64-bit package installer at http://dev.mysql.com/downloads/ should be all you need to install this. I would stick with 5.0.x even though 5.1 is GA (Monty of MySQL fame has some choice words about this). Even though I love compiling my own apps, MySQL does such a good job with their installer, I’ve left it to them for the past couple years.
# tar -xzvf httpd-2.2.11.tar.gz # cd httpd-2.2.11 # './configure' '--prefix=/usr/local/apache2.2.11' '--with-included-apr' '--enable-module=most' '--enable-shared=max' '--enable-headers' '--enable-rewrite' # make # make install
A couple things to note:
- You normally don’t have to explicitly state which apr to use, your system will figure this out. I had a version installed by darwin ports for subversion and the compiler wanted to use that one and would choke (this is Versions‘ fault).
- You don’t need the enable-headers and enable-rewrite. I like to do funky things like mod_rewrite for pretty URLs and mod_proxy for Tomcat. Choice is yours.
- From httpd-2.2.9, the configure options got all out of whack with slight syntax modifications to some important flags. To normalize this, you need to run `./buildconf` in the src directory before configuring. Seems like things are back to normal for 2.2.11.
- In my config string, I spell out which version of the software I’m using – apache2.2.11 – then later symlink /usr/local/apache2 to /usr/local/apache2.2.11. This is useful for running multiple versions.
7) Compile PHP
The final step. Here is where you’ll be able to see if you’ve done everything above correctly.
# tar -xzvf php-5.2.8.tar.gz # cd php-5.2.8 # './configure' '--prefix=/usr/local/php' '--with-apxs2=/usr/local/apache2.2.11/bin/apxs' '--with-zlib=/usr' '--with-mysql=/usr/local/mysql/' '--with-mysqli=/usr/local/mysql/bin/mysql_config' '--with-gd' '--with-png-dir=/usr/local/php' '--with-jpeg-dir=/usr/local/php' '--with-freetype-dir=/usr/local/php' '--with-t1lib=/usr/local/php' '--with-xmlrpc' '--with-pear' '--enable-mbstring' '--enable-cli' '--with-curl=/usr/local/' '--enable-soap' '--with-openssl=/usr/local/ssl/' # make # make install
Some things to note:
- You may not need all these compile options. Pick and choose what you feel you need.
- I compiled my own version of curl, but you can leave this out of the config string if you are ok with the default OSX curl. I’m a huge fan and power user of curl. Maybe someday I’ll write about what that means.
Other Configuration Stuff:
- Change user/group to www:www in conf/httpd.conf
- I’m not going to get into it too much, but make sure you compare your old httpd.conf file (/etc/apache2/httpd.conf) with the new one. There are several Apple specific directives in there to keep your webserver safe.
- Make sure to add support for mod_php:
<IfModule mod_php5.c>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
</IfModule>
- If you want this new version of Apache to start automatically at startup, make sure it is enabled in sharing, then do this:
# cd /usr/sbin/
# mv apachectl apachectl.bak
# ln -s /usr/local/apache2/bin/apachectl apachectl
Now fire up apache and load a page with <? phpinfo(); ?> to see that everything has installed correctly.
I’m more than happy to answer any questions you have about this process, so fire away!
Warning: This installation was not done on a virgin machine. Everything has been tested and works on my heavily hacked system.
Popularity: unranked [?]
Thank you for this wonderful tutorial.
However, I get an error on configuring apache:
Configure: error: invalid variable name: -prefix
I’m sorry to ask, but can you help me sort that out?
Sure, no problem. All the dashes in that statement are actually double dashes and WordPress felt it was convenient to change the display for me.
So replace all those long dashes with double dashes, as in –prefix=/usr/local/php
I’ll try to fix this within the tutorial.
I just patched WordPress to prevent this from happening…
Thank you! I took a closer look at it yesterday, and sorted that out. Also the ‘ and ” are changed by WordPress. Only one problem remains. The PHP configure stops with a mySQL error. The config.log says that libmysqlclient.dylib is not of required architecture. I have the latest 64bit mySQL installed.
And it is in the correct spot according to your config string? Also, a good way to double check this is with the ‘file’ command:
file /usr/local/apache2/bin/httpd
file /usr/local/php/bin/php
file /usr/local/mysql/bin/mysql
You should get a result with ‘Mach-O 64-bit executable x86_64′ in it.
Why do you choose compiler flags to optimise for size (-Os) and not for speed (-fast)?
Got it now. Had to recompile everything, and make absolutely shure everything got done with 64bit gcc. I also had to add
“# make install-lib” and “# make install-headers” to libjpeg. If I didn’t PHP compiler threw an error.
What would be realy nice now, is to take this exact setup and recompile os x built in apache and php, so that we can use apples server tools on OS X Leopard Server, or WebSharing preference pane on regular OS X Leopard. Do you have any ideas? I tried one tutorial on it, but it didn’t work for me.
@rune
Thanks for the update. When I get the chance to set this up on a virgin machine, I’ll look into the issue you describe. To replace the system installs, you’re going to have to muck around with LaunchAgents and LaunchDaemons in /System/Library/. The safer way is probably not to touch these files, but symlink the scripts they reference. You can read more at http://developer.apple.com/MacOsX/launchd.html.
@ceriel
As far as I know, the -fast option only works for the G5 architecture. You are right though, I could optimize this further using -O3. Doing the optimized Shiretoko builds taught me a lot that I will update this article with. Thanks for pointing this out.
-fast is not only about PowerPC/G5 architecture:
The gcc 4.0.1 manual, chapter optimize options [1]
indeed first talks about G5 optimazations during first (~four) paragraphs.
In the last paragraph intel optimizations using the -fast flag are mentioned, and this is said:
On Intel target, -fast currently enables the following optimization flags:
-O3
-fomit-frame-pointer
-fstrict-aliasing
-momit-leaf-frame-pointer
-fno-tree-pre
-falign-loops
All choices of flags enabled by -fast are subject to change without notice.
The same info is also mentioned in the Apple GCC man pages [2]:
On Intel target, -fast currently enables the following optimization flags:
-O3 -fomit-frame-pointer -fstrict-aliasing -momit-leaf-frame-pointer -fno-tree-pre -falign-loops
[1] http://developer.apple.com/documentation/developertools/gcc-4.0.1/gcc/Optimize-Options.html#index-fast-475
[2] http://developer.apple.com/DOCUMENTATION/DARWIN/Reference/ManPages/man1/gcc.1.html
You are right. I’m using the 4.2.1 gcc, but the -fast option remains unchanged:
http://developer.apple.com/documentation/DeveloperTools/gcc-4.2.1/gcc/Optimize-Options.html#Optimize-Options
Xcode 3.1.2 includeds both gcc 4.0.1 and 4.2.1, but defaults to 4.0.1
Gcc 4.0.1 lacks support for Core2 processor optimizations (-march=core2)
What is your (nice) way to change to gcc version 4.2.1, or to even switch between 4.0.1 and 4.2.1 on demand?
Ceriel,
You seem to have much more experience with compilers than I (having written a book or two on the subject). Doing “which gcc” shows me that I’m using /usr/bin/gcc which is a symlink to gcc-4.0. Could one not just change that symlink to gcc-4.2?
Also, I have used “-march=nocona” as an option in gcc-4.0 in an attempt to target the core2 architecture.
What are your suggestions on switching between compilers and optimizing for certain architectures? I don’t claim to know everything and am looking to learn, more than anything else.
Also, I’m not trying to be insulting to you in any way and am actually thrilled to be having this exchange with someone with your knowledge.
Chris,
Actually I am really a novice on compilers, that banged his head too many times the last couple of months trying to ‘install’ open source software on OS X. And in these months I also was disappointed that most of the stuff Apple delivers is still 32-bit and ppc compiled. Nice for you that you have bought our latest and greatest 64-bit Intel core 2 duo processor, but we are not going to use that.
My knowledge was derived during weeks of learning-the-hard-way, making lots of mistakes, from reading on the web, trying numerous different compiler flags and benchmarking the results.
With GCC 4.0 my best results (tested with MySQL) are setting -fast flag. With GCC 4.2 combining that with -march=core2 is I think the best you can get. GCC 4.0 is only able to optimise for architectures up to nocona. -m64 is listed on many sites, but in practice it has no impact on benchmarchs, and in theory it shouldn’t because that flag is only for AMD processors.
My test were limited by GCC 4.0, when trying to change to 4.2 and 4.2-llvm mysql threw errors or didn’t, but wouldn’t execute.
[...] Compiling 64-bit Apache/PHP on Mac OS X 10.5.6 [...]
Trying this tutorial with GCC 4.0, using -fast chokes on PHP. Everything else will compile fine. The best I could do with PHP was -03.
Thank you, Chris, for posting this, it was most helpful. This is the first time I’ve been able to build Apache or PHP on OS X!
I just had one problem…
The very last instruction, on having the new version of Apache start up appears to have the incorrect path for the new Apache build. I believe the very last line should read:
# ln -s /usr/local/apache2.2.11/bin/apachectl apachectl
Again, many thanks!
mattthehat,
You are correct, but as I mentioned earlier in the tutorial, I usually symlink apace2 to the more specific version – in this case apache2.2.11. This let’s me switch between versions of apache by just changing that symlink. I picked this method up from the way Apple symlinks their frameworks. And was also forced to do it when I had to switch between different versions of Tomcat when porting Cold Fusion to OS X back in the day.
Glad to have been of help in getting this to compile for you.
I had problems with the MySQL package from mysql.com.
I’m use to installing from source, and I was having problems compiling mysql as a 64bit binary.
The included apache and php with 10.5.6 work fine for my development work, as well both are 64bit.
I found using your FLAG setting modified with the -03:
MACOSX_DEPLOYMENT_TARGET=10.5
CFLAGS=”-arch x86_64 -g -O3 -pipe -no-cpp-precomp”
CCFLAGS=”-arch x86_64 -g -O3 -pipe”
CXXFLAGS=”-arch x86_64 -g -O3 -pipe”
LDFLAGS=”-arch x86_64 -bind_at_load”
export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
as well as change gcc / cc to sym link gcc-4.2
from /usr/bin
rm gcc
rm cc
then relink:
ln -s gcc-4.2 gcc
ln -s gcc-4.2 cc
and then compiled mysql-5.1.31 with :
./configure –prefix=/usr/local/mysql \
–with-extra-charsets=complex –enable-thread-safe-client \
–enable-local-infile –enable-shared –with-plugins=innobase
then do a make & sudo make install.
since I never shutdown my MBP, I wrote a start up script to call mysqld_safe –user=mysql
And you’re off to the races.
I’m working on compiling IMAP support into PHP 5.2.6 on a 10.5.6 machine and ran across this posting. It’s been very helpful and somewhat educational (though I doubt I’ll get as deep in the particulars of compiling flags as you all have ..)
As I look over your PHP configure options, you’ve left out IMAP support, yet you’ve built IMAP.
PHP gags on the IMAP portion of the conf, complaining of a UTF-8 error (libiconv is next on my list to add to the machine support.)
Was this intentional to leave IMAP out of your PHP configure options but using it for something else?
Whoa!
Maybe it’s a process thing, however, I’ve stepped through this (and now I have my very own heavily hacked OS X machine
and am running into a problem with libconv and iconv.
When compiling PHP with flag in the conf set –with-apxs2, the compiler throws and error:
ld: warning in /usr/sbin/httpd, file is not of required architecture
using file /usr/sbin/httpd delivers: Mach-O 64-bit executable x86_64
all good, however, iconv only compiles as 32-bit and I suspect this is where the problem lies.
I’ve been toying with this non-stop for the past several hours (going on 24) and would greatly appreciate your assistance.
Not intentional. I’m looking into this right now and will report what I find.
I was able to compile in iconv using ‘–with-iconv-dir=/usr/local/’ without a problem.
I’m currently parsing ESRI Shape files into millions of coordinates, plus parsing data for another project. The one build I did took forever and I have to take off. I’ll check out IMAP tomorrow AM.
Good Luck!
Well, the simple fix was to leave iconv out completely in the PHP conf with –without-iconv …
Hey, thanks for this tuto, really clear !
However, I get an warning when tryin’ to ./configure libpng :
checking if libraries can be versioned… no
configure: WARNING: *** You have not enabled versioned symbols.
and an error right after I start make :
gcc-4.0: -E, -S, -save-temps and -M options are not allowed with multiple -arch flags
make[1]: *** [libpng12_la-png.lo] Error 1
make: *** [all] Error 2
I’m tryin to compile as a Universal Bianary.
Any help would be really appreciated !
To determine if a 32- or 64-bit Intel x86 Core, Core 2 or Xeon processor is lurking underneath, look for EM64T in the features list output by sysctl.
Here’s a long form:
$ sysctl -a machdep.cpu
Or directly:
$ sysctl -a machdep.cpu.extfeatures
machdep.cpu.extfeatures: XD EM64T
$
If there is no EM64T listed, then there is no 64-bit addressing.
When i compile php 5-2.9 get the error
checking for MySQL UNIX socket location… no
checking for mysql_close in -lmysqlclient… no
checking for mysql_error in -lmysqlclient… no
configure: error: mysql configure failed. Please check config.log for more information.
@visualinventor checkout http://tinyurl.com/dmvkf8
This comment was originally posted on Twitter
Following these instructions, it seems that openssl builds for i386. Wouldn’t you want to add the ‘darwin64-x86_64-cc’ flag to config?
Can’t verify right now, but I believe you are right.
[...] See the original post here: Compiling 64-bit Apache/PHP on Mac OS X 10.5.6 | Dreaming Of Beetles [...]
Compiling 64-bit Apache/PHP on Mac OS X 10.5.6: http://tr.im/lSbU (via @clatko)
This comment was originally posted on Twitter
I have problems upgrading to any apache version with any openssl version over 0.9.7l (the default one that comes with leopard). The last lines when making and the errors are the following:
/sources/httpd-2.2.11/srclib/apr/libtool –silent –mode=link gcc -arch x86_64 -g -Os -pipe -no-cpp-precomp -L/usr/local/lib -L/usr/local/ssl/lib -arch x86_64 -bind_at_load -o ab ab.lo /sources/httpd-2.2.11/srclib/pcre/libpcre.la /sources/httpd-2.2.11/srclib/apr-util/libaprutil-1.la -lexpat -liconv /sources/httpd-2.2.11/srclib/apr/libapr-1.la -lpthread -lssl -lcrypto -lpthread
Undefined symbols:
"_BIO_set_callback_arg", referenced from:
_start_connect in ab.o
"_BIO_get_callback_arg", referenced from:
_ssl_print_cb in ab.o
"_SSL_CTX_set_info_callback", referenced from:
_main in ab.o
"_BIO_set_callback", referenced from:
_start_connect in ab.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [ab] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all-recursive] Error 1
OpenSSL (any version) were installed successfully with the following configure command:
./configure darwin-i386-cc
–prefix=/usr/local
–with-zlib-lib=/usr/lib
–with-zlib-include=/usr/include
Apache failed during the make process as I said before with the following configure command:
CC="darwin-i386-cc"
./configure –enable-layout=Darwin
–enable-mods-shared=all
–with-ssl=/usr
–with-mpm=prefork
–disable-unique-id
–enable-ssl
–enable-dav
–enable-cache
–enable-proxy
–enable-logio
–enable-deflate
–with-included-apr
–enable-cgi
–enable-cgid
–enable-suexec
PHP were installed successfully with the following configure command but using phpinfo (), I detected that PHP is not recognizing the new installed OpenSSL version (remains showing the bundle 0.9.7l version):
./configure
–prefix=/usr
–mandir=/usr/share/man
–infodir=/usr/share/info
–with-apxs2=/usr/sbin/apxs
–with-ldap=/developer/sdks/macosx10.5.sdk/usr
–with-kerberos=/usr
–enable-cli
–with-zlib-dir=/usr
–enable-exif
–enable-ftp
–enable-mbstring
–enable-mbregex
–enable-sockets
–with-iodbc=/usr
–with-curl=/usr
–with-config-file-path=/etc
–sysconfdir=/private/etc
–with-mysql-sock=/var/mysql
–with-mysqli=/usr/local/mysql/bin/mysql_config
–with-mysql=/usr/local/mysql
–with-openssl=/usr
–with-xmlrpc
–with-xsl=/usr
–without-pear
–with-libxml-dir=/usr
–with-iconv=/usr/local
–with-pdo-mysql=/usr/local/mysql/bin/mysql_config
–with-jpeg-dir=/usr/
–with-png-dir=/usr/
–with-ttf=/usr/
–with-freetype-dir=/usr/
–with-t1lib=/usr/
–enable-gd-native-ttf
–with-xpm-dir=/usr/X11R6
–with-mcrypt
–with-pgsql=/Library/PostgreSQL/8.3
–with-gd=/usr/
–enable-maintainer-zts
–with-mssql=/usr/
Thanks in advance for your help and hoping I can help anothers with my configuration commands
[...] At the moment, I’m pressed for time so I don’t want anything to go wrong. The 64-bit Apache/PHP tutorial is working great so far under Snow Leopard (just find the more recent version of the projects and [...]
When I compiled apache my modules folder is empty, is this normal?
This is normal. With these build instructions, you are compiling in the modules rather than loading them as shared objects. The only .so you should have is for php.