如何在 Linux 上安装 AMQP PHP 扩展和 RabbitMQ 客户端
【摘要】 本教程介绍如何安装和配置 RabbitMQ 客户端库和 AMQP PHP 扩展。
要连接到 RabbitMQ 消息队列服务器,您可以使用各种编程语言编写客户端程序。目前您可以使用 C#、erlang、java、perl、PHP、python 和 ruby 编写客户端。
本教程介绍如何安装和配置 RabbitMQ 客户端库和 AMQP PHP 扩展。
一旦你安装了 AMQP PHP 扩展,你就可以使用 AMQP 编写 PHP 程序,它可以连接到RabbitMQ 服务器来操作消息。
第 1 部分:安装 RabbitMQ 客户端库
1.安装cmake
cmake 是 rabbitmq-c 库使用的开源构建系统。在安装 AMQP PHP 扩展之前,我们需要安装 rabbitmq-c 库。但是,要安装 rabbitmq-c 库,我们需要 cmake。
从cmake 下载页面,向下滚动到 Linux i386 部分并下载 cmake-2.8.10.2-Linux-i386.sh 文件。或者,使用 wget 将其下载到您的系统上。
cd /usr/src
wget http://www.cmake.org/files/v2.8/cmake-2.8.10.2-Linux-i386.sh
执行 cmake-2.8.10.2-Linux-i386.sh 文件,它会在你的当前目录下安装 cmake,如下图所示。
# /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
重命名 cmake 目录,并验证 cmake 是否安装正确。
# 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.下载RabbitMQ客户端
首先安装 AMQP PHP 扩展所需的 librabbitmq 库。从rabbitmq-c git repository下载 zip 文件。
cd /usr/src
unzip rabbitmq-c-master.zip
cd rabbitmq-c-master
3.配置RabbitMQ客户端
使用 cmake 配置 rabbitmq 客户端进行安装。确保将 PREFIX 指定为 /usr/local,将安装 rabbitmq 客户端。
# 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. 安装 RabbitMQ 客户端
配置完成后,使用cmake安装rabbitmq客户端,如下图。这将在 /usr/local 下安装 librabbitmq 库。部分输出如下所示。
# /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.验证RabbitMQ客户端
该库附带一个示例程序,您可以使用它来验证它是否按预期工作。
从终端执行以下操作。这将处于等待状态。
# cd /usr/src/rabbitmq-c-master/build
# ./examples/amqp_listen localhost 5672 amq.direct test
打开另一个 rterminal,然后执行以下操作。这将向队列发送“hello world”消息。
# cd /usr/src/rabbitmq-c-master/build
# ./examples/amqp_sendstring localhost 5672 amq.direct test "hello world"
现在回到第一个处于等待状态的终端,您现在将看到如下所示的“hello world”消息。
# ./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:
第 2 部分:安装 AMQP PHP 扩展
您可以使用 pecl 安装 amqp,也可以从源代码编译。
要使用 pecl 安装,只需执行以下操作:
# pecl search amqp
amqp 1.0.9/(1.0.9 stable) Communicate with any AMQP compliant server
# pecl install amqp
要从源代码安装(我更喜欢),请按照以下步骤操作。
6.下载AMQP PHP扩展
下载AMQP PHP 扩展的最新稳定版本。当前稳定版本为 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.配置AMQP
执行phpize并配置如下图。
# 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. 安装 AMQP
使用 make 和 make install 安装 AMQP,如下所示。显示部分输出。
# 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.修改php.ini,添加AMQP Extension
在您的系统上找到 php.ini 文件并将以下行添加到其中。
# vi /usr/local/lib/php.ini
extension=amqp.so
10. 验证 AMQP PHP 扩展
创建以下将显示 phpinfo 的测试页面,并将其放在 Apache 的 htdocs 下。
# vi /usr/local/apache2/htdocs/test.php
<?php
phpinfo();
?>
现在,如果您从浏览器调用 test.php,您将看到 AMQP PHP 扩展显示在页面上,如下所示。现在,您可以在 PHP 代码中编写 AMQP 调用来与 RabbitMQ 服务器通信。
【版权声明】本文为华为云社区用户翻译文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,
举报邮箱:cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)