I have recently been installing ImageMagick on my server for use on my other site Celeb O Rama, but because I am on Dreamhost I can’t just install what I like, like you would on a server that you own. So by using the Dreamhost Wiki I installed ImageMagick, but there are no instructions on how to install Imagick. The following is how to install them both on Dreamhost.

First to install ImageMagick:

wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
tar zxvf ImageMagick.tar.gz
cd ImageMagick-<version>
./configure --prefix=$HOME/local -with-gslib --with-gs-font-dir=/usr/share/fonts/type1/gsfonts/ --without-perl
make
make install

Usually you would use --with-perl-options but for some reason versions of ImageMagick after 6.4.5 don’t seem to install perlmagick correctly on Dreamhost’s servers. You don’t really need it when using ImageMagick with PHP anyway.

Onto Installing Custom PHP5

Ok, this is the tricky bit. To install Imagick you need to have a custom PHP installation. If you don’t you won’t be able to install Imagick. The main reason that I come across for this (since you could just make a copy of the PHP.ini instead) is that Dreamhost’s version of php-config, which Imagick uses to find PHP’s version, still says PHP 4 instead of 5.2.5. This means Imagick won’t install since it requires 5.1.2 and up, I think. I know it has to be higher than PHP 5.

So here is how to do it. This is also available from the Dreamhost wiki. Copy the following script which was written by Skwerl. Credits are at the top of the shell script. Oh, and don’t forget to change the domain to the domain you want to use the custom PHP install with:

#!/bin/sh

# Script updated 2008-09-23 by Skwerl (antiquiet.com) to work with dreamhost's debian etch
# Script updated 2007-11-24 by Andrew (ajmconsulting.net) to allow 3rd wget line to pass
# Script updated 2006-12-25 by Carl McDade (hiveminds.co.uk) to allow memory limit and freetype
#
# LIBMCRYPT version information (was set as static download file name previously.)
#
# Save the code to a file as *.sh
# Abort on any errors
#
set -e

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

# Where do you want all this stuff built? I'd recommend picking a local
# filesystem.
# ***Don't pick a directory that already exists!***  We clean up after
# ourselves at the end!
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

# Pre-download clean up!!!!
rm -rf $SRCDIR $DISTDIR

# Update version information here.
PHP5="php-5.2.5"
LIBICONV="libiconv-1.11"
LIBMCRYPT="libmcrypt-2.5.7"
LIBXML2="libxml2-2.6.32"
LIBXSLT="libxslt-1.1.24"
MHASH="mhash-0.9.7.1"
ZLIB="zlib-1.2.3"
CURL="curl-7.14.0"
LIBIDN="libidn-0.6.8"
CCLIENT="imap-2004g"
CCLIENT_DIR="imap-2004g" # Another pest!
FREETYPE="freetype-2.2.1"
OPENSSL="openssl-0.9.8i"

# What PHP features do you want enabled?
PHPFEATURES="--prefix=${INSTALLDIR} \
 --with-config-file-path=${INSTALLDIR}/etc/php5/${DOMAIN} \
 --enable-fastcgi \
 --enable-force-cgi-redirect \
 --with-xml \
 --with-libxml-dir=${INSTALLDIR} \
 --with-freetype-dir=${INSTALLDIR} \
 --enable-soap \
 --with-openssl=${INSTALLDIR} \
 --with-mhash=${INSTALLDIR} \
 --with-mcrypt=${INSTALLDIR} \
 --with-zlib-dir=${INSTALLDIR} \
 --with-jpeg-dir=/usr \
 --with-png-dir=/usr \
 --with-gd \
 --enable-gd-native-ttf \
 --enable-memory-limit \
 --enable-ftp \
 --enable-exif \
 --enable-sockets \
 --enable-wddx \
 --with-iconv=${INSTALLDIR} \
 --enable-sqlite-utf8 \
 --enable-calendar \
 --with-curl=${INSTALLDIR} \
 --enable-mbstring \
 --enable-mbregex \
 --enable-bcmath \
 --with-mysql=/usr \
 --with-mysqli \
 --without-pear \
 --with-gettext \
 --with-imap=${INSTALLDIR} \
 --without-imap-ssl"

# ---- end of user-editable bits. Hopefully! ----

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

# set up directories
mkdir -p ${SRCDIR}
mkdir -p ${INSTALLDIR}
mkdir -p ${DISTDIR}
cd ${DISTDIR}

# Get all the required packages
wget -c http://us.php.net/get/${PHP5}.tar.gz/from/this/mirror
wget -c http://mirrors.usc.edu/pub/gnu/libiconv/${LIBICONV}.tar.gz
wget -c http://easynews.dl.sourceforge.net/sourceforge/mcrypt/${LIBMCRYPT}.tar.gz
wget -c ftp://xmlsoft.org/libxml2/${LIBXML2}.tar.gz
wget -c ftp://xmlsoft.org/libxml2/${LIBXSLT}.tar.gz
wget -c http://superb-west.dl.sourceforge.net/sourceforge/mhash/${MHASH}.tar.gz
wget -c http://www.zlib.net/${ZLIB}.tar.gz
wget -c http://curl.haxx.se/download/${CURL}.tar.gz
wget -c http://kent.dl.sourceforge.net/sourceforge/freetype/${FREETYPE}.tar.gz
wget -c ftp://alpha.gnu.org/pub/gnu/libidn/${LIBIDN}.tar.gz
wget -c ftp://ftp.cac.washington.edu/imap/old/${CCLIENT}.tar.Z
wget -c http://www.openssl.org/source/${OPENSSL}.tar.gz

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

cd ${SRCDIR}
# Unpack them all
echo Extracting ${PHP5}...
tar xzf ${DISTDIR}/${PHP5}.tar.gz
echo Done.
echo Extracting ${LIBICONV}...
tar xzf ${DISTDIR}/${LIBICONV}.tar.gz
echo Done.
echo Extracting ${LIBMCRYPT}...
tar xzf ${DISTDIR}/${LIBMCRYPT}.tar.gz
echo Done.
echo Extracting ${LIBXML2}...
tar xzf ${DISTDIR}/${LIBXML2}.tar.gz
echo Done.
echo Extracting ${LIBXSLT}...
tar xzf ${DISTDIR}/${LIBXSLT}.tar.gz
echo Done.
echo Extracting ${MHASH}...
tar xzf ${DISTDIR}/${MHASH}.tar.gz
echo Done.
echo Extracting ${ZLIB}...
tar xzf ${DISTDIR}/${ZLIB}.tar.gz
echo Done.
echo Extracting ${CURL}...
tar xzf ${DISTDIR}/${CURL}.tar.gz
echo Done.
echo Extracting ${LIBIDN}...
tar xzf ${DISTDIR}/${LIBIDN}.tar.gz
echo Done.
echo Extracting ${CCLIENT}...
uncompress -cd ${DISTDIR}/${CCLIENT}.tar.Z |tar x
echo Done.
echo Extracting ${FREETYPE}...
tar xzf ${DISTDIR}/${FREETYPE}.tar.gz
echo Done.
echo Extracting ${OPENSSL}...
tar xzf ${DISTDIR}/${OPENSSL}.tar.gz
echo Done.

# Build them in the required order to satisfy dependencies.

#libiconv
cd ${SRCDIR}/${LIBICONV}
./configure --enable-extra-encodings --prefix=${INSTALLDIR}
# make clean
make
make install

#libxml2
cd ${SRCDIR}/${LIBXML2}
./configure --with-iconv=${INSTALLDIR} --prefix=${INSTALLDIR}
# make clean
make
make install

#libxslt
cd ${SRCDIR}/${LIBXSLT}
./configure --prefix=${INSTALLDIR} \
 --with-libxml-prefix=${INSTALLDIR} \
 --with-libxml-include-prefix=${INSTALLDIR}/include/ \
 --with-libxml-libs-prefix=${INSTALLDIR}/lib/
# make clean
make
make install

#zlib
cd ${SRCDIR}/${ZLIB}
./configure --shared --prefix=${INSTALLDIR}
# make clean
make
make install

#libmcrypt
cd ${SRCDIR}/${LIBMCRYPT}
./configure --disable-posix-threads --prefix=${INSTALLDIR}
# make clean
make
make install

#libmcrypt lltdl issue!!
cd  ${SRCDIR}/${LIBMCRYPT}/libltdl
./configure --prefix=${INSTALLDIR} --enable-ltdl-install
# make clean
make
make install

#mhash
cd ${SRCDIR}/${MHASH}
./configure --prefix=${INSTALLDIR}
# make clean
make
make install

#freetype
cd ${SRCDIR}/${FREETYPE}
./configure --prefix=${INSTALLDIR}
# make clean
make
make install

#libidn
cd ${SRCDIR}/${LIBIDN}
./configure --with-iconv-prefix=${INSTALLDIR} --prefix=${INSTALLDIR}
# make clean
make
make install

#cURL
cd ${SRCDIR}/${CURL}
./configure --with-ssl=${INSTALLDIR} --with-zlib=${INSTALLDIR} \
  --with-libidn=${INSTALLDIR} --enable-ipv6 --enable-cookies \
  --enable-crypto-auth --prefix=${INSTALLDIR}
# make clean
make
make install

# c-client
cd ${SRCDIR}/${CCLIENT_DIR}
make -i ldb SSLTYPE=none
# Install targets are for wusses!
cp c-client/c-client.a ${INSTALLDIR}/lib/libc-client.a
cp c-client/*.h ${INSTALLDIR}/include

#OpenSSL
cd ${SRCDIR}/${OPENSSL}
./config --prefix=${INSTALLDIR} --openssldir=${INSTALLDIR}
make
make install

echo - about to build php5...
echo
read -p  "(Press any key to continue)" temp;
echo

#PHP 5
cd ${SRCDIR}/${PHP5}
./configure ${PHPFEATURES}
# make clean
make
make install

#copy config file
mkdir -p ${INSTALLDIR}/etc/php5/${DOMAIN}
cp ${SRCDIR}/${PHP5}/php.ini-dist ${INSTALLDIR}/etc/php5/${DOMAIN}/php.ini

#copy PHP CGI
mkdir -p ${HOME}/${DOMAIN}/cgi-bin
chmod 0755 ${HOME}/${DOMAIN}/cgi-bin
cp ${INSTALLDIR}/bin/php ${HOME}/${DOMAIN}/cgi-bin/php.cgi
rm -rf $SRCDIR $DISTDIR
echo ---------- INSTALL COMPLETE! ----------

Copy that into clipboard open an SSH connection & do vi php5-skwerl.sh and paste in the code (press ‘i’ then right click to paste). Then save the file by pressing ESC then :wq (that’s ‘Colon’, ‘w’ then ‘q’) then press enter. Use chmod +x php5-skwerl.sh to make the script executable. Finally run the script using ./php5-skwerl.sh. DO NOT copy the code into notepad & save it then upload it, it’s alot easier to use vi.

During the installation you may get a little message warning about unencrpted passwords, I can’t remember exactly but as long as you don’t want to use openSSL you can either say yes or no. Since I have no use for SSL or openSSL I just said no.

Once the code has finished, it took around 30-40 minutes for me, then you will need to tell your server to use your PHP installation. To do this add this to your .htaccess file.

Options +ExecCGI
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php.cgi

<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>

Then give a PHP page a refresh & see what happens, chances are you’ll get a 500 or Internal Server Error. If you do, don’t panic, just run this from the SSH command line (bash).

cp ~/php5/bin/php-cgi ~/DOMAIN.TLD/cgi-bin/php.cgi

Change DOMAIN.TLD to the domain you chose to install the custom PHP installation too. All should be fine after that.

Installing Imagick

Now for the bit which isn’t covered in the Dreamhost wiki. Installing Imagick. It took a little bit of time but I managed to get it installed. You first need to download, compile & install autoconf & then Imagick.

mkdir ~/source
cd ~/source
wget ftp://ftp.gnu.org/gnu/autoconf/autoconf-2.63.tar.gz
tar xzf autoconf-2.63.tar.gz
cd autoconf-2.63
./configure --prefix=$HOME/source
nice -n 19 make
make install
export PATH=/home/{username}/source/bin:$PATH
cd ~/
wget http://pecl.php.net/get/imagick-2.2.2.tgz
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2
phpize
./configure -prefix=$HOME/imagick --with-imagick=$HOME/local --with-php-config=$HOME/php5/bin/php-config
make
make install

Don’t forget to change {username} on line 9 to your username or you’ll get a error.

Phew! After that it should tell you the path to imagick.so which you need to instuct your PHP.ini to find. So let’s do that. First either write down the path given or copy it to clipboard. In Putty you do that just by highlighting the text in the window.

cd ~/
vi ~/php5/etc/php5/DOMAIN.TLD/php.ini

scroll all the way down until you find extension_dir='./' and change it to the FULL path to the folder given by the build of imagick before. If you have it in clipboard press i then delete ./ and right click. Now find the list of extensions which look like ‘;extension=thing_some.dll’ and after the last one add ‘extension=imagick.so’. Now press ESC then colon ‘w’ ‘q’ and enter to quit and save.

Once you’ve done all that, make a php file with phpinfo() in it and go down and see if you can find this

Imagick Awaiting Your Command.

Imagick Awaiting Your Command.

And Relax

Yup, that’s it. It takes about 1 hour maybe 1 hour 30. It’s worth it if you don’t like to use exec() in your scripts, also Imagick supports PHP’s try, catch exception system so you can easily output nice errors instead of guessing what’s happened when exec fails. For a full list of imagick, imagickDraw, imagickPixel and all the other imagick commands look at php.net.

Hope that helps someone, the instructions can be applied to any server really, but you will need to change certain options since these ones are specifically for Dreamhost since write access to /usr/ is denied for obvious reasons. Any problems or questions feel free to ask.

The shell script & instructions for installing php5 & the info for installing imagemagick have been used from the Dreamhost wiki since they are correct & no modifications need to be made for them to work. Credit for those parts go to the authors from the Wiki. The imagick installation was written by me since no documentation for installing imagick exists in the Dreamhost wiki.