ubuntu12.04 安装Gearman及其php扩展
2013-12-05 11:12
387 查看
最近公司打算使用到Gearman开源软件,因此花了一天上午安装。折腾了好久,在这里把自己在网上搜索的一些相关资料做一个小小的总结。
安装Gearman
========================================================
向一个机器添加Gearman需要两步:
1.构建并启动这个守护进程
2.构建与php(或python等)版本相匹配的PHP扩展。
我安装版本是Gearman 1.1.5:
1. 安装依赖包:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gcc autoconf bison flex libtool make libboost-all-dev libcurl4-openssl-dev curl libevent-dev memcached uuid-dev libsqlite3-dev
libmysqlclient-dev
2.下载Gearman版本
wget https://launchpad.net/gearmand/1.2/1.1.5/+download/gearmand-1.1.5.tar.gz
3.解压、编译、安装源码包
tar xvzf gearmand-1.1.5.tar.gz
cd gearmand-1.1.5
./configure
make
make install
注:这个过程中可能无法编译成功,这时候要根据个人实际情况,看它报的是什么错,然后根据报错,更新或者安装共享库。
sudo apt-get install ***等。
4.为大多数最新的共享库创建必须的链接和缓存。其中可能报:
error: gearman: error while loading shared libraries: libgearman.so.6: cannot open shared object file: No such file or directory
解决办法:sudo ldconfig
5.通过pecl安装gearman
sudo apt-get install php-pear
sudo pecl install gearman
sudo gedit /etc/php5/cgi/php.ini
注:有些版本的php.ini可能不在/etc/php5/cgi/这个路径下,我的是在/etc/php5/cli/这个路径下。
另外,可能你的机器上没安装php5这个工具,如果没安装的话,可以直接通过在先安装:sudo apt-get install php5
如果上面的方法无法安装gearman的php扩展,可以尝试下下面方法:
$ wget http://pecl.php.net/get/gearman-1.1.1.tgz
$ tar zxvf gearman-1.0.2.tgz
$ cd gearman-1.0.2/
$ phpize
$ make
$ make install
$ sudo echo "extension = gearman.so" > /etc/php5/conf.d/gearman.ini
6. 在php.ini文件末尾添加"extension=gearman.so"
7. 检测扩展是否安装成功
$ php --info | grep "gearman support"
gearman support => enabled
显示出:gearman support => enabled,就表示安装成功啦。
8 测试
1)sudo ldconfig
2)启动gearmand: gearmand -d &
这一步可能会遇到:
启动这个 agent,即 Gearman 守护程序:
/usr/local/sbin/gearmand --daemon
报错:Could not open log file "/usr/local/var/log/gearmand.log", from "/usr/sbin", switching to stderr. (No such file or directory)
解决:
mkdir -p /usr/local/var/log/
cd /usr/local/var/log/
touch gearmand.log
再次尝试启动:
/usr/local/sbin/gearmand --daemon
成功运行.查看进程:ps -ef | grep gearmand
root 19390 1 0 17:50 ? 00:00:00 gearmand --daemon
root 19403 1 0 17:54 ? 00:00:00 /usr/local/sbin/gearmand --daemon
root 19406 1556 0 17:54 pts/3 00:00:00 grep gearmand
3)查看gearmand是否在运行:ps auxw | grep [g]earmand
4)检查germand的任务检测端口4730:sudo lsof -i tcp:4730
9.例子:从PHP使用Gearman
=====================================================
从 PHP 使用 Gearman 类似于之前的示例,惟一的区别在于这里是在 PHP 内创建 producer 和 consumer。
每个 consumer 的工作均封装在一个或多个 PHP 函数内。
先用 PHP 编写的一个 Gearman worker。将这些代码保存在一个名为 worker.php 的文件中。
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("title", "title_function");
while ($worker->work());
function title_function($job)
{
return ucwords(strtolower($job->workload()));
}
?>
再用 PHP 编写的一个 producer,或 client。将此代码保存在一个名为 client.php 的文件内。
<?php
$client= new GearmanClient();
$client->addServer();
print $client->do("title", "All The World's a stage!");
print "\n";
?>
现在,可以用如下的命令行连接客户机与 worker 了:
php worker.php &
php client.php
结果:
All The World's a stage!
10. 对上面的例子中代码的一些分析:
首先, PHP Gearman Extension 提供了一个名为 GearmanClient 的类别,它可以让程式安排工作给 Job Server 。而 addServer 方法表示要通知的是哪些 Job Server ,也就是说如果有多台 Job Server 的话,就可以透过 addServer 新增。然后我们将要呼叫哪个
Worker 以及该 Worker 所需要的资料,利用 GearmanClient 的 doBackground 方法传送过去。 doBackground 方法顾名思义就是在背景执行, Client 在丢出需求后就可以继续处理其他的程式,也就是我们常说的「射后不理」。
doBackground 方法的第一个参数是告诉 Job Server 要执行哪个功能,而这个功能则是由 Worker 提供的;要注意是,这个参数只是识别用的,并不是真正的函式名称。而第二个参数是要传给 Worker 的资料,它必须是个字串;因此如果要传送的是阵列的话,我们就要用 PHP 的 serialize 函式来对这些资料做序列化。
PHP 的 Gearman Extension 也提供了一个 GearmanWorker 类别,让我们可以实作 Worker 。而 GearmanWorker 类别也提供了addServer 方法,让所生成的 Worker 物件可以注册到 Job Server 中。
另外 GearmanWorker 类别也提供了 addFuncton 方法,告诉 Job Server 自己可以处理哪些工作。 addFunction 的第一个参数就是对应到 GearmanClient::doBackground 方法的第一个参数,也就是功能名称;这使得 Client 和 Worker 能透过这个名称来互相沟通。而第二个参数则是一个callback函式,它会指向真正应该要处理该工作的函式或类别方法等。
最后因为 Worker 因为要随时准备服务,是不能被中断的,因此我们透过一个无限迴圈来让它常驻在 Job Server 中。
安装Gearman
========================================================
向一个机器添加Gearman需要两步:
1.构建并启动这个守护进程
2.构建与php(或python等)版本相匹配的PHP扩展。
我安装版本是Gearman 1.1.5:
1. 安装依赖包:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install gcc autoconf bison flex libtool make libboost-all-dev libcurl4-openssl-dev curl libevent-dev memcached uuid-dev libsqlite3-dev
libmysqlclient-dev
2.下载Gearman版本
wget https://launchpad.net/gearmand/1.2/1.1.5/+download/gearmand-1.1.5.tar.gz
3.解压、编译、安装源码包
tar xvzf gearmand-1.1.5.tar.gz
cd gearmand-1.1.5
./configure
make
make install
注:这个过程中可能无法编译成功,这时候要根据个人实际情况,看它报的是什么错,然后根据报错,更新或者安装共享库。
sudo apt-get install ***等。
4.为大多数最新的共享库创建必须的链接和缓存。其中可能报:
error: gearman: error while loading shared libraries: libgearman.so.6: cannot open shared object file: No such file or directory
解决办法:sudo ldconfig
5.通过pecl安装gearman
sudo apt-get install php-pear
sudo pecl install gearman
sudo gedit /etc/php5/cgi/php.ini
注:有些版本的php.ini可能不在/etc/php5/cgi/这个路径下,我的是在/etc/php5/cli/这个路径下。
另外,可能你的机器上没安装php5这个工具,如果没安装的话,可以直接通过在先安装:sudo apt-get install php5
如果上面的方法无法安装gearman的php扩展,可以尝试下下面方法:
$ wget http://pecl.php.net/get/gearman-1.1.1.tgz
$ tar zxvf gearman-1.0.2.tgz
$ cd gearman-1.0.2/
$ phpize
$ make
$ make install
$ sudo echo "extension = gearman.so" > /etc/php5/conf.d/gearman.ini
6. 在php.ini文件末尾添加"extension=gearman.so"
7. 检测扩展是否安装成功
$ php --info | grep "gearman support"
gearman support => enabled
显示出:gearman support => enabled,就表示安装成功啦。
8 测试
1)sudo ldconfig
2)启动gearmand: gearmand -d &
这一步可能会遇到:
启动这个 agent,即 Gearman 守护程序:
/usr/local/sbin/gearmand --daemon
报错:Could not open log file "/usr/local/var/log/gearmand.log", from "/usr/sbin", switching to stderr. (No such file or directory)
解决:
mkdir -p /usr/local/var/log/
cd /usr/local/var/log/
touch gearmand.log
再次尝试启动:
/usr/local/sbin/gearmand --daemon
成功运行.查看进程:ps -ef | grep gearmand
root 19390 1 0 17:50 ? 00:00:00 gearmand --daemon
root 19403 1 0 17:54 ? 00:00:00 /usr/local/sbin/gearmand --daemon
root 19406 1556 0 17:54 pts/3 00:00:00 grep gearmand
3)查看gearmand是否在运行:ps auxw | grep [g]earmand
4)检查germand的任务检测端口4730:sudo lsof -i tcp:4730
9.例子:从PHP使用Gearman
=====================================================
从 PHP 使用 Gearman 类似于之前的示例,惟一的区别在于这里是在 PHP 内创建 producer 和 consumer。
每个 consumer 的工作均封装在一个或多个 PHP 函数内。
先用 PHP 编写的一个 Gearman worker。将这些代码保存在一个名为 worker.php 的文件中。
<?php
$worker= new GearmanWorker();
$worker->addServer();
$worker->addFunction("title", "title_function");
while ($worker->work());
function title_function($job)
{
return ucwords(strtolower($job->workload()));
}
?>
再用 PHP 编写的一个 producer,或 client。将此代码保存在一个名为 client.php 的文件内。
<?php
$client= new GearmanClient();
$client->addServer();
print $client->do("title", "All The World's a stage!");
print "\n";
?>
现在,可以用如下的命令行连接客户机与 worker 了:
php worker.php &
php client.php
结果:
All The World's a stage!
10. 对上面的例子中代码的一些分析:
首先, PHP Gearman Extension 提供了一个名为 GearmanClient 的类别,它可以让程式安排工作给 Job Server 。而 addServer 方法表示要通知的是哪些 Job Server ,也就是说如果有多台 Job Server 的话,就可以透过 addServer 新增。然后我们将要呼叫哪个
Worker 以及该 Worker 所需要的资料,利用 GearmanClient 的 doBackground 方法传送过去。 doBackground 方法顾名思义就是在背景执行, Client 在丢出需求后就可以继续处理其他的程式,也就是我们常说的「射后不理」。
doBackground 方法的第一个参数是告诉 Job Server 要执行哪个功能,而这个功能则是由 Worker 提供的;要注意是,这个参数只是识别用的,并不是真正的函式名称。而第二个参数是要传给 Worker 的资料,它必须是个字串;因此如果要传送的是阵列的话,我们就要用 PHP 的 serialize 函式来对这些资料做序列化。
PHP 的 Gearman Extension 也提供了一个 GearmanWorker 类别,让我们可以实作 Worker 。而 GearmanWorker 类别也提供了addServer 方法,让所生成的 Worker 物件可以注册到 Job Server 中。
另外 GearmanWorker 类别也提供了 addFuncton 方法,告诉 Job Server 自己可以处理哪些工作。 addFunction 的第一个参数就是对应到 GearmanClient::doBackground 方法的第一个参数,也就是功能名称;这使得 Client 和 Worker 能透过这个名称来互相沟通。而第二个参数则是一个callback函式,它会指向真正应该要处理该工作的函式或类别方法等。
最后因为 Worker 因为要随时准备服务,是不能被中断的,因此我们透过一个无限迴圈来让它常驻在 Job Server 中。
相关文章推荐
- 安装Gearman及其PHP扩展
- Linux 上安装Gearman及其PHP扩展
- CentOS 安装Gearman及其PHP扩展
- Centos安装Gearman及其php扩展
- bdb及其在php下扩展的安装
- Ubuntu12.04下PHP安装扩展库(无需再次编译安装PHP)
- CentOSx64 安装 Gearmand 和 Gearman php扩展
- ubuntu下安装gearman及php,python扩展
- memcached服务及其相关库以及php的memcached扩展的安装总结
- linux下 安装 ImageMagick 及其 php imagick扩展
- CentOS 5.8 x64 安装 Gearmand 和 Gearman php扩展
- CentOS 下安装 zeromq 及其php扩展安装
- 【转】ubuntu12.04上安装imagemagick的PHP扩展
- mongoDB2.6.7及其php扩展安装
- linux下 安装 ImageMagick 及其 php imagick扩展
- gearman-安装php扩展
- php安装gearman扩展实现异步分步式任务
- php安装gearman扩展实现异步分步式任务
- Centos下 Gearman安装与php扩展安装
- 在Mac OS 上安装 Gearman及PHP的扩展