WEReveal

Installing Apache, MySQL, and PHP on Macintosh

Most people will find that downloading and installing the “AMP” part of “LAMP” on a Macintosh from either the fine folks at Apache Friends or living-e will result in a great experience. I encourage those who are not comfortable with the command line to use one of them. However, if you are crazy like me, you may want to download the source code and install it from scratch. I do so I can get an installation with the exact configuration that I want. It can be a frustrating process at times but it really isn’t that hard. Here is how I do it. This is a very personal way of doing it. Use this as an example and not as a pure how-to.

I can hear many asking, “WHY? After all, Mac OS X already has both Apache and PHP installed.” Well, for me anyway, it was because the version and configuration of Apache and PHP were not what I needed. I develop web sites that need different capabilities than what Apple gives. And, quite frankly, I do it because I can and I have fun doing so.

You should get the developer tools from Apple. You do have to sign-up for a developer account but you can do so for free to get the basic tools. They have several pay-per-year accounts as well that are real nice and if you get serious about developing for the Mac, they are a must. You need to download the Xcode for your version of MacOS X. This will provide you with all you need to actually compile everything.

You may also need to install either Fink or MacPorts to get some libraries that will be required for Apache and/or PHP, depending on your configuration. I use Fink now although I had been an early user of MacPorts which was originally called DarwinPorts. There was a period that MacPorts didn’t have what I needed but Fink did. I also have found Fink easier to maintain for some strange reason. Be sure if you use Fink to also get FinkCommander as it will make using Fink easier.

I use the freetype, jpeg, png, and bz2 libraries from Fink. This allows me to do a bunch of the image and file manipulations 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 highly beneficial. I still install from source using Fink, I just don’t have to think about it.

[edit] I believe there may be a few other libraries/binaries that my compiles are using from Fink. I just noticed Apache config was using pkg-config, gawk, and lynx all from my Fink install (again, installed using FinkCommander). I could not verifiy what php might have also been using but I will try to establish it if I can.[/edit]

After installing those libraries, the next thing to do is get MySQL (yes, the order of installation can matter). Here is one place where I just use the binaries MySQL provides. Although it gets installed in a location I don’t like (/usr/local), more on this later, it just works. I used to compile MySQL from source but I found that it provided me with nothing beneficial and the binaries actually seem faster than what I compiled. If you are using the PowerPC version, be sure to get the 32 bit version. The 64 bit version conflicts with something that you need to compile.

The binary package for Macintosh comes in a standard Mac Install Package and puts everything where you need it. You may also want to install the autoload package so that MySQL starts up every time you turn your computer on – there is a nice control panel for this as well.

I mentioned that I don’t necessarily 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 newer Linux thought. Anyway, I prefer to install packages into /opt. Apache likes to install everything into a single directory and PHP works well that way as well. That is considered to be a “package” install and packages belong in /opt.

I also have picked up the concept that all data for different packages get saved in /srv. As such, MySQL data gets saved in /srv/mysql, websites get saved in /srv/websites/websiteName and other server data would be saved in their appropriate directories.

Why do I do this? To mirror my servers. On my servers I create hard drive partitions just for /opt and another just for /srv. This allows me to (re)size those partitions as needed. On my Gentoo Linux servers, they are in LVM2/ReiserFS/RAID5 configurations. On my Macintosh my /srv is actually a symlink to an external drive that gets mirrored which of course gets mounted as /Volumes/srv…

*rant on* Why does Apple force all drives to be in /Volumes and not allow us to mount a drive to a different mount point?  *rant off*

Before I forget, most everything I do will be done at command line. So make sure you have the Terminal app open. I have mine automatically open when I start my Mac and is second nature for me. If you don’t use it much, I will provide the exact commands. However, I do these commands as root. If you don’t have your Mac setup with root access, don’t worry, you can use sudo instead (which is theoretically safer way of doing it). But again, use this as an example and not necessarily the exact way you should do it.

I create (mkdir) both the /opt and /srv directories before going further.  If you choose to install them in their default locations (/usr/local) you can skip this step. I also change the MySQL config file (my.cnf) to use /srv/mysql for its data directory and create that directory. You may have to create the my.cnf file. It is easy to do so by copying one of the .cnf files in the support-files directory (inside your mysql directory) to /etc. If this is for a test system like I have, using the my-small.cnf file works quite well. If you are setting up a server, I would start with the my-medium.cnf and test with that before using the my-large.cnf which should really only be used on a server that is dedicated to MySQL.

Now that I have created the /opt  directory, I mkdir /opt/src where I download the source code for Apache and PHP. Go to both Apache and PHP websites and download the latest stable source code. I get the bzip versions. As of this writing it is Apache 2.2.8 and PHP 5.2.6. Untar the source code for both (tar -xvjf filename for bzip, tar -xvzf for gzip). A trick I do, something I picked up from compiling the Linux kernel, is to create a symlink to the most current source code named more generic. For example, ln -s httpd-2.2.8 apache2.2. This creates an alias named apache2.2. I do the same for the compiled code. This way I may have two or three versions handy. My config files will point to the symlink so I don’t have to rewrite the config every time I upgrade versions.

You need to compile Apache next. But, if you want to use MySQL for authentication, you need to take one more step. Due to licensing issues, Apache cannot include the MySQL database connector. You will need to download the connector code first. Then configure and compile. (all code examples will be using my directory structure):

# 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 –prefix= option. If you are not going to use MySQL for authentication, you don’t need the –with-mysql command. I enable all modules but as shared so I can turn on and off different modules as I need them. I have found that my compiles work best when I specify –with-included-apr, most likely because I have a lot of APRs running around on my Mac.

Be sure to turn off web serving in the Mac Preferences (Personal Web Sharing) before starting up this version of Apache. To turn this Apache on, you can use the command /path/to/apache/bin/apachectl start  HOWEVER, you probably should verify that the httpd.conf file in /path/to/apache/conf is configured correctly first. For my purposes, I change the DocumentRoot to “/srv/websites/localhost/html” and make corresponding changes throughout the conf file.  The configuration file(s) are always the one spot which will make your installation fail if they are not configured properly. 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 getting it to compile over the years but lately, I can recompile PHP on my Mac in very little time and without breaking a sweat.  Here is my configuration (as taken 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 indicates those libraries that I installed with Fink. Fink nicely installs into it own directory so it is very easy to remove if you need to. You will also notice that I have downloaded and compiled the latest version of PostgreSQL and have compiled PHP to use that version.

If PHP configures correctly, it is as easy as

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

You will need to make sure that the Apache config file was modified to load the php module. Look for

“LoadModule php5_module        modules/libphp5.so”

Normally, this gets added automatically. BUT that won’t get your Apache to actually server php file correctly. You need to add the following to the httpd.conf file.

AddType application/x-httpd-php .php

Most will want to add index.php to the DirectoryIndex directive.
Some will change .html to also use application/x-httpd-php

If you want to get fancy or use virtual hosts, there are a lot of nice ways to do this, most involve breaking the httpd.conf file into smaller parts. For example, the AddType directives for php I put into a separate file that sits in the conf/extras directory and then include it in the httpd.conf file. I also create a vhosts.d directory which contains configurations for each virtual host. I include those files by the directive “Include conf/vhosts.d/*.conf” This is a common way of doing it (e.g., Gentoo Linux).

I have one other trick up my sleeve to make things easy. Once I have a working version of Apache and PHP, I make sure I copy the “config.nice” file to a different location and name them something like apache.config.nice and php.config.nice. These files are normally found in the root of the source code after you have run ./configure. When you download a new version of the source, you can copy the “nice” file to the source directory, modify it slightly (version stuff), and run it instead of typing out the whole configure line. Basically, upgrading to a new version ends up being

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

and a quick delete of the old symlink and create the new one. ( To answer a question, the && says basically on the command line, if the previous command exits successfully do the next command. So if “make” works then do “make install” else exit. )

I hope you have found the way I do it informational and may inspire some to go ahead and install Apache and PHP from source. I also download pureFTPd and compile it from source. Although most on the Mac seem to prefer ProFTPd, I have always liked pureFTPd’s flexibility, security, and ease of use. And for the rest of you, grab MAMP or XAMPP and start playing!

Leave a Reply