NATS Installation

  1. Execute the below command to download the nats-server binary.

  1. Move the nats-server binary to /usr/local/bin.

sudo mv nats-server /usr/local/bin/

  1. Make the binary executable.

sudo chmod +x /usr/local/bin/nats-server

  1. Generate two pairs of NKeys using the nk tool. One pair is for sys user and other for normal user. For a given pair, the key starting with S is the seed key and the one starting with U is the public key. In the nats-server.conf below, you need to paste in the public key of sys user and normal user. Note down the seed key and public key, which will be used in the later parts of installation. Follow this link to install nk tool and generate nkeys https://docs.nats.io/using-nats/nats-tools/nk


  1. Create a nats-server.conf file in /etc directory and make necessary changes.

 Sample nats-server.conf

host: 0.0.0.0
port: 4222

jetstream: enabled

accounts: {
sysAcc: {
    users: [
        {nkey: "<SYS_NATS_PUBLIC_KEY>"}
    ],
    exports: [
            {stream: ira.sys.disconnect.>}
    ]
},
normal: {
    users: [
            {nkey: "<NORMAL_NATS_PUBLIC_KEY>"}
    ],
    imports: [
            {stream: {account: sysAcc, subject: ira.sys.disconnect.>}}
    ],
    jetstream: enabled
}
}

system_account: sysAcc

6. Create nats-server.service file in /etc/systemd/system and make necessary configuration changes.

Sample nats-server.service

[Unit]
Description=NATS Server
After=network-online.target ntp.service

[Service]
PrivateTmp=true
Type=simple
ExecStart=/usr/local/bin/nats-server -c /etc/nats-server.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s SIGINT $MAINPID

User=root
Group=root

Restart=always
RestartSec=5

# The nats-server uses SIGUSR2 to trigger using Lame Duck Mode (LDM) shutdown
KillSignal=SIGUSR2
# You might want to adjust TimeoutStopSec too.

LimitNOFILE=800000


[Install]
WantedBy=multi-user.target

7. Enable and start the service.

sudo systemctl enable nats-server.service
sudo systemctl daemon-reload
sudo systemctl start nats-server.service

8. Check the status of the service and verify if nats is shown as running.

sudo systemctl status nats-server.service