Tutorials / Discord

How to Host a Discord Bot on a VPS

beginner40 minLast reviewed: 2026-04-30

A Discord bot is a good fit for a small VPS because it usually needs persistent uptime more than large resources. A Starter plan is plenty for one or several lightweight bots. The operational risks are token handling, process restarts, dependency updates, and logs. If you paste the token into shell history or commit it to Git, the server size will not matter.

This guide uses Node.js through nvm, PM2 for process supervision, and a systemd EnvironmentFile for secrets. PM2 is familiar in the Node ecosystem and works well for bots that should restart after crashes or deploys. For larger fleets, you may prefer containers, but that is unnecessary for a simple bot.

Illustration for How to Host a Discord Bot on a VPS

Prerequisites

  • A uNode VPS deployed with Ubuntu 24.04 LTS.
  • A Discord application and bot token from the Discord Developer Portal.
  • A Node.js bot project stored in Git.
  • Root SSH access.
Open console server deploy

Step 1

Install base packages and a deploy user

Update Ubuntu, install Git and build tools, then create a non-root user for the bot. Running application code as root is unnecessary and makes mistakes more expensive.

ssh root@<your-server-ip>
apt update && apt upgrade -y
apt install -y git curl build-essential logrotate
adduser bot
usermod -aG sudo bot

Step 2

Install Node with nvm

Install nvm as the bot user so Node versions stay isolated from the OS package manager. Use the current LTS unless your bot requires a specific version.

su - bot
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
node --version
npm --version

Step 3

Deploy the bot code

Clone your repository and install dependencies. This example assumes a Node bot with npm scripts. If your bot is TypeScript, build it before starting PM2 or point PM2 at the compiled output.

git clone https://github.com/you/your-discord-bot.git ~/bot
cd ~/bot
npm ci
npm run build --if-present

Step 4

Store secrets outside Git

Do not put the Discord token in the repository or process command line. Store it in an environment file readable by the bot user. You can use the same pattern for database URLs, API keys, and guild IDs.

exit
mkdir -p /etc/unode-bots
cat >/etc/unode-bots/discord-bot.env <<'EOF'
DISCORD_TOKEN=replace-with-your-token
NODE_ENV=production
EOF
chown root:bot /etc/unode-bots/discord-bot.env
chmod 640 /etc/unode-bots/discord-bot.env

Step 5

Run the bot with PM2

PM2 manages restarts and logs. Load the environment file before starting. If your entry point is different, adjust npm start or ecosystem.config.js accordingly.

su - bot
. ~/.nvm/nvm.sh
cd ~/bot
set -a
. /etc/unode-bots/discord-bot.env
set +a
npm install -g pm2
pm2 start npm --name discord-bot -- start
pm2 save
pm2 startup systemd -u bot --hp /home/bot

Step 6

Add log rotation

Bots can log aggressively when Discord reconnects or a command loops. PM2 has a logrotate module that keeps logs bounded. Also send structured application logs to stdout so PM2 captures them consistently.

pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 14
pm2 logs discord-bot

Step 7

Deploy updates safely

Pull code, install dependencies, rebuild if needed, and restart. If your bot serves many communities, test updates in a staging guild first. Discord tokens should be rotated if they ever appear in logs, screenshots, or Git history.

su - bot
cd ~/bot
git pull
npm ci
npm run build --if-present
pm2 restart discord-bot