Installing RabbitMQ from source on a Raspberry Pi

发布于:2025-03-03 ⋅ 阅读:(23) ⋅ 点赞:(0)

Installing RabbitMQ from source on a Raspberry Pi requires compiling Erlang and RabbitMQ manually. I’ll guide you through the steps and provide a PHP demo to test it.


Step 1: Update the System

Ensure your Raspberry Pi is up to date:

sudo apt update && sudo apt upgrade -y

Step 2: Install Dependencies

RabbitMQ requires Erlang, so we need to install necessary build tools:

sudo apt install -y build-essential wget curl git libssl-dev \
    libncurses5-dev libwxgtk3.0-gtk3-dev libgl1-mesa-dev libglu1-mesa-dev \
    libpng-dev libssh-dev unixodbc-dev xsltproc fop

Step 3: Install Erlang (from Source)

RabbitMQ depends on Erlang, so we need to install it first.

1. Download and Compile Erlang

cd ~
git clone https://github.com/erlang/otp.git
cd otp
git checkout OTP-26.0  # Use latest stable version
./configure --prefix=/usr/local --enable-hipe --enable-smp-support
make -j$(nproc)
sudo make install

2. Verify Erlang Installation

erl -version

It should display something like: Erlang/OTP 26 [erts-12.0]


Step 4: Install RabbitMQ (from Source)

Now, let’s build RabbitMQ itself.

1. Download and Build RabbitMQ

cd ~
git clone https://github.com/rabbitmq/rabbitmq-server.git
cd rabbitmq-server
make -j$(nproc) PLATFORM=linux
sudo make install

2. Start RabbitMQ Server

sudo rabbitmq-server -detached

To check if it’s running:

sudo rabbitmqctl status

Step 5: Enable RabbitMQ Management Plugin

For easy web-based management, enable the plugin:

sudo rabbitmq-plugins enable rabbitmq_management
sudo systemctl restart rabbitmq-server

Now, you can access the RabbitMQ web UI at:

http://<RaspberryPi_IP>:15672

Default login:

  • User: guest
  • Password: guest

Since you’ve installed PHP from source, let’s also compile the amqp.so extension from source.

Step 6: Install PHP AMQP Extension (From Source)

1. Install Required Dependencies

Since we’re compiling from source, we need to manually install librabbitmq first:

cd ~
git clone https://github.com/alanxz/rabbitmq-c.git
cd rabbitmq-c
mkdir build && cd build
cmake ..
cmake --build . --target install
sudo ldconfig

2. Compile PHP AMQP Extension

cd ~
git clone https://github.com/php-amqp/php-amqp.git
cd php-amqp
phpize
./configure --with-amqp
make -j$(nproc)
sudo make install

3. Enable the Extension in php.ini

Add the following line:

echo "extension=amqp.so" | sudo tee -a /usr/local/lib/php.ini

or manually edit /usr/local/lib/php.ini and add:

extension=amqp.so

4. Verify the Installation

Run:

php -m | grep amqp

If everything is set up correctly, it should output:

amqp

Step 7: PHP Demo (Send and Receive Messages)

1. Create a Producer (send.php)

<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

require 'vendor/autoload.php';

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

$msg = new AMQPMessage('Hello, Raspberry Pi!');
$channel->basic_publish($msg, '', 'hello');

echo " [x] Sent 'Hello, Raspberry Pi!'\n";

$channel->close();
$connection->close();
?>

2. Create a Consumer (receive.php)

<?php
use PhpAmqpLib\Connection\AMQPStreamConnection;

require 'vendor/autoload.php';

$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
$channel = $connection->channel();

$channel->queue_declare('hello', false, false, false, false);

echo " [*] Waiting for messages. To exit press CTRL+C\n";

$callback = function ($msg) {
    echo " [x] Received ", $msg->body, "\n";
};

$channel->basic_consume('hello', '', false, true, false, false, $callback);

while ($channel->is_consuming()) {
    $channel->wait();
}
?>

3. Install PHP Dependencies

composer require php-amqplib/php-amqplib

Step 8: Test the PHP RabbitMQ Connection

Open two terminal windows:

1. Start the Consumer

php receive.php

2. Send a Message

php send.php

If everything is set up correctly, you should see:

[x] Sent 'Hello, Raspberry Pi!'
[x] Received 'Hello, Raspberry Pi!'