How to build Apache 2.4, PHP 5.4 and MySQL/MariaDB from source on older Linux systems

Note: This is a blog post I wrote about 9 months ago but never actually never finished. The server this was for died upon me so I was not able to verify that this was bulletproof and kinda forgot about the hole post before today. I'll decided though to publish it as is.

Some of us (lik me) are stuck with old Centos 4.x installations. My web servers are located hundreds of miles away and gaining physical access make a Centos 4.x to 5.x/6.x upgrade more and less impossible. The second best way of fixing this is to compile your LAMP stack from source, should say AMP then the scope of this is not to compile the Linux kernel. Was original hoping for that someone had prepacked RPM/SRPM packages but could not find any repo. what where not out of date.

Build Apache httpd

Download the latest Apache 2.4 source - http://httpd.apache.org/docs/2.4/install.html
You system wide apr and apr-util is also prob. outdated, so download the source to these two projects. Download apr and apr-util from http://apr.apache.org/download.cgi

Make sure you have APR and APR-Util already installed on your system. If you don't, or prefer to not use the system-provided versions, download the latest versions of both APR and APR-Util from Apache APR, unpack them into ./srclib/apr and ./srclib/apr-util (be sure the domain names do not have version numbers; for example, the APR distribution must be under ./srclib/apr/) and use ./configure's --with-included-apr option. On some platforms, you may have to install the corresponding -dev packages to allow httpd to build against your installed copy of APR and APR-Util.

configure (genereate make file)

CFLAGS="-O2" ./configure --prefix=/usr/local/apache2 --with-included-apr --enable-so --with-expat=builtin

APR related problem

Make sure that you run configure with '--with-expat=builtin' or else it might fail:

httpd-2.4.1/srclib/apr-util/.libs/libaprutil-1.so: undefined reference to `XML_StopParser'
collect2: ld returned 1 exit status

This normally means that it picked up the system wide APR/APR-utils.

compile and install

If you have all libraries needed apache httpd should build without any errors. If it fails, make sure you have the dev versions of the libraries make asks for. To compile and install:

make
make install

Build MySQL/MariaDB

Before you try to run configure, make sure ncurses-devel (ncurses-dev) is installed. Pull down latest 5.3.x source code from http://mariadb.org/. MariaDB 5.5.x uses cmake to build from source.

./configure \
--disable-distribution \
--enable-assembler \
--with-gnu-ld \
--with-charset=utf8 \
--with-extra-charsets=none \
--without-uca \
--with-pthread \
--with-mysqld-user=mysql \
--with-fast-mutexes \
--with-atomic-ops=smp \
--with-big-tables \
--with-libevent=yes \
--with-mysqlmanager=no \
--without-man \
--with-plugins=max-no-ndb \
--prefix=/usr/local/mysql \

If configure successfully completed, run make and make install to compile and install the binaries to /usr/local/mysql

Configure and test MariaDB/MySQL

To test the server without interrupting the already running db. server do we need to change the default location of the databases and TCP port it listen on. Copy you running config.
copy /etc/my.cnf /usr/local/mysql

Change the following settings.

port=8001
datadir=/var/lib/mariadb
socket=/var/lib/mariadb/mysql.sock
err-log=/var/log/mariadb.log
pid-file=/var/run/mariadb/mysqld.pid

Create the directories that we specified above, and give the user db-user (mysql) full access.

mkdir /var/lib/mariadb
mkdir /var/run/mariadb
chown mariadb: /var/lib/mariadb
chown mysql: /var/run/mariadb

Install init. database
/usr/local/mysql/bin/mysql_install_db --user=mysql --defaults-file=/usr/local/mysql/my.cnf

Start the db server

/usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mariadb --defaults-file=/usr/local/mysql/my.cnf --user=mysql 
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --datadir=/var/lib/mariadb  --user=mysql

Connect to it
/usr/local/mysql/bin/mysql --port=8001 --socket=/var/lib/mariadb/mysql.sock -u root

Stop it
/usr/local/mysql/bin/mysqladmin --socket=/var/lib/mariadb/mysql.sock -u root shutdown

Build PHP 5.4.x

Download PHP http://php.net/ PHP 5.4.x

Configure and compile PHP

./configure \
CFLAGS="-O3" CXX=gcc CXXFLAGS="-O3" \
--with-gd \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo_mysql=/usr/local/mysql \
--enable-phar \
--enable-mbstring \
make
make install

Configure problems

If configure fails with something like:
configure: error: jpeglib.h not found
is it normally due to that you are missing the jpeg/png dev packages on your system. Make sure that PHP libDF locates libjpeg-devel and ibpng-devel. If your system is very old these it could be a good idea to download and compile these before you try to rerun configure.

libJPEG (Independent JPEG Group)

http://www.ijg.org/
./configure --enable-shared --prefix=/usr/local/
make
make install

libPNG

http://www.libpng.org/pub/png/libpng.html
./configure --prefix=/usr/local/
make
make install

Zlib

Download and install zlib to /usr/local/lib

libGD

Time to rerun configure including the newly built libraries:

CFLAGS="-O3" CXX=gcc CXXFLAGS="-O3" ./configure \
--with-jpeg-dir=/usr/local \
--with-png-dir=/usr/local \
--with-gd \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo_mysql=/usr/local/mysql \
--with-zlib --with-zlib-dir=/usr/local/lib \
--enable-phar \
--enable-mbstring