How to Install and Use RabbitMQ with Docker and PHP: Step‑by‑Step Guide

This guide walks you through installing RabbitMQ via Docker, configuring ports and users, exploring its messaging model, and integrating it with PHP using the php‑amqplib client, complete with runnable code examples for publishing and consuming messages.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Install and Use RabbitMQ with Docker and PHP: Step‑by‑Step Guide

RabbitMQ Overview

RabbitMQ is a widely used open‑source message broker that is lightweight, easy to deploy, supports multiple messaging protocols, and can run in distributed or federated configurations for high scalability and availability.

Installation and Basic Configuration

Search for the Docker image: docker search rabbitmq Pull the management‑enabled tag: docker pull rabbitmq:3.7.16-management (the tag 3.7.16-management includes a web UI).

Run the container, exposing the management UI (port 15672) and the AMQP port (5672), and set default credentials:

docker run -p 15672:15672 -p 5672:5672 -d \
  --hostname dnmp-rabbitmq \
  --name dnmp-rabbitmq --network dnmp_backend \
  -e RABBITMQ_DEFAULT_USER=admin \
  -e RABBITMQ_DEFAULT_PASS=admin \
  rabbitmq:3.7.16-management

Parameter meanings: 15672: port for the RabbitMQ management console. 5672: TCP port used by applications to connect to RabbitMQ. RABBITMQ_DEFAULT_USER and RABBITMQ_DEFAULT_PASS: credentials for the console (set to admin).

Verify the installation by opening http://127.0.0.1:15672/ in a browser and logging in with admin / admin. Then create a new user (e.g., resty) with administrator role, a virtual host named resty, and assign appropriate permissions.

RabbitMQ Messaging Model

The core components are:

Producer – sends messages to an exchange.

Consumer – receives messages from a queue.

Exchange – routes messages to queues based on routing rules (e.g., direct).

Queue – stores messages until a consumer processes them.

PHP Client Library (php‑amqplib)

Using ThinkPHP 5.1 as the test environment, install the library inside a Docker container:

docker run --rm -i -t \
  -v e:/dnmp/www/iot.tinywan.com:/app \
  composer require php-amqplib/php-amqplib v2.9.0 --ignore-platform-reqs

Message Publisher (mq_send.php)

#!/usr/bin/env php
<?php
namespace think;
define('APP_PATH', __DIR__.'/application/');
require __DIR__.'/thinkphp/base.php';
Container::get('app',[APP_PATH])->bind('http/RabbitMq/send')->run()->send();

public function send(){
    $connection = new AMQPStreamConnection('dnmp-rabbitmq', 5672, 'admin', 'admin');
    $channel = $connection->channel();
    $channel->queue_declare('hello', false, false, false, false);
    $msg = new AMQPMessage('Hello World!');
    $channel->basic_publish($msg, '', 'hello');
    echo "[x] Sent 'Hello World!'
";
    $channel->close();
    $connection->close();
}

Message Consumer (mq_receive.php)

#!/usr/bin/env php
<?php
namespace think;
define('APP_PATH', __DIR__.'/application/');
require __DIR__.'/thinkphp/base.php';
Container::get('app',[APP_PATH])->bind('http/RabbitMq/receive')->run()->send();

public function receive(){
    $connection = new AMQPStreamConnection('dnmp-rabbitmq', 5672, 'admin', 'admin');
    $channel = $connection->channel();
    $channel->queue_declare('hello', false, false, false, false);
    echo "[*] Waiting for messages. To exit press CTRL+C
";
    $callback = function($msg){
        echo "[x] Received '" . $msg->body . "'
";
    };
    $channel->basic_consume('hello', '', false, true, false, false, $callback);
    while (count($channel->callbacks)) {
        $channel->wait();
    }
}

Run the consumer inside the PHP container:

docker exec -it dnmp-php72 sh -c "php /var/www/iot.tinywan.com/mq_receive.php"

Run the publisher to send a test message:

docker exec -it dnmp-php72 sh -c "php /var/www/iot.tinywan.com/mq_send.php"

The consumer will output the received Hello World! message, confirming that the RabbitMQ broker, Docker deployment, and PHP client are correctly integrated.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerMessage QueueRabbitMQPHPAMQP
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.