Sunday, July 18, 2010

PEAR and PECL in Dreamhost Shared

I decided to use MongoDB for one of my PHP projects, you can follow Phillip Napieralski instructions for a fast build and setup. But I wanted a little more, I wanted to do it directly from PECL, nothing beats seeing your packages listed when you type $pecl config-show.

For starters you need to connect to your server via SSH, I have both a Mac and a PC so I guess am I hybrid. In windows I recommend you download Cygwin, don't forget to select the ssh package. And get ready to start typing into a black console. I'll assume that you haven't install anything in your account.

Getting ready

In order to install pecl we need to have our own php install, if you want to build your own install you can follow DH wiki. For a quick and dirty solution we are just going make DH install our own.

$ mkdir ~/src
$ mkdir ~/packages
$ cp -r /usr/local/php5/ ~/packages/
$ nano ~/packages/php5/bin/php-config
# Replace the following lines

The first two lines create a couple of folders in our home, they just help to keep the home folder organized, if we start installing everything directly in our home folder, it will get saturated very fast. On line 3 we copy the server's PHP install into our packages folder and then we open php-config with a text editor. Then replace the paths of prefix and extension_dir with the ones pointing to our php5 install.

Now we need to install AutoConf, and we'll follow the traditional steps to download the source, configure it and install it.

$ cd ~/src
$ wget
$ tar xzf autoconf-2.66.tar.gz
$ cd autoconf-2.66
$ ./configure --prefix=$HOME/packages
$ make
$ make install

On line 1 we move to the src directory, which I like to use to download the source code and compile applications, we download the source code from the GNU server (line 2), extract it (3). To configure it we need to get into the source code folder, and the --prefix specifies our base directory for the installation. So the last three lines take care of configuring, building and installing (copying the files to their directories) the application. Sweet, we just install an application from the GNU Project, you can install most of them using the same steps.

We should add our application path and the php binaries to the PATH.

$ nano ~/.bash_profile
# Add the following line to the end of the file.
export PATH=$HOME/packages/bin:$PATH
$ . ~/.bash_profile
$ cd ~/packages/bin
$ ln -s ~/packages/php5/bin/php ./php
$ ln -s ~/packages/php5/bin/pecl ./pecl
$ ln -s ~/packages/php5/bin/pear ./pear
$ ln -s ~/packages/php5/bin/phpize ./phpize
$ ln -s ~/packages/php5/bin/php-config ./php-config

Edit the ~/.bash_profile file with any editor you want, and add the export... line at the end of the file, then save the changes and the last line will execute the file, making sure the ~/packages/bin path get added to the PATH. The .bash_profile, gets executed every time we login, so in order to avoid logging out and logging in to see the change in the PATH we just execute the file by adding a period '.' at the beginning of the instruction.
And finally we add the php binaries as symbolic links to the ~/packages/bin directory in our path. This means we add something like a shortcut or a ghost from the ~/packages/bin to the ~/packages/php5/bin

Installing PEAR

We already sort of have PEAR and PECL in our system, but we are just going to configure it.

$ wget -O ~/src/go-pear.php
$ php ~/src/go-pear.php

Remember the wget command? We are downloading a php script from the PHP site, and in the second line we execute it. You should start seeing a couple of question about the PEAR configuration, press enter to skip the HTTP proxy, then press 1 and enter to setup the Installation prefix ($prefix), and type /home/YOUR_USERNAME/packages  Replace YOUR_USERNAME with your ssh username, you can leave everything in the default and just press enter to continue.
You'll be asked if you want to installed a couple of bundle packages too, you can press 'n' right now, and install them later, no biggie.
It will ask you if you want to alter the php.ini, and the path points to DH php.ini, I recommend you choose yes, because, even if it generates an error it will output an example php.ini that will help us configure our php.ini later.

Because we already had a pear application it will rename it to pear_old (~/packages/php5/bin/pear_old), and we might have a problem executing it because now the important one is in a different directory (~/packages/bin/pear). So just logout and login again.

Testing the settings

Let's try it out by installing a Pear package and making sure everything is Groovy, you can check all available Pear Packages, but I normally use the XML_Serializer for my web services.

pear install -o XML_Serializer
# We get an error 'Failed to download pear/XML_Serializer within...
pear install -o XML_Serializer-beta

First time we failed to download the package, that's because in our preferred_state PEAR configuration we have that we should only download from stable releases, and the XML Serializer is still in beta (v0.20), so we can just force it to install it by appending -beta at the end of the package name, and so we acknowledge that we want the beta version.

Configuring PECL

Everything is ready now, feel free to install your pecl extension by using:

$ pecl install mongo
$ pecl install markdown-alpha

So far we have added PECL and PEAR to our account, but we still haven't linked them to our Domain. To start using PEAR and PECL we need to make changes to the php.ini, and in order to make changes to the php.ini we first need to tell the domain to use our custom php.ini.

You can follow DH wiki on how to make a domain use a custom php.ini, once you've done that, the changes you need to make to the php.ini are:


# And so the list grows on all the extension you've installed (PECL)

And for PEAR, just append your PEAR directory to the include_path:


And that's it, you have PEAR and PECL in your DreamHost account, and the good thing is that you can use the PECL and PEAR manager for it.