Install NATS as a service in Linux (Mint, Ubuntu, Debian)

NATS is a high-performance, lightweight, open-source messaging system designed for building modern distributed systems, across any cloud or any arbitrary, user-defined edge. It’s very small and fast, so it passt better to small projects then RabbitMQ, Kafka or Azure Service Bus. Below you will find a step by step how to install NATS a a service in Linux Machine.

1. Download and install Nats

curl -sf https://binaries.nats.dev/nats-io/nats-server/v2@latest | sh
sudo mv nats-server /usr/local/bin/

2. Create serice Unit for Nats

sudo nano /etc/systemd/system/nats.service

and paste


[Unit]
Description=NATS Server
After=network.target

[Service]
Type=simple
User=alexbbell
ExecStart=/usr/local/bin/nats-server -m 8222
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

3. Reload the daemon

sudo systemctl daemon-reload

4. Enable Nats service and start it

sudo systemctl enable nats.service
sudo systemctl start nats.service

Add authentication to requests to NATS

sudo mkdir -p /etc/nats
sudo nano /etc/nats/nats-server.conf

port: 4222
http_port: 8222

authorization {
    users = [
        {
            user: "publisher_app"
            password: "StrongPublisherPassword123"
            permissions: {
                pub: "orders.queue"
                sub: ""
            }
        },
        {
            user: "consumer_app"
            password: "StrongConsumerPassword456"
            permissions: {
                pub: ""
                sub: "orders.queue"
            }
        }
    ]
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Let us know you are human: