Skip to main content

NATS as a Service

Introduction​

NATS is required for running an Autonomys Farming Cluster. NATS can be run as a service on Ubuntu, or in a container on Docker. This guide will cover how to set up NATS as a service.

Download and Install​

The first step is to download the binary from the NATS GitHub. Navigate to the NATS Server releases page and look for the release with the "Latest" tag. At the time of this guide the latest release is nats-server-v2.10.22-linux-amd64.tar.gz and it will be used as an example in this wiki. Note that you may need to click "Show Assets" to find the correct binary.

Download​

Use wget to download the tar.gz file

wget -P ~/ https://github.com/nats-io/nats-server/releases/download/v2.10.22/nats-server-v2.10.22-linux-amd64.tar.gz

Extract​

tar -xvf nats-server-v2.10.22-linux-amd64.tar.gz

Move​

sudo mv ~/nats-server-v2.10.22-linux-amd64/nats-server /usr/local/bin/

Clean up​

rm -rf ~/nats-server-v2.10.22-linux-amd64*

Create Config​

sudo mkdir -p /etc/nats/

Open Config​

sudo nano /etc/nats/config.conf

Update Config​

Paste in the following snippet, and then press ctrl + x, then y, then enter to save.

max_payload = 2MB

Create NATs User​

Create User​

sudo useradd -r -s /bin/false -c "NATS service account" -d /nonexistent nats

Set Permissions​

sudo chown -R nats:nats /usr/local/bin/nats-server

Create Service​

Create Service​

Create the service file

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

Paste in the service code

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

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

KillSignal=SIGUSR2

[Install]
WantedBy=multi-user.target

Start Service​

Reload daemon​

sudo systemctl daemon-reload

Start​

sudo systemctl start nats

Verify​

sudo systemctl status nats

Enable​

sudo systemctl enable nats

NATS is now running and can accept connections on port 4222