在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