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
Rebooting ensures all libraries and kernel modules load properly for the next steps.
🚀 Installing Gearman and Gearmand
- Download Gearman source code:
wget https://launchpad.net/gearmand/trunk/0.29/+download/gearmand-0.29.tar.gz
- 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
📜 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.