Installing Apache, MySQL, and PHP on Macintosh

Most peo­ple will find that down­load­ing and installing the “AMP” part of “LAMP” on a Macintosh from either the fine folks at Apache Friends or liv­ing-e will result in a great expe­ri­ence. I encour­age those who are not com­fort­able with the com­mand line to use one of them. However, if you are crazy like me, you may want to down­load the source code and install it from scratch. I do so I can get an instal­la­tion with the exact con­fig­u­ra­tion that I want. It can be a frus­trat­ing process at times but it real­ly isn’t that hard. Here is how I do it. This is a very per­son­al way of doing it. Use this as an exam­ple and not as a pure how-to.

I can hear many ask­ing, “WHY? After all, Mac OS X already has both Apache and PHP installed.” Well, for me any­way, it was because the ver­sion and con­fig­u­ra­tion of Apache and PHP were not what I need­ed. I devel­op web sites that need dif­fer­ent capa­bil­i­ties than what Apple gives. And, quite frankly, I do it because I can and I have fun doing so.

You should get the devel­op­er tools from Apple. You do have to sign-up for a devel­op­er account but you can do so for free to get the basic tools. They have sev­er­al pay-per-year accounts as well that are real nice and if you get seri­ous about devel­op­ing for the Mac, they are a must. You need to down­load the Xcode for your ver­sion of MacOS X. This will pro­vide you with all you need to actu­al­ly com­pile every­thing.

You may also need to install either Fink or MacPorts to get some libraries that will be required for Apache and/or PHP, depend­ing on your con­fig­u­ra­tion. I use Fink now although I had been an ear­ly user of MacPorts which was orig­i­nal­ly called DarwinPorts. There was a peri­od that MacPorts didn’t have what I need­ed but Fink did. I also have found Fink eas­i­er to main­tain for some strange rea­son. Be sure if you use Fink to also get FinkCommander as it will make using Fink eas­i­er.

I use the freetype, jpeg, png, and bz2 libraries from Fink. This allows me to do a bunch of the image and file manip­u­la­tions that I do in PHP.  You may not find you need them. You can also install them from source and I used to do that but since they were just libraries, I found the ease of use of Fink or MacPorts high­ly ben­e­fi­cial. I still install from source using Fink, I just don’t have to think about it.

[edit] I believe there may be a few oth­er libraries/binaries that my com­piles are using from Fink. I just noticed Apache con­fig was using pkg-con­fig, gawk, and lynx all from my Fink install (again, installed using FinkCommander). I could not ver­i­fiy what php might have also been using but I will try to estab­lish it if I can.[/edit]

After installing those libraries, the next thing to do is get MySQL (yes, the order of instal­la­tion can mat­ter). Here is one place where I just use the bina­ries MySQL pro­vides. Although it gets installed in a loca­tion I don’t like (/usr/local), more on this lat­er, it just works. I used to com­pile MySQL from source but I found that it pro­vid­ed me with noth­ing ben­e­fi­cial and the bina­ries actu­al­ly seem faster than what I com­piled. If you are using the PowerPC ver­sion, be sure to get the 32 bit ver­sion. The 64 bit ver­sion con­flicts with some­thing that you need to com­pile.

The bina­ry pack­age for Macintosh comes in a stan­dard Mac Install Package and puts every­thing where you need it. You may also want to install the autoload pack­age so that MySQL starts up every time you turn your com­put­er on — there is a nice con­trol pan­el for this as well.

I men­tioned that I don’t nec­es­sar­i­ly like /usr/local for installing MySQL. I guess I am old-school and very new school at the same time. Part of this comes from using Solaris a lot and some from some new­er Linux thought. Anyway, I pre­fer to install pack­ages into /opt. Apache likes to install every­thing into a sin­gle direc­to­ry and PHP works well that way as well. That is con­sid­ered to be a “pack­age” install and pack­ages belong in /opt.

I also have picked up the con­cept that all data for dif­fer­ent pack­ages get saved in /srv. As such, MySQL data gets saved in /srv/mysql, web­sites get saved in /srv/websites/websiteName and oth­er serv­er data would be saved in their appro­pri­ate direc­to­ries.

Why do I do this? To mir­ror my servers. On my servers I cre­ate hard dri­ve par­ti­tions just for /opt and anoth­er just for /srv. This allows me to (re)size those par­ti­tions as need­ed. On my Gentoo Linux servers, they are in LVM2/ReiserFS/RAID5 con­fig­u­ra­tions. On my Macintosh my /srv is actu­al­ly a sym­link to an exter­nal dri­ve that gets mir­rored which of course gets mount­ed as /Volumes/srv…

*rant on* Why does Apple force all dri­ves to be in /Volumes and not allow us to mount a dri­ve to a dif­fer­ent mount point?  *rant off*

Before I for­get, most every­thing I do will be done at com­mand line. So make sure you have the Terminal app open. I have mine auto­mat­i­cal­ly open when I start my Mac and is sec­ond nature for me. If you don’t use it much, I will pro­vide the exact com­mands. However, I do these com­mands as root. If you don’t have your Mac set­up with root access, don’t wor­ry, you can use sudo instead (which is the­o­ret­i­cal­ly safer way of doing it). But again, use this as an exam­ple and not nec­es­sar­i­ly the exact way you should do it.

I cre­ate (mkdir) both the /opt and /srv direc­to­ries before going fur­ther.  If you choose to install them in their default loca­tions (/usr/local) you can skip this step. I also change the MySQL con­fig file (my.cnf) to use /srv/mysql for its data direc­to­ry and cre­ate that direc­to­ry. You may have to cre­ate the my.cnf file. It is easy to do so by copy­ing one of the .cnf files in the sup­port-files direc­to­ry (inside your mysql direc­to­ry) to /etc. If this is for a test sys­tem like I have, using the my-small.cnf file works quite well. If you are set­ting up a serv­er, I would start with the my-medium.cnf and test with that before using the my-large.cnf which should real­ly only be used on a serv­er that is ded­i­cat­ed to MySQL.

Now that I have cre­at­ed the /opt  direc­to­ry, I mkdir /opt/src where I down­load the source code for Apache and PHP. Go to both Apache and PHP web­sites and down­load the lat­est sta­ble source code. I get the bzip ver­sions. As of this writ­ing it is Apache 2.2.8 and PHP 5.2.6. Untar the source code for both (tar -xvjf file­name for bzip, tar -xvzf for gzip). A trick I do, some­thing I picked up from com­pil­ing the Linux ker­nel, is to cre­ate a sym­link to the most cur­rent source code named more gener­ic. For exam­ple, ln -s httpd-2.2.8 apache2.2. This cre­ates an alias named apache2.2. I do the same for the com­piled code. This way I may have two or three ver­sions handy. My con­fig files will point to the sym­link so I don’t have to rewrite the con­fig every time I upgrade ver­sions.

You need to com­pile Apache next. But, if you want to use MySQL for authen­ti­ca­tion, you need to take one more step. Due to licens­ing issues, Apache can­not include the MySQL data­base con­nec­tor. You will need to down­load the con­nec­tor code first. Then con­fig­ure and com­pile. (all code exam­ples will be using my direc­to­ry struc­ture):

# cd /opt/src/httpd-2.2.8/srclib/apr-util/dbd/
# curl -O http://apache.webthing.com/svn/apache/apr/apr_dbd_mysql.c
# cd /opt/src/httpd-2.2.8/
# ./buildconf
# ./configure --prefix=/opt/apache2.2.8 --enable-mods-shared=all  \
--with-included-apr --with-mysql=/usr/local/mysql
# make && make install (or sudo make install)

If you are going to use the default install for Apache, don’t use the –pre­fix= option. If you are not going to use MySQL for authen­ti­ca­tion, you don’t need the –with-mysql com­mand. I enable all mod­ules but as shared so I can turn on and off dif­fer­ent mod­ules as I need them. I have found that my com­piles work best when I spec­i­fy –with-includ­ed-apr, most like­ly because I have a lot of APRs run­ning around on my Mac.

Be sure to turn off web serv­ing in the Mac Preferences (Personal Web Sharing) before start­ing up this ver­sion of Apache. To turn this Apache on, you can use the com­mand /path/to/apache/bin/apachectl start  HOWEVER, you prob­a­bly should ver­i­fy that the httpd.conf file in /path/to/apache/conf is con­fig­ured cor­rect­ly first. For my pur­pos­es, I change the DocumentRoot to “/srv/websites/localhost/html” and make cor­re­spond­ing changes through­out the conf file.  The con­fig­u­ra­tion file(s) are always the one spot which will make your instal­la­tion fail if they are not con­fig­ured prop­er­ly. The only way to see this is in the log files (  /path/to/apache/logs ). You may have to come back to the conf file after installing PHP.

Speaking of PHP, I have run into more issues get­ting it to com­pile over the years but late­ly, I can recom­pile PHP on my Mac in very lit­tle time and with­out break­ing a sweat.  Here is my con­fig­u­ra­tion (as tak­en from config.nice):

#! /bin/sh
# Created by configure
'./configure' \
'--prefix=/opt/php5.2' \
'--with-zlib' \
'--with-openssl' \
'--enable-exif' \
'--with-zlib-dir=/usr' \
'--enable-ftp' \
'--enable-mbstring' \
'--enable-sockets' \
'--with-mysql=/usr/local/mysql' \
'--with-mysqli=/usr/local/mysql/bin/mysql_config' \
'--with-pdo-mysql=/usr/local/mysql' \
'--with-pgsql=/opt/pgsql' \
'--with-pdo-pgsql=/opt/pgsql' \
'--with-apxs2=/opt/apache2.2/bin/apxs' \
'--with-gd' \
'--enable-gd-native-ttf' \
'--with-ttf' \
'--with-freetype-dir=/sw/lib/freetype2' \
'--with-curl' \
'--with-curlwrappers' \
'--with-jpeg-dir=/sw' \
'--with-png-dir=/sw' \
'--with-bz2=/sw' \

The –with-something=/sw indi­cates those libraries that I installed with Fink. Fink nice­ly installs into it own direc­to­ry so it is very easy to remove if you need to. You will also notice that I have down­loaded and com­piled the lat­est ver­sion of PostgreSQL and have com­piled PHP to use that ver­sion.

If PHP con­fig­ures cor­rect­ly, it is as easy as

# make && make install (or sudo make install)

You will need to make sure that the Apache con­fig file was mod­i­fied to load the php mod­ule. Look for

LoadModule php5_module        modules/libphp5.so”

Normally, this gets added auto­mat­i­cal­ly. BUT that won’t get your Apache to actu­al­ly serv­er php file cor­rect­ly. You need to add the fol­low­ing to the httpd.conf file.

AddType application/x-httpd-php .php

Most will want to add index.php to the DirectoryIndex direc­tive.
Some will change .html to also use appli­ca­tion/x-httpd-php

If you want to get fan­cy or use vir­tu­al hosts, there are a lot of nice ways to do this, most involve break­ing the httpd.conf file into small­er parts. For exam­ple, the AddType direc­tives for php I put into a sep­a­rate file that sits in the conf/extras direc­to­ry and then include it in the httpd.conf file. I also cre­ate a vhosts.d direc­to­ry which con­tains con­fig­u­ra­tions for each vir­tu­al host. I include those files by the direc­tive “Include conf/vhosts.d/*.conf” This is a com­mon way of doing it (e.g., Gentoo Linux).

I have one oth­er trick up my sleeve to make things easy. Once I have a work­ing ver­sion of Apache and PHP, I make sure I copy the “config.nice” file to a dif­fer­ent loca­tion and name them some­thing like apache.config.nice and php.config.nice. These files are nor­mal­ly found in the root of the source code after you have run ./configure. When you down­load a new ver­sion of the source, you can copy the “nice” file to the source direc­to­ry, mod­i­fy it slight­ly (ver­sion stuff), and run it instead of typ­ing out the whole con­fig­ure line. Basically, upgrad­ing to a new ver­sion ends up being

./config.nice && make && make install

and a quick delete of the old sym­link and cre­ate the new one. ( To answer a ques­tion, the && says basi­cal­ly on the com­mand line, if the pre­vi­ous com­mand exits suc­cess­ful­ly do the next com­mand. So if “make” works then do “make install” else exit. )

I hope you have found the way I do it infor­ma­tion­al and may inspire some to go ahead and install Apache and PHP from source. I also down­load pureFTPd and com­pile it from source. Although most on the Mac seem to pre­fer ProFTPd, I have always liked pureFTPd’s flex­i­bil­i­ty, secu­ri­ty, and ease of use. And for the rest of you, grab MAMP or XAMPP and start play­ing!

Leave a Reply