Back to posts.

Compile Apache, PHP and MySQL on Mac 10.10

I've always been a enthusiastic for the MySQL, PHP and Apache setup though the last couple of years compilation of Apache on Mac hasn't been that good. Even the current build script does not compile apache without problems. It doesn't seem that apache is going to fix this problem any time soon, so I spend some time to create a working script that compiles apache with php and mysql on Mac 10.10.

You can find the complete build script on Gist or copy paste the version below. I've a directory ~/Documents/www/ where I compiled and installed the binaries into a subdir called servers.

I've ran the complete script several times and everything works fine; it will compile and install a complete MySQL, PHP and Apache setup. Once everything has been compiled you need to configure PHP, Apache and MySQ. You can use any configuration options you want, though I'll describe the changes I made.

Configure Apache

I like to use local domain names like http://project-name.localhost and therefore I use the mod_vhost_alias.so module. For this I configure a stub vhost that I can use for all my local projects which follow this naming. My ~/Documents/www/ directory is my document root and all my projects are stored underneath that directory. Each website I work on has it's own VirtualDocumentRoot in it's html directory. For example, at some point I may have:

~Documents/www/project1/html
~Documents/www/project2/html
~Documents/www/project3/html

Open www/servers/installed/conf/httpd.conf and check/change/add the following:

- Load the php module:
  LoadModule php5_module  modules/libphp5.so
  LoadModule vhost_alias_module modules/mod_vhost_alias.so
  LoadModule rewrite_module modules/mod_rewrite.so
 
- Hostname:
  ServerName localhost:80
 
- Set DocumentRoot:
  DocumentRoot "/Users/roxlu/Documents/www/"
  <Directory "/Users/roxlu/Documents/www/">
 
- Virtual hosts (uncomment at the bottom):
  Include conf/extra/httpd-vhosts.conf
 
- Load php files:
  <FilesMatch "\.ph(p[2-6]?|tml)$">
    SetHandler application/x-httpd-php
  </FilesMatch>
 
- Make sure index.php is loaded
  <IfModule dir_module>
    DirectoryIndex index.php index.html
  </IfModule>

Open www/servers/installed/conf/extra/httpd-vhosts.conf and add something like:

<VirtualHost *:80>
     ServerName localhost
     ServerAlias *.localhost
     UseCanonicalName off
     VirtualDocumentRoot /Users/roxlu/Documents/wwww/%1/html/
 
      <Directory /Users/roxlu/Documents/www/>
           AllowOverride All
           Order deny,allow
           Allow from all
      </Directory>
</VirtualHost>

Configure MySQL

The build script below will copy the mysql_install_db from the www/servers/installed/scripts/ directory into the directory that was using for --prefix while building the libraries and applications. The script will also execute this mysql_install_db to install the default databases for MySQL. When the databases are installed you can set the default password after starting MySQL.

cd www/servers/installed/bin/
./mysqld_safe & 
./mysqladmin -u root password 'YOUR_PASSWORD'

Install PHPMyAdmin

  • Download the latest (english) version of PHPMyAdmin.
  • Extract into www/phpmyadmin/html and copy config.sample.inc.php to config.inc.php.
  • Edit:
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'somepassword';

Build script

#!/bin/bash
 
d=${PWD}
sd=${d}/sources
bd=${d}/build
id=${d}/installed/
 
PATH_ORIG=${PATH}
CC_ORIG=${CC}
CPP_ORIG=${CPP}
 
function set_env_new {
    export PATH=${id}/bin:${id}:${PATH}
    export CC=/usr/bin/cc
    export CPP=/usr/bin/cpp
}
 
function set_env_old {
    export PATH=${PATH_ORIG}
    export CC=${CC_ORIG}
    export CPP=${CPP_ORIG}
}
 
set_env_new
 
if [ ! -d ${sd} ] ; then
    mkdir ${sd}
fi
 
if [ ! -d ${bd} ] ; then
    mkdir ${bd}
fi
 
if [ ! -d ${id} ] ; then
    mkdir ${id}
fi
 
# Download libtool
if [ ! -d ${sd}/libtool ] ; then
    cd ${sd}
    if [ ! -f libtool.tar.gz ] ; then
        curl -L -o libtool.tar.gz http://ftpmirror.gnu.org/libtool/libtool-2.4.4.tar.gz
    fi
    tar -zxvf libtool.tar.gz
    mv libtool-2.4.4 libtool
fi
 
# Download autoconf
if [ ! -d ${sd}/autoconf ] ; then
    cd ${sd}
    if [ ! -f autoconf.tar.gz ] ; then
        curl -L -o autoconf.tar.gz http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
    fi
    tar -zxvf autoconf.tar.gz
    mv autoconf-2.69 autoconf
fi
 
# Download Apache
if [ ! -d ${sd}/apache ] ; then
    cd ${sd}
    if [ ! -f apache.tar.gz ] ; then
        curl -L -o apache.tar.gz http://apache.proserve.nl//httpd/httpd-2.4.10.tar.gz
    fi
 
    tar -zxvf apache.tar.gz
    mv httpd-2.4.10 apache
fi
 
# Download php
if [ ! -d ${sd}/php ] ; then
    cd ${sd}
    if [ ! -f php.tar.gz ] ; then
        curl -L -o php.tar.gz http://de1.php.net/get/php-5.6.4.tar.gz/from/this/mirror
    fi
 
    tar -zxvf php.tar.gz
    mv php-5.6.4 php
fi
 
# Download pcre
if [ ! -d ${sd}/pcre ] ; then
    cd ${sd}
    if [ ! -f pcre.tar.gz ] ; then
        curl -L -o pcre.tar.gz ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz
    fi
    tar -zxvf pcre.tar.gz
    mv pcre-8.36 pcre
fi
 
# Download APR
if [ ! -d ${sd}/apr ] ; then
    cd ${sd}
    if [ ! -f apr.tar.gz ] ; then
        curl -L -o apr.tar.gz http://mirrors.supportex.net/apache//apr/apr-1.5.1.tar.gz
    fi
    tar -zxvf apr.tar.gz
    mv apr-1.5.1 apr
fi
 
# Download APR-util
if [ ! -d ${sd}/apr-util ] ; then
    cd ${sd}
    if [ ! -f apr-util.tar.gz ] ; then
        curl -L -o apr-util.tar.gz http://mirrors.supportex.net/apache//apr/apr-util-1.5.4.tar.gz
    fi
    tar -zxvf apr-util.tar.gz
    mv apr-util-1.5.4 apr-util
fi
 
# Download MySQL
if [ ! -d ${sd}/mysql ] ; then
    cd ${sd}
    if [ ! -f mysql.tar.gz ] ; then
        curl -L -o mysql.tar.gz http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.22.tar.gz
    fi
    tar -zxvf mysql.tar.gz
    mv mysql-5.6.22 mysql
fi
 
# Download libpng (for php)
if [ ! -d ${sd}/png ] ; then
    cd ${sd}
    if [ ! -f png.tar.gz ] ; then
        curl -L -o png.tar.gz http://prdownloads.sourceforge.net/libpng/libpng-1.6.15.tar.gz?download
    fi
    tar -zxvf png.tar.gz
    mv libpng-1.6.15 png
fi
 
# Download libjpeg
if [ ! -d ${sd}/jpg ] ; then
    cd ${sd}
    if [ ! -f jpg.zip ] ; then
        curl -L -o jpg.zip http://downloads.sourceforge.net/project/libjpeg/libjpeg/6b/jpegsr6.zip
    fi
    unzip jpg.zip
    mv jpeg-6b jpg
fi
 
# Download libmcrypt
if [ ! -d ${sd}/mcrypt ] ; then
    cd ${sd}
    if [ ! -f mcrypt.tar.gz ] ; then
        curl -o mcrypt.tar.gz -L http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
    fi
    tar -zxvf mcrypt.tar.gz
    mv libmcrypt-2.5.8 mcrypt
fi
 
# Download libxml
if [ ! -d ${sd}/xml ] ; then
    cd ${sd}
    if [ ! -f xml.tar.gz ] ; then
        curl -o xml.tar.gz -L ftp://xmlsoft.org/libxml2/libxml2-2.9.2.tar.gz
    fi
    tar -zxvf xml.tar.gz
    mv libxml2-2.9.2 xml
fi
 
# Download libcurl
if [ ! -d ${sd}/curl ] ; then
    cd ${sd}
    if [ ! -f curl.tar.gz ] ; then
        curl -o curl.tar.gz -L http://curl.haxx.se/download/curl-7.39.0.tar.gz
    fi
    tar -zxvf curl.tar.gz
    mv curl-7.39.0 curl
fi
 
# Download libz
if [ ! -d ${sd}/zlib ] ; then
    cd ${sd}
    if [ ! -f zlib.tar.gz ] ; then
        curl -o zlib.tar.gz -L http://zlib.net/zlib-1.2.8.tar.gz
    fi
    tar -zxvf zlib.tar.gz
    mv zlib-1.2.8 zlib
fi
 
# Download libiconv
if [ ! -d ${sd}/iconv ] ; then
    cd ${sd}
    if [ ! -f iconv.tar.gz ] ; then
        curl -o iconv.tar.gz -L http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
    fi
    tar -zxvf iconv.tar.gz
    mv libiconv-1.14 iconv
fi
 
# Compile libtool
if [ ! -f ${id}/bin/libtool ] ; then
    cd ${sd}/libtool
    ./configure \
        --prefix=${id}
    make
    make install
fi
 
# Compile autoconf
if [ ! -f ${id}/bin/autoconf ] ; then
    cd ${sd}/autoconf
    ./configure --prefix=${id}
    make
    make install
fi
 
# Compile apr
if [ ! -f ${id}/lib/libapr-1.a ] ; then
    cd ${bd}
    if [ ! -d apr ] ; then
        mkdir apr
    fi
    cd apr
    ${sd}/apr/configure \
         --prefix=${id} 
    make
    make install
fi
 
# Compile apr-util
if [ ! -f ${id}/lib/libaprutil-1.a ] ; then
    cd ${bd}
    if [ ! -d apr-util ] ; then
        mkdir apr-util
    fi
    cd apr-util
    ${sd}/apr-util/configure \
         --prefix=${id} \
         --with-apr=${id}/bin
 
    make
    make install
fi
 
# Compile pcre
if [ ! -f ${id}/lib/libpcre.a ] ; then
    cd ${bd}
    if [ ! -d pcre ] ; then
        mkdir pcre
    fi
    cd pcre
    ${sd}/pcre/configure \
         --enable-static=yes \
         --enable-shared=no \
         --prefix=${id}
    make
    make install
fi
 
# Compile apache
if [ ! -f ${id}/bin/apachectl ] ; then 
    cd ${bd}
    if [ -d apache ] ; then
        rm -rf apache
    fi
    mkdir apache
    cd apache
 
    cd ${bd}/apache
    ${sd}/apache/configure \
         --prefix=${id} 
    make
    make install
fi
 
# Compile MySQL
if [ ! -f ${id}/bin/mysqld_safe ] ; then
    if [ ! -d ${bd}/mysql ] ; then
        mkdir ${bd}/mysql
    fi
    cd ${bd}/mysql
    cmake -DCMAKE_INSTALL_PREFIX=${id} \
          ${sd}/mysql/
    make
    make install
 
    # Install MySQL database.
    if [ ! -f ${id}/mysql_install_db ] ; then
        cp ${id}/scripts/mysql_install_db ${id}
        cd ${id}
        ./mysql_install_db
    fi
fi
 
# Compile libz
if [ ! -f ${id}/lib/libz.a ] ; then 
    cd ${sd}/zlib
    ./configure --prefix=${id} 
    make
    make install
fi
 
# Compile libpng
if [ ! -f ${id}/lib/libpng.a ] ; then
    if [ ! -d ${bd}/png ] ; then
        mkdir ${bd}/png
    fi
 
    set_env_old
 
    cd ${bd}/png
    ${sd}/png/configure --prefix=${id} \
         --enable-shared=no \
         --enable-static=yes
    make
    make install
 
    set_env_new
fi
 
# Compile libjpg: TODO build script of libjpeg is borked! 
if [ 0 -eq 1 ] ; then
    cd ${bd}
    if [ ! -d jpg ] ; then
        mkdir jpg
    fi
    set -x
    cd jpg
    cd ${sd}/jpg/
    ./configure --prefix=${id} \
                --enable-shared=no \
                --enable-static=yes
    make
    make install
fi
 
# Compile mcrypt
if [ ! -f ${id}/lib/libmcrypt.a ] ; then
    cd ${sd}/mcrypt
    ./configure --prefix=${id} \
                --enable-shared=no \
                --enable-static=yes
    make
    make install
fi
 
# Compile iconv
if [ ! -f ${id}/lib/libiconv.a ] ; then
    cd ${sd}/iconv
    ./configure --prefix=${id} \
                --enable-static-yes \
                --enable-shared=no
    make
    make install
fi
 
# Compile libxml
if [ ! -f ${id}/lib/libxml2.a ] ; then
    cd ${bd}
    if [ ! -d libxml ] ; then
        mkdir libxml
    fi
    cd ${bd}/libxml
    ${sd}/xml/configure --prefix=${id} \
         --enable-shared=no \
         --enable-static=yes \
         --with-iconv=${id}
    make
    make install
fi
 
# Compile libcurl
if [ ! -f ${id}/lib/libcurl.a ] ; then
    cd ${bd}
    if [ ! -d libcurl ] ; then
        mkdir libcurl
    fi
    cd libcurl
    ${sd}/curl/configure --prefix=${id} \
         --enable-static=yes \
         --enable-shared=no 
    make
    make install
fi
 
 
# Compile php
if [ 1 -eq 1 ] ; then
 
    # --with-jpeg-dir=${id} \
    # --with-freetype=${id} \
    # --with-ldap \
 
    if [ ! -d ${bd}/php ] ; then
        mkdir ${bd}/php
    fi
 
    export LIBS="-lresolv"
    export DYLD_LIBRARY_PATH=${id}/lib/:${DYLD_LIBRARY_PATH}
 
    cd ${bd}/php
    ${sd}/php/configure --prefix=${id} \
                --without-pear \
                --with-apxs2=${id}/bin/apxs \
                --with-mysql=${id} \
                --with-pdo-mysql=${id}/bin/mysql_config \
                --with-png-dir=${id} \
                --with-curl=${id} \
                --with-libxml-dir=${id} \
                --with-mcrypt=${id} \
                --with-zlib=${id} \
                --with-iconv=${id} \
                --with-gd \
                --with-mhash \
                --enable-mbstring \
                --enable-ftp \
                --enable-exif \
                --enable-bcmath \
                --enable-soap \
                --enable-sockets \
                --enable-ftp \
                --enable-zip \
                --enable-shared=yes \
                --enable-static=yes \
                --enable-opcache=no
 
    make
    make install
fi