How to Install RabbitMQ on Debian 12

Updated on November 21, 2023
How to Install RabbitMQ on Debian 12 header image

Introduction

RabbitMQ is a popular, lightweight, open-source message broker that implements the Advanced Message Queuing Protocol (AMQP). It enables applications to exchange messages asynchronously, decouple them from each other and improve scalability.

This article explains how to install RabbitMQ on Debian 12.

Prerequisites

Before you begin:

Install RabbitMQ

Erlang is a RabbitMQ dependency package required on a server. asdf is a CLI tool that can manage language runtime versions on a per-project basis and enables the smooth installation of Erlang. Follow the steps below to enable all dependency packages and install RabbitMQ on your server.

  1. Install all required asdf dependencies.

    console
    $ sudo apt install curl git -y
    
  2. Download asdf using the official project repository.

    console
    $ git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.0
    
  3. Open the .bashrc file in your environment.

    console
    $ nano ~/.bashrc
    
  4. Add the following directives at the end of the file.

    ini
    . "$HOME/.asdf/asdf.sh"
    
    . "$HOME/.asdf/completions/asdf.bash"
    

    Save and lose the file.

  5. Reload the .bashrc file in your session.

    console
    $ source ~/.bashrc
    
  6. View the asdf version on your server.

    console
    $ asdf --version
    

    Output:

    version: v0.14.0-ccdd47d
    
    ...
  7. Install the asdf-erlang plugin.

    console
    $ asdf plugin add erlang https://github.com/asdf-vm/asdf-erlang.git
    
  8. Install all necessary Erlang dependencies.

    console
    $ sudo apt-get -y install build-essential autoconf m4 libncurses-dev libwxgtk3.2-dev libwxgtk-webview3.2-dev libgl1-mesa-dev libglu1-mesa-dev libpng-dev libssh-dev unixodbc-dev xsltproc fop libxml2-utils openjdk-17-jdk
    
  9. Create a new environment variable to skip Java dependency warnings during installation.

    console
    $ export KERL_CONFIGURE_OPTIONS="--disable-debug --without-javac"
    
  10. Install the Erlang version 26.1.2

    console
    $ asdf install erlang 26.1.2
    
  11. Set the global Erlang version to use on the server.

    console
    $ asdf global erlang 26.1.2
    
  12. Access the Erlang shell to verify your installation.

    console
    $ erl
    

    Output:

    Erlang/OTP 26 [erts-14.1.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit:ns]
    
    Eshell V14.1.1 (press Ctrl+G to abort, type help(). for help)
    1> 

    Enter A and press Enter to exit the Erlang shell.

Install the RabbitMQ Package

  1. Install RabbitMQ.

    console
    $ sudo apt install rabbitmq-server -y --fix-missing
    

    The above command installs the latest version of RabbitMQ server package and fixes any broken dependencies on your server.

  2. View the RabbitMQ service status and verify that it's running.

    console
    $ sudo systemctl status rabbitmq-server
    

    Output:

    ● rabbitmq-server.service - RabbitMQ Messaging Server
         Loaded: loaded (/lib/systemd/system/rabbitmq-server.service; enabled; preset: enabled)
         Active: active (running) since Mon 2024-08-26 09:35:15 UTC; 25s ago
       Main PID: 50330 (beam.smp)
          Tasks: 22 (limit: 2299)
         Memory: 97.4M
            CPU: 3.131s
  3. Enable the RabbitMQ service to automatically start at system boot.

    console
    $ sudo systemctl enable rabbitmq-server
    
  4. Start the RabbitMQ service.

    console
    $ sudo systemctl start rabbitmq-server
    

Configure RabbitMQ

Follow the steps below to configure RabbitMQ on your server.

  1. List all RabbitMQ virtual hosts.

    console
    $ sudo rabbitmqctl list_vhosts
    
  2. Create a new RabbitMQ virtual host to use in your applications. Replace myhost with your desired virtual host.

    console
    $ sudo rabbitmqctl add_vhost myhost
    
  3. Create a new RabbitMQ user and assign the user a strong password.

    console
    $ sudo rabbitmqctl add_user example-user strong-password
    
  4. Grant the user administrative privileges to use RabbitMQ.

    console
    $ sudo rabbitmqctl set_user_tags example-user administrator
    
  5. Grant the user full permissions to all RabbitMQ virtual hosts, exchanges, queues, bindings, and routing keys.

    console
    $ sudo rabbitmqctl set_permissions -p myhost example-user ".*" ".*" ".*"
    

Secure RabbitMQ

Follow the steps below to disable the guest user and secure the RabbitMQ web management console with trusted SSL certificates on your server.

  1. Disable the RabbitMQ guest user profile.

    console
    $ sudo rabbitmqctl delete_user guest
    
  2. Open the main RabbitMQ configuration file to enable user authentication.

    console
    $ sudo nano /etc/rabbitmq/rabbitmq.conf
    
  3. Add the following directive at the end of the file.

    ini
    auth_backends.1 = internal
    

    Save and close the file.

  4. Restart RabbitMQ to apply changes.

    console
    $ sudo systemctl restart rabbitmq-server
    
  5. Install the Nginx web server to work as a reverse proxy with RabbitMQ.

    console
    $ sudo apt install nginx -y
    
  6. Create a new Nginx virtual host configuration for RabbitMQ.

    console
    $ sudo nano /etc/nginx/sites-available/rabbitmq.conf
    
  7. Add the following configurations to the file. Replace rabbitmq.example.com with your actual domain.

    nginx
    server {
           listen 80;
           listen [::]:80;
    
           server_name rabbitmq.example.com;
    
           location / {
           proxy_pass http://localhost:15672;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
      }
    
      location /mqtt {
          proxy_pass http://localhost:15675;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
    }
    

    Save and close the file.

    The above Nginx configuration listens for incoming network connections on the HTTP port 80 to serve the RabbitMQ console from port 15672 and the /mqtt path for web socket connections.

  8. Disable the default virtual host configuration.

    console
    $ sudo rm /etc/nginx/sites-enabled/default
    
  9. Create a new symbolic link to the /sites-enabled directory to enable the virtual host configuration.

    console
    $ sudo ln -s /etc/nginx/sites-available/rabbitmq.conf /etc/nginx/sites-enabled/
    
  10. Test the Nginx configuration for errors.

    console
    $ sudo nginx -t
    

    Output:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  11. Restart the Nginx service.

    console
    $ sudo systemctl restart nginx
    
  12. Install the Certbot Let's Encrypt client plugin for Nginx.

    console
    $ sudo apt install python3-certbot-nginx certbot -y
    
  13. Allow network connections on the HTTP port 80 through the firewall to enable Let's Encrypt validations.

    console
    $ sudo ufw allow 80/tcp
    
  14. Restart UFW to apply changes.

    console
    $ sudo ufw reload
    
  15. Generate a new SSL certificate to use with your Nginx virtual host and RabbitMQ domain rabbitmq.example.com. Replace hello@example.com with your actual email.

    console
    $ sudo certbot --nginx -d rabbitmq.example.com -m hello@example.com --agree-tos
    

    Your output should be similar to the one below when successful.

    Successfully received certificate.
    Certificate is saved at: /etc/letsencrypt/live/rabbitmq.example.com/fullchain.pem
    Key is saved at:         /etc/letsencrypt/live/rabbitmq.example.com/privkey.pem
    Deploying certificate
    Successfully deployed certificate for rabbitmq.example.com to /etc/nginx/sites-enabled/rabbitmq.conf
    Congratulations! You have successfully enabled HTTPS on https://rabbitmq.example.com
  16. Restart Nginx to apply the SSL configuration changes.

    console
    $ sudo systemctl restart nginx
    
  17. Allow HTTPS connections on port 443 through the firewall.

    console
    $ sudo ufw allow 443/tcp
    
  18. Restart UFW to apply changes.

    console
    $ sudo ufw reload
    

Access the RabbitMQ Console

You can access the RabbitMQ console to monitor and manage your server functionalities. Follow the steps below to enable the RabbitMQ management plugin, and access the web administration console to perform tasks such as queue monitoring.

  1. Enable the RabbitMQ management plugin.

    console
    $ sudo rabbitmq-plugins enable rabbitmq_management
    

    Output:

    The following plugins have been enabled:
      rabbitmq_management
      rabbitmq_management_agent
      rabbitmq_web_dispatch
    
    started 3 plugins.
  2. View all RabbitMQ plugins and verify that the management plugin is active.

    console
    $ sudo rabbitmq-plugins list | grep management
    

    Output:

    [  ] rabbitmq_federation_management    3.10.8
    [E*] rabbitmq_management               3.10.8
    [e*] rabbitmq_management_agent         3.10.8
    [  ] rabbitmq_shovel_management        3.10.8
    [  ] rabbitmq_stream_management        3.10.8

    Based on the above output, the web administration management plugin is enabled based on the [E] prompt.

  3. Access your RabbitMQ domain using a web browser such as Chrome to access the web administration console.

    https://rabbitmq.example.com

    Enter your administrative RabbitMQ user and password details you created earlier to log in to the console.

    RabbitMQ web interface

  4. Use the RabbitMQ management interface to view active nodes, users, virtual hosts, queues, exchanges, and perform management tasks on the server.

    RabbitMQ management interface

Conclusion

You have installed RabbitMQ on a Debian 12 server and secured access to the web administration console. RabbitMQ implements message-based architectures, microservices, and distributed systems you can use with existing applications on your server. For more information and configuration options, visit the RabbitMQ documentation.