FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Shell Scripting » PHP5 Custom Install Shell Script Example

by 1 comment

Today I successfully learned how to compile and run multiple custom php installations for a DreamHost account, and to get it working I came upon a simple shell script that I made a couple changes to.

This script downloads the correct files, php extensions, and source code that you will need to get a custom php5 environment set up on DH. Its a rather dangerous file though if you don't know what you are doing, because compiling your own custom php-cgi for your web sites isn't exactly a common thing. This file will delete a couple of your directories if you aren't careful, I just posted it here because I enjoyed hacking this out and this is a pretty cool example of some of the things you can do with the unix shell.

Its fun to rotate between languages like that, although shell programming isn't exactly the most exciting thing compared to python, ruby, and other great Object->Oriented languages taking over. But they do all run in the shell :)

These are alternative PHP 5 install scripts created by Charles Wiltgen, who created them to work around problems he was having with the "main" PHP 5 install script.

Discussed in more detail on the DreamHost Customer Wiki.

Functions in Shell Scripts

Two changes I made were adding some functionality to the code with these two tiny gunctions, one downloads compressed files, the other decompresses them.

function aa_unpack () {
  # compressed, tar and gzip files to DISTDIR
  if [ -f $DISTDIR/$1* ] ; then
    echo Extracting "$1";
    zcat ${DISTDIR}/$1* | tar -xvf - &>/dev/null;
    echo Done.;  echo; wait
  fi
}

function askapache_grab () {
  #saves file to SRCDIR
    echo `basename $1`
  curl -L --retry 20 --max-time 1800 --retry-delay 30 -# -f --max-redirs 4 --remote-name "$1"
}

php5-install-prep.sh - Source Code

#!/bin/sh

# Version 0.6, 2007-12-16
#
# - Updated 2007-12-16 by AskApache (www.askapache.com)
#   - Implemented functions to fetch the URI and decompress it
#   - Added a couple more error-checks
#   - Replaced wget with cURL
#   - Added more to help keep it from getting killed
#  - Updated to php-5.2.3, curl-7.17.1, freetype-2.3.5
# - Updated 2007-01-15 by Charles Wiltgen (charles@wiltgen.net)
#   - Make "nicer" to help keep it from getting killed by DreamHost
#   - Make less verbose to keep signal-to-noise level high
# - Updated 2006-12-25 by Carl McDade (hiveminds.co.uk)
#   - Allow memory limit and freetype

# Abort on any errors
set -e

# The domain in which to install the PHP CGI script.
export DOMAIN="askapache.com"

# Where do you want all this stuff built? I'd recommend picking a local filesystem.
# ***Don't pick a directory that already exists!***
SRCDIR=${HOME}/source

# And where should it be installed?
INSTALLDIR=${HOME}/php5

# Set DISTDIR to somewhere persistent, if you plan to muck around with this
# script and run it several times!
DISTDIR=${HOME}/dist

# Update version information here.
PHP5="php-5.2.3"
LIBICONV="libiconv-1.11"
LIBMCRYPT="libmcrypt-2.5.7"
LIBXML2="libxml2-2.6.27"
LIBXSLT="libxslt-1.1.18"
MHASH="mhash-0.9.7.1"
ZLIB="zlib-1.2.3"
CURL="curl-7.17.1"
LIBIDN="libidn-0.6.8"
CCLIENT="imap-2004g"
CCLIENT_DIR="imap-2004g"
FREETYPE="freetype-2.3.5"

# Push the install dir's bin directory into the path
export PATH=${INSTALLDIR}/bin:$PATH

function aa_unpack () {
  # compressed, tar and gzip files to DISTDIR
  if [ -f $DISTDIR/$1* ] ; then
    echo Extracting "$1";
    zcat ${DISTDIR}/$1* | tar -xvf - &>/dev/null;
    echo Done.;  echo; wait
  fi
}

function askapache_grab () {
  #saves file to SRCDIR
    echo `basename $1`
  curl -L --retry 20 --max-time 1800 --retry-delay 30 -# -f --max-redirs 4 --remote-name "$1"
}

echo
echo --------------------------------------------------
echo --   Run this script before php5-install.sh     --
echo --------------------------------------------------
echo
echo - Downloads and unpacks all prerequisite packages
echo - **SRCDIR and DISTDIR will be deleted**
echo
read -p  "        (Press any key to continue)" temp;
echo;echo

# cleanup to remove source and dist directories if present
if [ -d "$SRCDIR" ] || [ -d "$DISTDIR" ];then
  echo
  echo --- Cleaning up any previous attempts ---
  rm -rf $SRCDIR $DISTDIR &>/dev/null
  echo Done.
  echo
  wait
fi

#setup directories
mkdir -p ${SRCDIR} ${INSTALLDIR} ${DISTDIR} &>/dev/null

# Get all the required packages
echo;echo
echo --- Downloading all required packages ---
echo

cd ${DISTDIR}
askapache_grab http://us.php.net/distributions/${PHP5}.tar.gz
askapache_grab http://mirrors.usc.edu/pub/gnu/libiconv/${LIBICONV}.tar.gz
askapache_grab http://umn.dl.sourceforge.net/sourceforge/mcrypt/${LIBMCRYPT}.tar.gz
askapache_grab ftp://xmlsoft.org/libxml2/${LIBXML2}.tar.gz
askapache_grab ftp://xmlsoft.org/libxml2/${LIBXSLT}.tar.gz
askapache_grab http://umn.dl.sourceforge.net/sourceforge/mhash/${MHASH}.tar.gz
askapache_grab http://www.zlib.net/${ZLIB}.tar.gz
askapache_grab http://curl.askapache.com/download/${CURL}.tar.gz
askapache_grab http://easynews.dl.sourceforge.net/sourceforge/freetype/${FREETYPE}.tar.gz
askapache_grab ftp://alpha.gnu.org/pub/gnu/libidn/${LIBIDN}.tar.gz
askapache_grab ftp://ftp.cac.washington.edu/imap/old/${CCLIENT}.tar.Z
wait
echo Done.

# Extract the files from the required packages.
echo;echo;echo
echo --- Unpacking downloaded archives. This process may take several minutes! ---
echo

cd ${SRCDIR}
aa_unpack ${PHP5}
aa_unpack ${LIBICONV}
aa_unpack ${LIBMCRYPT}
aa_unpack ${LIBXML2}
aa_unpack ${LIBXSLT}
aa_unpack ${MHASH}
aa_unpack ${ZLIB}
aa_unpack ${CURL}
aa_unpack ${LIBIDN}
aa_unpack ${CCLIENT}
aa_unpack ${FREETYPE}
wait

echo --------------------------------------------------
echo -- Done downloading and unpacking prerequisites --
echo --------------------------------------------------

exit 0;

php5-install-prep.sh - Output

[root@~/.^*][~/dm/e]
$ ./php5-install-prep.sh

--------------------------------------------------
-- Run this script before php5-install.sh --
--------------------------------------------------

- Downloads and unpacks all prerequisite packages
- **SRCDIR and DISTDIR will be deleted**

        (Press any key to continue)

--- Cleaning up any previous attempts ---
Done.

--- Downloading all required packages ---

php-5.2.3.tar.gz
######################################################################## 100.0%
libiconv-1.11.tar.gz
######################################################################## 100.0%
libmcrypt-2.5.7.tar.gz
######################################################################## 100.0%
libxml2-2.6.27.tar.gz
######################################################################## 100.0%
libxslt-1.1.18.tar.gz
######################################################################## 100.0%
mhash-0.9.7.1.tar.gz
######################################################################## 100.0%
zlib-1.2.3.tar.gz
######################################################################## 100.0%
curl-7.17.1.tar.gz
######################################################################## 100.0%
freetype-2.3.5.tar.gz
######################################################################## 100.0%
libidn-0.6.8.tar.gz
######################################################################## 100.0%
imap-2004g.tar.Z
######################################################################## 100.0%
Done.

--- Unpacking downloaded archives. This process may take several minutes! ---

Extracting php-5.2.3
Done.

Extracting libiconv-1.11
Done.

Extracting libmcrypt-2.5.7
Done.

Extracting libxml2-2.6.27
Done.

Extracting libxslt-1.1.18
Done.

Extracting mhash-0.9.7.1
Done.

Extracting zlib-1.2.3
Done.

Extracting curl-7.17.1
Done.

Extracting libidn-0.6.8
Done.

Extracting imap-2004g
Done.

Extracting freetype-2.3.5
Done.

--------------------------------------------------
-- Done downloading and unpacking prerequisites --
--------------------------------------------------

Tags

Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | htaccess.io | htaccess.guru

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain