nordelo
Menu

Guides

Run a Discord bot around the clock on a VPS

Move your Discord bot from your computer to a VPS so it stays online and restarts by itself. With Node.js and systemd.

A bot running on your own computer goes offline when the computer does. On a VPS it stays online day and night. This guide is for a bot written in Node.js, for example with discord.js, on Ubuntu 24.04 or Debian. A Cloud 2 is plenty for most bots.

1. Install Node.js and git

Connect to the server with SSH and run:

apt update && apt install -y nodejs npm git
node --version

If your bot needs a newer Node version than the one shown, install it from nodejs.org with nvm instead.

2. Add a user for the bot

The bot should not run as root. That way a bug in the bot cannot take over the whole server.

adduser --system --group --home /opt/bot bot

3. Get the code

If your code is on GitHub, clone it. Otherwise upload the folder with scp -r or an SFTP program like FileZilla.

git clone https://github.com/YOUR-NAME/your-bot.git /opt/bot/app
cd /opt/bot/app && npm install --omit=dev
chown -R bot:bot /opt/bot

4. Keep the token in its own file

Never put your Discord token in the code. Put it in a file only root can read.

echo 'DISCORD_TOKEN=your-token-here' > /etc/bot.env
chmod 600 /etc/bot.env

In your code, read it with process.env.DISCORD_TOKEN.

5. Let systemd run the bot

systemd starts the bot when the server boots and restarts it if it crashes. Replace index.js with your start file.

cat > /etc/systemd/system/bot.service <<EOF
[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target

[Service]
User=bot
WorkingDirectory=/opt/bot/app
EnvironmentFile=/etc/bot.env
ExecStart=/usr/bin/node index.js
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now bot

6. Check that it is alive

systemctl status bot
journalctl -u bot -f

The second command shows the bot's output live. Stop it with Ctrl+C.

Update the bot

cd /opt/bot/app && sudo -u bot git pull && sudo -u bot npm install --omit=dev
systemctl restart bot

See the VPS sizes