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.
Comments on this entry are closed.
Dear Ramesh Natarajan Sir,
Very Very good information,
I love ALL the info that you share with us, Thank you!
That’s great! Thanks so much for this.
For others getting
Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIR)
Make sure you have libssl-dev package installed (Ubuntu).
Hey,
Awesome guide!,
Im having issues with my install when I go to run the examples for 5. Verify RabbitMQ Client
(Running on a fresh Ubuntu Client 12.04 LTS VM)
#./examples/amqp_listen localhost 5672 amq.direct test
opening TCP socket
# <– auto returns to command prompt, does not enter waiting state. Not sure what I have done wrong, I installed everything but skipped SSL support (tried it last time when installing on Ubuntu Server last night and had the same issue using a different guide.) using -DENABLE_SSL_SUPPORT=NO
Any ideas?
Great guide Ramesh, it’s pretty much the only complete one I could find!
@Steve: I’ve ran into the same issue. In my case the RabbitMQ server wasn’t running on the same host, so localhost wouldn’t work. If you don’t have RabbitMQ server running on localhost, be aware that the guest account (that is used by the examples) by default only allows connections from localhost on the server. You need to create a new user or alter the guest account for the test to work.
Hi, thanks for the info…however, when I run the:
/usr/src/cmake/bin/cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
line, I get:
/usr/src/cmake/bin/cmake: 1: /usr/src/cmake/bin/cmake: Syntax error: “(” unexpected
…any idea what I can do about this?
Hi Ramesh,
Thanks for the guide.
After adding the extension=amqp.so into php.ini file, i am getting the following error whenever i run any php file.
—
PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib64/php/modules/amqp.so’ – /usr/lib64/php/modules/amqp.so: undefined symbol: amqp_get_heartbeat in Unknown on line 0
—
I tried installing aqmp in both ways (pecl and source).
Please help me.
I ma using PHP 5.6.30 on CentOS release 6.7 .