How to Install AMQP PHP Extension and RabbitMQ Client on Linux

转自http://www.thegeekstuff.com/2013/05/amqp-php-extension/


To connect to RabbitMQ message queue server, you can write your client program using various programming languages. Currently you can write client using C#, erlang, java, perl, PHP, python, and ruby.

This tutorial explains how to install and configure RabbitMQ client library and AMQP PHP extension.

Once you get AMQP PHP extension installed, you can write PHP program using AMQP, which can connect to RabbitMQ server to manipulate the messages.

Part 1: Install RabbitMQ Client Library

1. Install cmake

cmake is a open source build system that is used by rabbitmq-c library. We need to install rabbitmq-c library before we install AMQP PHP extension. But, to install rabbitmq-c library, we need cmake.

From the cmake download page, scoll down to Linux i386 section and download the cmake-2.8.10.2-Linux-i386.sh file. Or, use the wget to download it on your system.

cd /usr/src
wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2-Linux-i386.sh

Execute the cmake-2.8.10.2-Linux-i386.sh file, which will install cmake under your current directory as shown below.

# /bin/sh cmake-2.8.10.2-Linux-i386.sh
Do you accept the license? [yN]: y
By default the CMake will be installed in:
  "/usr/src/cmake-2.8.10.2-Linux-i386"
Do you want to include the subdirectory cmake-2.8.10.2-Linux-i386?
Saying no will install in: "/usr/src" [Yn]: y
Using target directory: /usr/src/cmake-2.8.10.2-Linux-i386
Extracting, please wait...
tar: Read 4096 bytes from -
Unpacking finished successfully

Rename the cmake directory, and verify that cmake is installed properly.

# cd /usr/src
# mv cmake-2.8.10.2-Linux-i386 cmake
# /usr/save/cmake/bin/cmake --version
cmake version 2.8.10.2

2. Download RabbitMQ Client

First install the librabbitmq library, which is required by the AMQP PHP extension.

Download the zip file from rabbitmq-c git repository.

cd /usr/src
unzip rabbitmq-c-master.zip
cd rabbitmq-c-master

3. Configure RabbitMQ Client

Use cmake to configure the rabbitmq client for install. Make sure to specify the PREFIX as /usr/local, where the rabbitmq client will be installed.

# mkdir build
# cd build
# /usr/src/cmake/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
-- The C compiler identification is GNU 4.1.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- CMAKE_BUILD_TYPE not specified. Creating Release build
-- Found C inline keyword: inline
-- Looking for getaddrinfo
-- Looking for getaddrinfo - found
-- Looking for socket
-- Looking for socket - found
-- Looking for htonll
-- Looking for htonll - not found
-- Found POPT: /usr/include
-- Could NOT find XMLTO (missing:  XMLTO_EXECUTABLE)
-- Building rabbitmq as a shared library - yes
-- Building rabbitmq as a static library - no
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/save/rabbitmq-c-master/build

4. Install RabbitMQ Client

After configuring, use cmake to install rabbitmq client as shown below. This will install librabbitmq library under /usr/local. Partial output is shown below.

#  /usr/src/cmake/bin/cmake --build . --target install
Scanning dependencies of target rabbitmq
[  2%] Building C object librabbitmq/CMakeFiles/rabbitmq.dir/amqp_framing.c.o
[  4%] Building C object librabbitmq/CMakeFiles/rabbitmq.dir/amqp_api.c.o
..
[ 95%] Built target amqp-publish
[ 97%] Building C object tests/CMakeFiles/test_parse_url.dir/test_parse_url.c.o
[100%] Building C object tests/CMakeFiles/test_tables.dir/test_tables.c.o
[100%] Built target test_tables
Install the project...
-- Install configuration: "Release"
-- Installing: /usr/local/lib/pkgconfig/librabbitmq.pc
-- Installing: /usr/local/lib/librabbitmq.so.1.0.1
-- Installing: /usr/local/lib/librabbitmq.so.1
-- Installing: /usr/local/lib/librabbitmq.so
-- Installing: /usr/local/include/amqp.h
-- Installing: /usr/local/include/amqp_framing.h
-- Installing: /usr/local/bin/amqp-publish
-- Removed runtime path from "/usr/local/bin/amqp-publish"
-- Installing: /usr/local/bin/amqp-get
-- Removed runtime path from "/usr/local/bin/amqp-get"
-- Installing: /usr/local/bin/amqp-consume
-- Removed runtime path from "/usr/local/bin/amqp-consume"
-- Installing: /usr/local/bin/amqp-declare-queue
-- Removed runtime path from "/usr/local/bin/amqp-declare-queue"
-- Installing: /usr/local/bin/amqp-delete-queue
-- Removed runtime path from "/usr/local/bin/amqp-delete-queue"

5. Verify RabbitMQ Client

The library comes with an example program that you can use to verify that it works as expected.

From a terminal, do the following. This will be in a waiting state.

# cd /usr/src/rabbitmq-c-master/build
# ./examples/amqp_listen localhost 5672 amq.direct test

Open anothe rterminal, and do the following. This will send a “hello world” message to the queue.

# cd /usr/src/rabbitmq-c-master/build
# ./examples/amqp_sendstring localhost 5672 amq.direct test "hello world"

Now go back to the first terminal that was in waiting state, where you’ll now see the “hello world” message as shown below.

# ./examples/amqp_listen localhost 5672 amq.direct test
Result 0
Frame type 1, channel 1
Method AMQP_BASIC_DELIVER_METHOD
Delivery 1, exchange amq.direct routingkey test
Content-type: text/plain
----
00000000: 68 65 6C 6C 6F 20 77 6F : 72 6C 64                 hello world
0000000B:

Part 2: Install AMQP PHP Extension

You can install amqp using pecl, or you can compile from source.

To install using pecl, just do the following:

# pecl search amqp
amqp    1.0.9/(1.0.9 stable)       Communicate with any AMQP compliant server

# pecl install amqp

To install from source (which I prefer), follow the steps below.

6. Download AMQP PHP extension

Download the latest stable version of AMQP PHP extension. The current stable version is 1.0.10

cd /usr/src
wget http://pecl.php.net/get/amqp-1.0.10.tgz
tar xvfz amqp-1.0.10.tgz
cd amqp-1.0.9

7. Configure AMQP

Execute phpize and configure as shown below.

# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

# ./configure --with-amqp
checking for egrep... grep -E
checking for a sed that does not truncate output... /bin/sed
..
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h

8. Install AMQP

Install AMQP using make and make install as shown below. Partial output is shown.

# make
..
creating amqp.la
(cd .libs && rm -f amqp.la && ln -s ../amqp.la amqp.la)
/bin/sh /usr/save/amqp-1.0.9/libtool --mode=install
cp ./amqp.la /usr/save/amqp-1.0.9/modules
cp ./.libs/amqp.so /usr/save/amqp-1.0.9/modules/amqp.so
cp ./.libs/amqp.lai /usr/save/amqp-1.0.9/modules/amqp.la
PATH="$PATH:/sbin" ldconfig -n /usr/save/amqp-1.0.9/modules
----------------------------------------------------------------------
Libraries have been installed in:
   /usr/save/amqp-1.0.9/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
Build complete.
# make install
Installing shared extensions:
/usr/local/lib/php/extensions/no-debug-non-zts-20090626/
..
You should add "extension=amqp.so" to php.ini

9. Modify php.ini and add AMQP Extension

Locate the php.ini file on your system and add the following line to it.

# vi /usr/local/lib/php.ini
extension=amqp.so

10. Verify AMQP PHP Extension

Create the following test page that will display phpinfo, and place this under your Apache’s htdocs.

# vi /usr/local/apache2/htdocs/test.php
<?php
phpinfo();
?>

Now, if you call test.php from your browser, you’ll see that AMQP PHP extension is displayed on the page as shown below. Now, you can write AMQP calls in your PHP code to talk to RabbitMQ server.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值