DevelopmentLinux

Gearman 0.28 Setup on CentOS 6 (2012 Guide, Archived)

Isometric brushed-aluminum and cobalt-blue glass task dispatcher hub with brass planetary gears routing copper and frosted-glass job cubes along conveyor rails

Archived guide

I wrote this in 2012 and keep it for reference. CentOS 6 reached end of life on November 30, 2020, and the repositories it relies on no longer serve it. On a current system, install Gearman from your distribution's own packages instead.

Looking to simplify your server-side queue processing? Gearman is a versatile tool that enables distributed task handling across multiple languages. This guide walks you through setting up Gearman 0.28 on CentOS 6, configuring PHP workers, and testing the system for efficient background job processing.


๐Ÿ“ฆ What is Gearman and Why Use It?

Gearman is a distributed job server that allows applications to offload tasks to other machines or processes. Itโ€™s lightweight, language-agnostic, and supports both clustering and failover. Imagine youโ€™re running a high-traffic web app and need to handle email notifications, image processing, or data crunching asynchronously. Gearman steps in to manage those jobs smoothly without blocking your main application.

In our setup, weโ€™ll use PHP for both the client and worker to demonstrate Gearmanโ€™s flexibility. The result? A scalable system where background tasks can be handled efficientlyโ€”freeing up your web server for more requests.


๐Ÿ› ๏ธ Prerequisites

  • A fresh CentOS 6 installation
  • Enabled repositories: EPEL, Remi, and RPMForge
  • Root access to your server

First, set up the required repositories:

rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm \
http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm \
http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

โš™๏ธ Installing Required Packages

๐Ÿ“Œ PHP and Dependencies

yum install -y php php-cli php-common php-devel php-gd php-mbstring \
php-mysql php-pdo php-pear php-soap php-xml php-mcrypt pcre-devel libssh2-devel

๐Ÿ“Œ Development Tools for Gearman

yum install kernel-devel libevent-devel wget gcc gcc-c++ make autoconf automake boost boost-devel libuuid libuuid-devel

๐Ÿ“Œ Update System and Reboot

yum update -y
reboot

Note

Rebooting ensures all libraries and kernel modules load properly for the next steps.


๐Ÿš€ Installing Gearman and Gearmand

  1. Download Gearman source code:

    wget https://launchpad.net/gearmand/trunk/0.29/+download/gearmand-0.29.tar.gz
    
  2. Extract and build:

    tar zxvf gearmand-0.29.tar.gz
    cd gearmand-0.29
    ./configure
    make
    make install
    

You now have both the server (gearmand) and CLI tools installed.


๐Ÿ“ก Running Gearman

Start the server:

gearmand &

Start a worker:

gearman -w -f test_fn wc

Run a client task:

gearman -f test_fn "Hello World"

The worker reverses the string, and Gearman routes the result back to the client.


๐Ÿ”Œ Installing Gearman PHP Extension

pecl install gearman

Add to your php.ini:

extension=gearman.so

Verify installation:

php -m | grep gearman

The result? A scalable system where background tasks can be handled efficientlyโ€”freeing up your web server for more requests.

๐Ÿ“œ Example PHP Code

๐Ÿ‘จโ€๐Ÿ’ป Worker (worker.php)

<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction("reverse", function($job) {
    return strrev($job->workload());
});
while ($worker->work());
?>

๐Ÿ‘จโ€๐Ÿ’ป Client (client.php)

<?php
$client = new GearmanClient();
$client->addServer();
echo $client->do("reverse", "Hello Gearman!");
?>

Start your worker, run the client, and enjoy distributed task processing!


๐ŸŽฏ Final Thoughts

Gearman is perfect for delegating tasks like sending emails, resizing images, or crunching data asynchronously. By setting it up on CentOS 6, youโ€™re creating a scalable infrastructure thatโ€™s language-agnostic and highly efficient.


J

J. Walker

Technologist, developer, and author sharing insights on open source tooling, automation, and modern systems architecture.

Next Article → Pulling Twitter Feeds With a Twitter App (2013, Archived)