All posts

Blog

How to Host a Discord Bot 24/7 on a VPS (Node.js or Python)

Illustrated cover for the Developers guide "How to Host a Discord Bot 24/7 on a VPS (Node.js or Python)"

Most Discord bots start life on someone's laptop. That works for testing, but the bot goes offline whenever the computer sleeps, reboots or loses Wi-Fi. Free hosting tricks tend to come with sleep timers, restarts and shared IP addresses that can run into rate limits.

A small VPS solves this cleanly. Your bot runs on an always-on server, restarts itself if it crashes, and comes back automatically after a reboot. This guide shows you how to host a Discord bot 24/7 on a VPS, with examples for both discord.js (Node.js) and discord.py (Python).

How a Discord bot connects

It helps to understand one detail first. A typical Discord bot connects outward to Discord's gateway over a WebSocket and keeps that connection open. Discord pushes events (messages, slash commands, member joins) down that connection.

That means:

  • You don't need to open any inbound ports for a standard gateway bot. Your firewall can stay tight.
  • You don't need a domain name or SSL certificate unless your bot also runs a web dashboard or uses an HTTP interactions endpoint.
  • The bot needs to stay running. If the process stops, the bot goes offline.

How big a VPS do you need?

A small bot serving a handful of servers uses very little CPU and memory, so an entry-level VPS is usually enough. Bots that play music, process images, run a database or serve thousands of servers need more. Watch real usage after launch with the tools in our guide to monitoring server resources and upgrade only if needed.

Step 1: Prepare the server

Set up a fresh VPS following first steps on a new Linux VPS: updates, a sudo user, SSH keys and a firewall that allows SSH.

Create a dedicated, unprivileged user for the bot so a bug or compromised dependency can't touch the rest of the system:

sudo adduser --system --group --home /opt/discord-bot discordbot

Step 2: Install the runtime

For Node.js (discord.js): check the discord.js guide for the minimum Node.js version your discord.js release needs, then install a current LTS release of Node.js. Ubuntu's default nodejs package can be older than what discord.js requires, so many people use the NodeSource packages or a version manager. Verify with:

node --version

For Python (discord.py): check the discord.py documentation for the supported Python versions. Install Python and the venv module:

sudo apt install python3 python3-venv

Step 3: Copy your bot to the server

Use Git or SFTP to put your code in /opt/discord-bot/app. Our comparison of FTP vs SFTP vs SSH explains why SFTP is the safe choice for transfers.

sudo mkdir -p /opt/discord-bot/app
sudo chown -R discordbot:discordbot /opt/discord-bot

Then install dependencies as the bot user.

Node.js:

cd /opt/discord-bot/app
sudo -u discordbot npm ci --omit=dev

Python:

cd /opt/discord-bot/app
sudo -u discordbot python3 -m venv /opt/discord-bot/venv
sudo -u discordbot /opt/discord-bot/venv/bin/pip install -r requirements.txt

Step 4: Keep your bot token secret

Your bot token is effectively the password to your bot. Anyone who has it can control the bot in every server it has joined.

  • Never hard-code it in your source code.
  • Never commit it to Git. Add .env to .gitignore.
  • Store it in an environment file readable only by the bot user:
sudo -u discordbot nano /opt/discord-bot/.env
DISCORD_TOKEN=your-token-here
sudo chmod 600 /opt/discord-bot/.env

If the token ever leaks (in a public repo, a screenshot or a paste), go to the Discord Developer Portal and reset it immediately. Then update the .env file on the server.

Your code should read the token from the environment, for example process.env.DISCORD_TOKEN in Node.js or os.environ["DISCORD_TOKEN"] in Python.

Step 5: Run the bot as a systemd service

systemd is built into modern Linux distributions and can start your bot at boot, restart it if it crashes, and collect its logs. Create /etc/systemd/system/discord-bot.service.

Node.js version:

[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=discordbot
Group=discordbot
WorkingDirectory=/opt/discord-bot/app
EnvironmentFile=/opt/discord-bot/.env
ExecStart=/usr/bin/node index.js
Restart=always
RestartSec=5
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

Python version: change only the ExecStart line:

ExecStart=/opt/discord-bot/venv/bin/python bot.py

Use the real path to node (check with which node) and your actual entry file name.

Enable and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now discord-bot
sudo systemctl status discord-bot

Your bot should now show as online in Discord, and it will come back automatically after crashes and reboots.

Prefer PM2 for Node.js?

PM2 is a popular process manager for Node.js apps and works well for bots too. We cover it in our guide to deploying a Node.js app with PM2. systemd has the advantage of being built in and handling Python and Node.js bots the same way.

Step 6: Read the logs

When something goes wrong, systemd's journal has the output:

sudo journalctl -u discord-bot -f

-f follows new log lines live. Use --since "1 hour ago" to look back. Common problems you'll spot here:

  • Invalid token: the token in .env is wrong or was reset.
  • Disallowed intents: your code requests privileged intents (such as message content or server members) that aren't enabled for your bot in the Developer Portal.
  • Module not found: dependencies weren't installed in the right place, or the service is using a different Node.js or Python than you expect.

Step 7: Deploy updates

A simple update routine:

cd /opt/discord-bot/app
sudo -u discordbot git pull
sudo -u discordbot npm ci --omit=dev     # or pip install -r requirements.txt in the venv
sudo systemctl restart discord-bot

If you change slash command definitions, remember to re-register them with Discord in whatever way your bot handles it.

Extra tips for a reliable bot

  • Handle errors. Catch exceptions in command handlers so one bad command doesn't crash the whole bot.
  • Respect rate limits. discord.js and discord.py handle Discord's rate limits for you, but bulk actions in loops can still trigger them.
  • Use a real database (SQLite for small bots, PostgreSQL for larger ones) instead of JSON files that can corrupt if the bot crashes mid-write.
  • Back up your data regularly. See the 3-2-1 backup rule.
  • Keep dependencies updated so your bot keeps working as Discord's API evolves.

If you still need a server for an always-on project like this, Host Whimsy offers VPS hosting.

Wrapping up

Hosting a Discord bot 24/7 on a VPS is straightforward: install the runtime, keep the token in a locked-down environment file, run the bot as a dedicated user under systemd with automatic restarts, and use the journal to troubleshoot. No open ports, no sleep timers, and your bot stays online even when your laptop doesn't.