RabbitMQ is a widely-used, robust message broker — enabling applications to communicate asynchronously through queues, decoupling components and smoothing out traffic spikes.
Prerequisites
- Ubuntu 22.04/24.04 or Debian 11/12 VPS
- Root or sudo access
Step 1 — Add the RabbitMQ Repository
curl -1sLf https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/rabbitmq.gpg
echo "deb [signed-by=/usr/share/keyrings/rabbitmq.gpg] https://packagecloud.io/rabbitmq/rabbitmq-server/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/rabbitmq.list
Step 2 — Install RabbitMQ
sudo apt update
sudo apt install rabbitmq-server -y
Step 3 — Enable and Start RabbitMQ
sudo systemctl enable --now rabbitmq-server
Step 4 — Enable the Management Plugin (Web UI)
sudo rabbitmq-plugins enable rabbitmq_management
Step 5 — Create an Administrative User
sudo rabbitmqctl add_user admin CHANGE_ME_STRONG_PASSWORD
sudo rabbitmqctl set_user_tags admin administrator
sudo rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"
Step 6 — Remove the Default Guest User (Security)
sudo rabbitmqctl delete_user guest
The default guest account has a well-known default password and should never remain active on a production instance.
Step 7 — Allow the Required Ports
sudo ufw allow from YOUR_TRUSTED_IP to any port 15672
sudo ufw allow 5672/tcp
Port 5672 is the AMQP protocol port (application connections), 15672 is the management web UI — restrict the UI to trusted IPs.
Step 8 — Access the Management UI
http://YOUR_SERVER_IP:15672
Creating a Queue via the UI
Under Queues and Streams → Add a new queue, name it and configure durability settings.
Connecting from an Application (Node.js Example)
const amqp = require('amqplib');
const connection = await amqp.connect('amqp://admin:password@localhost:5672');
const channel = await connection.createChannel();
await channel.assertQueue('task_queue', { durable: true });
channel.sendToQueue('task_queue', Buffer.from('Hello World'), { persistent: true });
Consuming Messages
channel.consume('task_queue', (msg) => {
console.log('Received:', msg.content.toString());
channel.ack(msg);
}, { noAck: false });
Understanding Durability and Persistence
By default, queues and messages don't survive a broker restart unless explicitly marked durable/persistent — for production use, always set durable: true on queues and persistent: true on messages you can't afford to lose.
Setting Up High Availability (For Production)
RabbitMQ supports clustering and mirrored queues for redundancy — a more advanced setup appropriate once message loss risk from a single-node failure becomes unacceptable for your use case.
Monitoring Queue Health
See How to Monitor Message Queue Health and Performance for detailed monitoring guidance.
Common Errors
Connection refused — verify the firewall allows port 5672 from your application server's IP.
Messages lost after a restart — queues/messages weren't configured as durable/persistent; review the durability settings shown above.
Best Practices
- Remove the default guest account immediately
- Use durable queues and persistent messages for anything you can't afford to lose
- Restrict the management UI to trusted IPs
Related Articles
- Message Queue Basics: When and Why to Use One
- How to Monitor Message Queue Health and Performance
- How to Install NATS for Lightweight Messaging
