测试环境:centos7.6,rabbitmq3.7.12,php7.2.6

安装rabbitmqd参考:centos安装rabbitmq

要在php中使用rabbitmq需要安装两个扩展:rabbitmq-camqp,这里使用的两个扩展都为已发布的最新版。

安装rabbitmq-c

扩展下载地址:https://github.com/alanxz/rabbitmq-c/releases

  1. # 安装步骤的详细信息可以参考github地址
  2. wget https://github.com/alanxz/rabbitmq-c/archive/v0.9.0-master.tar.gz
  3. tar -zxvf v0.9.0-master.tar.gz
  4. cd rabbitmq-c-0.9.0-master
  5. mkdir build && cd build
  6. cmake -DCMAKE_INSTALL_PREFIX=/usr/local/rabbitmq-c ..
  7. cmake --build . --target install

安装amqp

扩展下载地址:http://pecl.php.net/package/amqp

  1. wget http://pecl.php.net/get/amqp-1.9.4.tgz
  2. tar -zxvf amqp-1.9.4.tgz
  3. cd amqp-1.9.4
  4. /usr/local/php/bin/phpize
  5. ./configure --with-php-config=/usr/local/php/bin/php-config --with-amqp --with-librabbitmq-dir=/usr/local/rabbitmq-c
  6. make && make install

这里在安装的时候报了一个错

  1. [root@localhost amqp-1.9.4]# make && make install
  2. /bin/sh /home/amqp-1.9.4/libtool --mode=link cc -DPHP_ATOM_INC -I/home/amqp-1.9.4/include -I/home/amqp-1.9.4/main -I/home/amqp-1.9.4 -I/usr/local/php/include/php -I/usr/local/php/include/php/main -I/usr/local/php/include/php/TSRM -I/usr/local/php/include/php/Zend -I/usr/local/php/include/php/ext -I/usr/local/php/include/php/ext/date/lib -I/usr/local/rabbitmq-c/include -DHAVE_CONFIG_H -g -O2 -o amqp.la -export-dynamic -avoid-version -prefer-pic -module -rpath /home/amqp-1.9.4/modules amqp.lo amqp_type.lo amqp_exchange.lo amqp_queue.lo amqp_connection.lo amqp_connection_resource.lo amqp_channel.lo amqp_envelope.lo amqp_basic_properties.lo amqp_methods_handling.lo amqp_timestamp.lo amqp_decimal.lo -Wl,-rpath,/usr/local/rabbitmq-c/lib -L/usr/local/rabbitmq-c/lib -lrabbitmq
  3. cc -shared .libs/amqp.o .libs/amqp_type.o .libs/amqp_exchange.o .libs/amqp_queue.o .libs/amqp_connection.o .libs/amqp_connection_resource.o .libs/amqp_channel.o .libs/amqp_envelope.o .libs/amqp_basic_properties.o .libs/amqp_methods_handling.o .libs/amqp_timestamp.o .libs/amqp_decimal.o -L/usr/local/rabbitmq-c/lib -lrabbitmq -Wl,-rpath -Wl,/usr/local/rabbitmq-c/lib -Wl,-soname -Wl,amqp.so -o .libs/amqp.so
  4. /usr/bin/ld: cannot find -lrabbitmq
  5. collect2: error: ld returned 1 exit status
  6. make: *** [amqp.la] 错误 1

发现/usr/local/rabbitmq-c目录下没有lib文件夹,有个lib64,然后把拷贝了一份lib目录,问题解决了。

  1. cp -r /usr/local/rabbitmq-c/lib64 /usr/local/rabbitmq-c/lib

添加amqp扩展
  1. vi /usr/local/php/etc/php.ini
  2. extension=amqp.so

重启php-fpm

  1. /etc/init.d/php-fpm restart

查看amqp扩展是否安装成功

  1. [root@localhost ~]# php -m | grep amqp
  2. amqp

测试php访问amqp

admin用户是我在上一节安装介绍添加的,当然也可以直接使用guest进行连接。

  1. <?php
  2. $config = [
  3. 'host' => '127.0.0.1',
  4. 'vhost' => '/',
  5. 'port' => 5672,
  6. 'login' => 'admin',
  7. 'password' => '123456'
  8. ];
  9. $con = new \AMQPConnection();
  10. if($con->connect()){
  11. echo '连接成功';
  12. }

然后访问连接成功

  1. [root@localhost amqp]# php demo.php
  2. 连接成功

在实际项目中可以在https://packagist.org 查找amqp的库,这里推荐使用 php-amqplib,可通过如下方法加载

  1. composer require php-amqplib/php-amqplib