how to setup and run rabbitmq with php using docker

    By: Thad Mertz
    5 months ago

    In this guide i will guide you how you can setup rabbitmq for php application using docker. super easy an very informative.


    ok i assume you are familiar with docker-compose.


    So first here is the code.

    make sure you have docker installed in your computer.or Install it from here


    docker-compose.yml


    version: '3'
    services:
      php:
        build:
          context: .
          dockerfile: ./Dockerfile
        ports:
          - "99:80"
        volumes:
          - ./www:/var/www/html
          - ./logs/php:/var/log/php
        depends_on:
          - db
          - rabbitmq
        networks:
          - php
    
      db:
        image: arm64v8/mysql:8
        restart: always
        deploy:
          replicas: 1
        command: --default-authentication-plugin=mysql_native_password
        ports:
          - "3379:3306"
        volumes:
          - ./data/mysql_cnf/custom.cnf:/etc/mysql/conf.d/custom.cnf
          - ./data/mysql:/var/lib/mysql
          - ./logs/db:/var/log/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=awesomemanu
          - MYSQL_DATABASE=zarx
          - MYSQL_USER=manu
          - MYSQL_PASSWORD=awesomemanu
        networks:
          - php
    
      phpmyadmin:
        image: arm64v8/phpmyadmin:5.2.1
        restart: always
        deploy:
          replicas: 1
        depends_on:
          - db
        ports:
          - '8181:80'
        environment:
          PMA_HOST: db
          PMA_PORT: 3306
          MYSQL_USERNAME: root
          MYSQL_ROOT_PASSWORD: awesomemanu
          UPLOAD_LIMIT: 1000M
          PHP_INI_SCAN_DIR: /usr/local/etc/php/conf.d
        volumes:
          - ./config/php.ini:/usr/local/etc/php/conf.d/memory-limit.ini
          - ./logs/phpmyadmin:/var/log/phpmyadmin
        networks:
          - php
    
      rabbitmq:
        image: rabbitmq:3-management
        ports:
          - "5672:5672"
          - "15672:15672"
        environment:
          RABBITMQ_DEFAULT_USER: guest
          RABBITMQ_DEFAULT_PASS: guest
        volumes:
          - ./logs/rabbitmq:/var/log/rabbitmq
        networks:
          - php
    
    networks:
      php:
    

    Ok great.


    Now in this file we have 3 services.


    1. PHP APP.
    2. PHPMYADMIN and MYSQL
    3. RABBIT MQ


    RabbitMQ runs on separate server that is why we have dedicated docker container for it.

    also we will call producer script and receiver script using php for this example. What is producer and consumer script you say.

    Ok producer is sender of message and consumer is receiver of message. let's dive into the code


    // Index.php as producer
    
    <?php
    global $connection;
    require 'connection.php';
    
    try
    {
        $channel = new AMQPChannel($connection);
        $exchange = new AMQPExchange($channel);
        $exchange->setName('Test1');
        $exchange->setFlags(AMQP_DURABLE);
        $exchange->setType(AMQP_EX_TYPE_DIRECT); // Set the exchange type to 'direct'
    
        $message = 'Hello, RabbitMQ!';
        $exchange->publish($message, 'my_Routing_key');
    
    }
    catch (AMQPConnectionException|AMQPExchangeException|AMQPChannelException $e)
    {
        var_dump($e->getMessage());
    }
    
    


    Now if you wonder where is connection here is code for connection.php

    <?php
    
    
    $connection = new AMQPConnection([
        'host' => 'rabbitmq',
        'port' => 5672,
        'login' => 'guest',
        'password' => 'guest',
    ]);
    
    try
    {
        if($connection->connect())
        {
            echo "Connection established";
        }
    }
    catch (AMQPConnectionException $e)
    {
        var_dump($e->getMessage());
        exit;
    }
    


    Great let's talk about consumer.php

    <?php
    global $connection;
    require 'connection.php';
    
    $channel = new AMQPChannel($connection);
    $queue = new AMQPQueue($channel);
    $queue->setName('my_Custom_Queue');
    $queue->bind('Test1', 'my_Routing_key');
    
    while (true) {
        $message = $queue->get();
    
        if ($message) {
            echo "Received: " . $message->getBody() . "\n";
    
    
            // Acknowledge the message after processing
            $queue->ack($message->getDeliveryTag());
        }
    
    }
    


    Well that is it we are sending message and receiving message using rabbitMq. For this we need to run both php files. Check video guide if have any confusion.