在Linux上安装Gearman及配置使用Gearman的PHP扩展环境

来源:互联网 发布:2083欧洲独立宣言 知乎 编辑:程序博客网 时间:2024/05/20 02:29
在Linux上安装Gearman及配置使用Gearman的PHP扩展环境。


先介绍安装 Gearman 。

1. 先安装依赖库
# yum install -y boost-devel gperf libevent-devel libuuid-devel

2. 下载 Gearman
下载:
# wget https://launchpad.net/gearmand/1.2/1.1.12/+download/gearmand-1.1.12.tar.gz
解压:
# tar -xzvf gearmand-1.1.12.tar.gz 

3. 配置 Gearman
# cd gearmand-1.1.12
# pwd
/data/gearmand-1.1.12
# ./configure
...
checking for boostlib >= 1.39... configure: We could not detect the boost libraries (version 1.39 or higher). If you have a staged boost library (still not installed) please specify $BOOST_ROOT in your environment and do not give a PATH to --with-boost option.  If you are sure you have boost installed, then check your version number looking in <boost/version.hpp>. See http://randspringer.de/boost for more documentation.
configure: error: could not find boost

如果出现上面错误,先检查当前安装的Boost版本是否 >= 1.39,如果版本低,就需要安装高版本的Boost。
如果已经安装了Boost,就先卸载再安装;如果版本 >= 1.39,也出现上面错误,也卸载再安装。
安装Boost:
# yum install boost  
# yum install boost-devel  
# yum install boost-doc

再执行配置:
# ./configure

4. 编译 Gearman
# pwd
/data/gearmand-1.1.12
# make
...
  CXX      util/gearmand_hostile_gearmand-daemon.o
  CXX      util/gearmand_hostile_gearmand-pidfile.o
  CXX      libtest/gearmand_hostile_gearmand-cpu.o
  CXXLD    gearmand/hostile_gearmand
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld returned 1 exit status
make[1]: *** [gearmand/hostile_gearmand] Error 1
make[1]: Leaving directory `/data/backup/gearmand-1.1.12'
make: *** [all] Error 2

如果出现上面错误,就先查找 mysqlclient 库文件所在目录:
# find / -name *mysqlclient*
/usr/lib64/mysql/libmysqlclient.a
/usr/lib64/mysql/libmysqlclient_r.a

如上找到mysqlclient 库文件,就在/usr/lib/目录下建两个软链接:
# ln -s /usr/lib64/mysql/libmysqlclient.a /usr/lib/libmysqlclient.a 
# ln -s /usr/lib64/mysql/libmysqlclient_r.a /usr/lib/libmysqlclient_r.a 

如果没找到 mysqlclient 库文件,就需要安装 MySQL Client 了。

再执行编译:
# make

5. 安装 Gearman
# pwd
/data/gearmand-1.1.12
# make install

随意进入其他目录执行 gearman,检查安装是否成功:
# gearman

gearman Error in usage(No Functions were provided).
Client mode: gearman [options] [<data>]
Worker mode: gearman -w [options] [<command> [<args> ...]]
Common options to both client and worker modes.
        -f <function> - Function name to use for jobs (can give many)
        -h <host>     - Job server host
        -H            - Print this help menu
        -v            - Print diagnostic information to stdout(false)
        -p <port>     - Job server port
        -t <timeout>  - Timeout in milliseconds
        -i <pidfile>  - Create a pidfile for the process
        -S            - Enable SSL connections
Client options:
        -b            - Run jobs in the background(false)
        -I            - Run jobs as high priority
        -L            - Run jobs as low priority
        -n            - Run one job per line(false)
        -N            - Same as -n, but strip off the newline(false)
        -P            - Prefix all output lines with functions names
        -s            - Send job without reading from standard input
        -u <unique>   - Unique key to use for job
Worker options:
        -c <count>    - Number of jobs for worker to run before exiting
        -n            - Send data packet for each line(false)
        -N            - Same as -n, but strip off the newline(false)
        -w            - Run in worker mode(false)

出现上面显示,说明安装 Gearman 成功了。

启动与停止 Gearman 服务:
1. 启动 Gearman
# gearmand -d --log-file=/data/gearman/logs/gearmand.log 
log-file 参数指定日志文件。
2. 停止 Gearman
# gearadmin --shutdown


下面讲配置使用 Gearman 的 PHP 扩展环境。

1. 先安装PHP拓展环境
下载 gearman-1.1.2.tgz(注意,这个 gearman-1.1.2.tgz 与前面的 gearmand-1.1.12.tar.gz 不同):
# wget http://pecl.php.net/get/gearman-1.1.2.tgz
(下载列表地址:http://pecl.php.net/package/gearman)

# tar -zxvf gearman-1.1.2.tgz 

配置:
# cd gearman-1.1.2
# phpize

如果出现“phpize: command not found”,就安装PHP :
# yum install php php-devel
然后再执行:
# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

# ./configure

编译安装:
# make
# make install
Installing shared extensions:     /usr/lib64/php/modules/
出现如上提示,说明安装成功, 在 /usr/lib64/php/modules/ 目录下有 gearman.so 文件。

2. 在php.ini文件中加入Gearman配置
在配置前,先编写个php程序测试下,程序很简单,就是打印 Gearman 版本。
# cd /data/test
# vi test.php
<?php
print gearman_version() . "\n";
?>

然后执行程序看什么结果:
# php test.php 
PHP Fatal error:  Call to undefined function gearman_version() in /data/test/test.php on line 2
调用了未定义的 gearman_version 函数。

下面加上配置后再运行程序看看。
先找到php.ini文件:
# find / -name php.ini
/etc/php.ini
# cd /etc
为把配置加入适当的位置,先查找文件中 extension 单词出现的位置:
# more php.ini | grep extension
; Directives are variables used to configure PHP or PHP extensions.
; dynamically loaded extension (either a PHP extension or a Zend extension),
; you may only use these constants *after* the line that loads the extension.
; leading '/'. You must also specify the file extension being used including
; Directory in which the loadable extensions (modules) reside.
; http://www.php.net/manual/en/ini.core.php#ini.extension-dir
; extension_dir = "./"
; If you wish to have an extension loaded automatically, use the following
;   extension=modulename.extension
;   extension=msql.so
;   extension=/path/to/extension/msql.so
; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.
; Note: packaged extension modules are now loaded via the .ini files
;sqlite3.extension_dir =
; Sets the directory name where SOAP extension will put cache files.


加入配置:
# vi php.ini
extension=gearman.so
位置:


再执行程序:
# php test.php 
1.1.12

到此,使用 Gearman 的 PHP 扩展环境配置完成。


参考:

http://blog.csdn.net/clevercode/article/details/45718735

https://my.oschina.net/liucao/blog/755299


原创粉丝点击