Installing Gearman shared PECL extension for PHP on Debian/Ubuntu Linux

Please read how to install Gearman server here. Download the source code for gearman PHP extension here.

cd /tmp
wget http://pecl.php.net/get/gearman-0.7.0.tgz
tar xzf gearman-0.7.0.tgz
cd gearman-0.7.0

Before installing the Gearman PHP extension you need to install the autoconf package first. Here is how you do it on Debian Linux.

apt-get install autoconf

After installing autoconf, run the following commands to configure, compile, test, and install Gearman PHP extension on your Debian Linux machine.

# in the gearman PECL extension directory
phpize;
./configure --with-php-config=/opt/php/bin/php-config --with-gearman=/opt/gearman/;
make ;
make test;
make install;

The Gearman module on my machine was created in /opt/php/lib/php/extensions/no-debug-non-zts-20090626/. In order to use the gearman extension you have to load the module in PHP. Add the following line in your PHP configuration file (make sure you have Gearman module gearman.so in your extensions directory).

extension=gearman.so

In order to test the extension, follow the three steps below:

  1. Make sure Gearman server is up and running
  2. Write a Gearman worker in PHP and register with Gearman server
  3. Write a Gearman client in PHP and run it and make sure you get the desired output

Step 1:

ps aux | grep gearmand

If you can see the Gearman deamon running then it means Gearman job server is running.

Step 2:

Here is the content for the Gearman worker program (copy from Gearman's example on the website).

<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", "my_reverse_function");
while ($worker->work());

function my_reverse_function($job)
{
  return strrev($job->workload());
}
?>

Assuming that the filename is worker.php, do the following to register it with Gearman job server.

/opt/php/bin/php worker.php &
ps aux | grep worker

Step 3:

Here is the content for the Gearman client program (copy from Gearman's example on the website).

<?php
$client= new GearmanClient();
$client->addServer();
print $client->do("reverse", "Hello World!");
print "\n";
?>

Now, run the following command to test your installation program (assuming the client resides in client.php).

/opt/php/bin/php client.php

The output should look something like below:

!dlroW olleH

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I recommend thoroughy testing on a production-like test system first before moving to production.