Installing Apache 2.4 and PHP 5.4 from source

I’ve just been through the process of installing the latest versions (as of this writing) of Apache 2.4.2 and PHP 5.4.5 from source on a new Debian 6 virtual machine for the first time.

I had some small issues along the way and thought I’d do a short post on the process I went through to get it working and resolve various errors which may help someone out. First we will install Apache, followed by PHP.

Prerequisites

Before starting you will need make, gcc and g++ installed.

apt-get install make gcc g++

I also had the latest version of MySQL installed (5.5.25a), as I wanted to use this later on. The latest version was downloaded from the Dotdeb repository

Installing Apache 2.4.2 from source

Apache was then downloaded from http://httpd.apache.org/download using wget on one of the mirrors in my country and then extracted.

wget http://apache.mirror.uber.com.au//httpd/httpd-2.4.2.tar.gz
tar xf httpd-2.4.2.tar.gz
cd httpd-2.4.2/
./configure --enable-so
...
Configuring Apache Portable Runtime library ...
checking for APR... no
configure: error: APR not found.  Please read the documentation.

--enable-so is needed later on for PHP.

At this point I downloaded APR 1.4.6 from apr.apache.org/download using wget and then extracted it.

cd ..
wget http://apache.mirror.uber.com.au//apr/apr-1.4.6.tar.gz
tar xf apr-1.4.6.tar.gz
cd apr-1.4.6/
./configure
make
make install

Apache will also need APR-util installed, which can be downloaded from the apr.apache.org/download page too. When you run configure you will have to specify where the apr config is, you should see this location in the output from make install after finishing apr above.

cd ..
wget http://apache.mirror.uber.com.au//apr/apr-util-1.4.1.tar.gz
tar xf apr-util-1.4.1.tar.gz
cd apr-util-1.4.1/
./configure --with-apr=/usr/local/apr/bin/apr-1-config
make
make install

Now that’s complete we can try Apache again.

cd ../httpd-2.4.2/
./configure --enable-so
...
checking for pcre-config... false
configure: error: pcre-config for libpcre not found. PCRE is required and available from http://pcre.org/

pcre-8.30.tar.gz was then downloaded from http://pcre.org and for something different, I uploaded it to the server with SFTP to the root directory.

cd ..
tar xf pcre-8.30.tar.gz
cd pcre-8.30/
./configure
make
make install

Now to try Apache again…

cd ../httpd-2.4.2/
./configure --enable-so
make
make install

That’s it, Apache should now be ready to go, let’s move into the default installation and start it up.

cd /usr/local/apache2/bin/
./apachectl start
/usr/local/apache2/bin/httpd: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
ldconfig
./apachectl start
./apachectl -v
Server version: Apache/2.4.2 (Unix)
Server built:   Jul 26 2012 20:51:23

That error relating to the shared libraries was resolved by running ldconfig, see the man page on that for more information.

Browsing to the IP address in my web browser now brings up the Apache default page. As ServerSignature is enabled by default, using Firebug for FireFox I can see “Apache/2.4.2 (Unix)” as the Server header in the response headers, confirming the new install of Apache has served my page.

Installing PHP 5.4.5 from source

PHP was downloaded from http://au.php.net/get/php-5.4.5.tar.gz/from/a/mirror and then uploaded to the root directory on the server using SFTP.

I’m configuring PHP with apxs, this wasn’t working for me unless Apache was configured with --enable-so. I was after MySQL support, feel free to leave --with-mysql off.

cd /root
tar xf php-5.4.5.tar.gz
cd php-5.4.5/
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=mysqlnd
...
configure: error: xml2-config not found. Please check your libxml2 installation.
apt-get install libxml2-dev
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=mysqlnd
make
make install
php -v
PHP 5.4.5 (cli) (built: Jul 26 2012 21:18:58)

Configuration

Once you have both Apache and PHP installed and running there is a bit of configuration to do so that Apache knows how to use PHP. I created a phpinfo file here /usr/local/apache2/htdocs/phpinfo.php and it will not display in a browser prior to configuration.

First off, let’s copy the library file over to Apache.

cp /root/php-5.4.5/libs/libphp5.so /usr/local/apache2/modules/

Next you will want to edit the Apache configuration file in /usr/local/apache2/conf/httpd.conf and add the following lines into it.

AddType text/html .php
AddHandler php5-script	.php

This line should already be in there, referring to the .so file that was previously copied.

LoadModule php5_module modules/libphp5.so

You probably want to edit DirectoryIndex index.html and include index.php after this.

Once complete, restart Apache.

cd /usr/local/apache2/bin/
./apachectl restart

Summary

That’s it, you should now be able to load up the phpinfo.php page which will output various information related to the installation. This is quite minimalistic, you will probably want additional things with your installation however my purpose was extremely basic.

  1. I was getting really frustrated trying to do just this. I was not building php with apxs. I was trying with-apache to no avail. Thank you. This worked perfectly.

  2. Thank you for the assistance. My configuration was a bit different, but your article helped to point me in the right direction. :mrgreen:

  3. Thanks! Helped me…

  4. thank you very much it is working

  5. great! worked perfectly!

  6. I don’t believe –enable-so is required anymore for Apache 2.4 as the configure script automatically detects if it is needed now.

  7. Анатолий

    Thank U! Helped guide is excellent, it is a pity there is no MySQL would be a complete picture-)
    Thank U!

  8. life saver

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>