The first thing we want to do is build PHP, but we need to make sure that it has all the options needed for our specialized Web stack (nanoweb, php, sqlite, and cognifty). I’ve developed a custom bash file to call PHP’s regular configure script with all the options we will need. Saving the configure options in a bash file like this provides a consistent approach to building PHP. With the file saved in SVN, we can ensure that we won’t forget certain flags every time we compile PHP.
This build script will be called from the ant build script. Here are the 3 targets we will need for building and installing PHP in detail.
<property name="php-version" value="5.2.5" />
<property name="php-package" value="php-5.2.5.tar.gz" />
<!-- php 5 -->
<target name="extract-php"
description="Extract the PHP tar into the build directory">
<!-- creates build/php-5.2.5 -->
<untar src="${libs}/${php-package}" dest="${build}/" overwrite="false" compression="gzip"/>
<chmod file="build/php-${php-version}/configure" perm="ugo rx"/>
<chmod file="build/php-${php-version}/build/shtool" perm="ugo rx"/>
</target>
The properties help us update the php version and only change one spot in the build file. The can be thought of like variables in a script. The first target is just “extract-php” and it untars the file from the libs directory into the build directory.
<target name="build-php" depends="extract-php"
description="runs configure, make">
<echo message="Now configuring PHP with scripts/configure-php.sh in dir build/php-${php-version}/"/>
<copy file="scripts/configure-php.sh" tofile="build/php-${php-version}/configure-php.sh"/>
<chmod file="build/php-${php-version}/configure-php.sh" perm="ugo rx"/>
<exec dir="build/php-${php-version}/" executable="/bin/sh">
<arg value="./configure-php.sh"/>
</exec>
<exec dir="build/php-${php-version}/" executable="make">
<!--
<arg value="./configure-php.sh"/>
-->
</exec>
</target>
This next target does the configure and make steps on PHP. It relies on having the proper gnu make program installed. In this way, the ant build file is acting just like a bash script, but it is written in XML and can be expanded for other operating systems quite easily. You can see that this “build-php” target relies (or depends) on the “extract-php” target. This means that whenever you run this target, the “extract-php” target will be run first.
<target name="install-php">
<copy file="build/php-${php-version}/sapi/cgi/php-cgi" tofile="${run-root}/nanoweb/usr/bin/php"/>
<chmod file="${run-root}/nanoweb/usr/bin/php" perm="ugo rx"/>
</target>
The last target copies our final php-cgi binary into place in our “run-root” directory. You don’t need to create the directories before hand, ant will create them as they are needed. The chmod command ensures that the PHP binary is executable after installation. This target does not rely on any of the previous targets because they take quite a bit of time to complete. If you want to completely erase your “run-root” directory you can skip compiling PHP from script by calling this “install-php” target.
From the command line, you can the build process like this
$> /opt/ant/bin/ant compile-php ... output flying by... [exec] [exec] Build complete. [exec] Don't forget to run 'make test'. [exec] $> /opt/ant/bin/ant install-php
Nanoweb doesn’t require any compilation, per se. But it does have an installation script that will help us put the files in the proper place. The only problem is that the installation script is hard-coded to install nano web only in /usr. I have created a patch to the installer which will allow us to pass a –prefix flag and specify our “run-root” directory as the target installation directory.
The ant build script knows to look in the patches directory and apply this patch when you are installing nanoweb. Let’s look at the ant targets for nanoweb:
<!-- nanoweb 2.2 -->
<property name="nanoweb-version" value="2.2.9" />
<property name="nanoweb-package" value="nanoweb_2.2.9.tgz" />
<target name="extract-nanoweb"
description="Extract the nanoweb tar into the build directory">
<!-- creates build/nanoweb_2.2.9 -->
<untar src="${libs}/${nanoweb-package}" dest="${build}/" overwrite="false" compression="gzip"/>
</target>
Again, we see a target that simply extracts the source from the “libs” directory and places it in the build directory.
<target name="patch-nanoweb" depends="extract-nanoweb"
description="Apply patches to the nanoweb source">
<copy file="patches/01_nanoserv_add_prefix.diff" todir="build/nanoweb_${nanoweb-version}/"/>
<exec dir="build/nanoweb_${nanoweb-version}/" executable="patch">
<arg value="-p0"/>
<arg value="-i"/>
<arg value="01_nanoserv_add_prefix.diff"/>
</exec>
</target>
This target depends on the extraction target and applies the patch previously discussed,
<target name="install-nanoweb" depends="patch-nanoweb"
description="Use nanoweb's patched install-sh to install into run-root">
<mkdir dir="${user.dir}/run-root/nanoweb/"/>
<exec dir="build/nanoweb_${nanoweb-version}/" executable="sh">
<arg value="./install-sh"/>
<arg value="--prefix=${user.dir}/run-root/nanoweb"/>
</exec>
</target>
This last target simply calls nanoweb’s own installation script, while passing our new prefix parameter. In ant the ${user.dir} property defaults to the current working directory.
To run these targets, execute these commands from the command line:
$> /opt/ant/bin/ant install-nanoweb ...output flying by...
Take a moment to look at the resulting structure in your “run-root” directory. The nanoweb installation looks a bit messy, but it is fully functional. To test it out, cd into the directory “run-root/nanoweb/” and run the following command:
$> cd run-root/nanoweb/ $> ./usr/sbin/nanoctl start
Now, if you point your browser at http://localhost:8443 you should see the default nanoweb page. If you don’t, stop and get it working before moving on.
What we’ve seen here is a repeatable build process to create a Web server stack relying only on PHP. With just a few ant commands we can rebuild everything from scratch. Later, in another article, we will add more targets to the ant script which will allow the script to package our Web stack for deployment to any server. But, before we can deploy anything to a server, we need to have something worthwhile to deploy...
Next Page: Installing Cognifty