How to Host a Discord Bot on a VPS: systemd, Swap, and the Parts
Some links on this page are affiliate links. If you sign up through them we may earn a commission at no extra cost to you. We only link to services we have actually used, and commission never decides what we recommend.
You got the token, you ran node index.js, the bot answered a slash command, and then you closed the terminal. Now it is offline, and the tutorial you followed stopped at the exact moment the hard part begins. This is the part after the tutorial: a fresh VPS, a supervisor that starts at boot, and the four or five places where the install actually breaks.
Quick verdict
One bot, one process, one small VPS: 1 GB RAM, a non-root bot user, and a systemd unit with Restart=on-failure. That is the whole answer. 512 MB works only if you add a swapfile first and accept that npm ci may crawl; below that you are debugging the OOM killer instead of your bot.
The provider matters less than the region. Pick the region closest to where your users are, from whichever provider has a plan you can afford, and stop optimising. Region is the only lever that changes a bot's latency — everything else about the box is the same shape. Measure before you commit: spin up an hourly instance in two candidate regions and run curl -w '%{time_connect}\n' -o /dev/null -s https://discord.com/api/v10/gateway from each, then destroy the loser.
The command that matters:
sudo systemctl daemon-reload && sudo systemctl enable --now discordbot
enable writes the symlink that starts the unit at boot; --now starts it this second. Skip enable and you have a bot that survives crashes but not reboots, which is the failure you were trying to fix.
The real choice is not the provider, it's the process supervisor
Provider comparisons barely matter for a bot that opens one outbound websocket and sits idle most of the day. Check the actual footprint yourself: systemctl status discordbot reports Memory: in its output, and that number is the whole story. What matters is the thing that notices the process died at 3 a.m. and starts it again.
nohup node index.js & is the trap. It ignores SIGHUP, so closing your SSH session does not kill the bot — which makes it feel like it worked. But nothing re-runs it after a reboot, nothing restarts it after an unhandled promise rejection, and its output lands in nohup.out in whatever directory you happened to be in. When the bot dies silently three days later you have no timestamps and no stack trace.
| Option | What it actually does | Failure mode | Pick it when |
|---|---|---|---|
| systemd (plain VPS) | Starts the unit at boot, restarts on crash, logs to the journal | No zero-downtime deploys; a code bug becomes a restart loop | You have one bot and one entry file, and you want the supervisor that ships with the OS |
| screen / tmux | Keeps a shell session alive after you disconnect | Dies on reboot, dies on OOM, no restart, logs scroll away in a buffer | You are debugging interactively for ten minutes, never for production |
| Docker | Container with a restart policy, image pins your Node version | More moving parts: image builds, volume mounts for .env, log driver config | You already containerize things, or the same box runs a database and a web app too |
The decision rule is short. If you already know Docker and have a registry or a Compose file lying around, use it — the restart policy does the same job and you get a reproducible Node version for free. If you have one bot and one index.js, systemd is less to maintain, ships with the OS, and needs no daemon of its own.
There is a middle option people reach for: pm2. It is a real supervisor and it works, but it has its own boot persistence to configure (pm2 startup, then pm2 save), and if you forget the second command your process list is lost on reboot. systemd has no equivalent footgun because the unit file is the saved state.
On picking a host. Any 1 GB instance from any provider runs this setup unchanged; the only thing worth comparing is region. If your guilds are concentrated somewhere far from the provider's cheapest default datacentre, a region closer to them is the one upgrade that shows up in slash command response time. Check the current region list and price on the order page before you buy — both change.
Step by step, including the parts that break
1. Create the instance and a non-root user
Pick the smallest plan with 1 GB RAM. On first login as root:
adduser bot
usermod -aG sudo bot
Then log out and back in as bot. Running the bot as root is the habit you will regret: any dependency that gets compromised, or any fs call with a bad path in your own code, runs with full write access to the box. A bot user limits the blast radius to /home/bot, and it costs you one command now.
2. Install a Node that discord.js will accept
The distro's nodejs package is often older than what current discord.js requires. The symptom is specific: npm prints an EBADENGINE warning, or the process dies immediately with SyntaxError: Unexpected token '?' because optional chaining is not parsed. Install from NodeSource instead:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
node -v
Check the printed version against the engines field in your package.json before you go further. Fixing it here takes a minute; fixing it after the unit is written means you are reading journal output to find the same error.
3. Add swap before you add RAM
npm ci resolving a discord.js dependency tree is the memory spike, not the running bot. On a 1 GB box it can squeak through; on 512 MB it can be killed mid-install, and you see Killed in the output or a truncated node_modules. If that happens, dmesg | grep -i oom tells you whether the kernel did it. Add swap first:
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
That last line is the one people skip, and it is the one that matters — without it the swapfile is gone after the next reboot and your bot comes back to an OOM kill it did not have before.
4. Copy the code and lock down the token
sudo mkdir -p /home/bot/app
sudo chown bot:bot /home/bot/app
git clone <your-repo-url> /home/bot/app
# or, from your laptop:
# rsync -av --exclude node_modules --exclude .env ./ bot@your-host:/home/bot/app/
cd /home/bot/app
npm ci --omit=dev
chmod 600 /home/bot/app/.env
Use npm ci --omit=dev, not npm install. ci installs exactly what package-lock.json pins and fails loudly if the lockfile and package.json disagree, which is what you want on a server. --omit=dev skips nodemon, eslint, and the rest of the toolchain you only need on your laptop. If you have no lockfile, commit one — ci will refuse to run without it.
Keep DISCORD_TOKEN in .env, owned by bot, mode 600. Do not put it in the unit file as an Environment= line, because unit files are world-readable and end up pasted into forum threads. Do not commit .env either; add it to .gitignore the same day you create it.
5. Write the unit file
# /etc/systemd/system/discordbot.service
[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=bot
WorkingDirectory=/home/bot/app
ExecStart=/usr/bin/node index.js
Restart=on-failure
RestartSec=5
EnvironmentFile=/home/bot/app/.env
[Install]
WantedBy=multi-user.target
After=network-online.target matters more than it looks: without it, the bot can start before DNS is up and crash on the first gateway connection. It only helps if something actually waits, though. On minimal images the target is a no-op unless systemd-networkd-wait-online or NetworkManager-wait-online is enabled — check with systemctl status systemd-networkd-wait-online. If it is not there, enable it, or accept that the ordering line is decorative.
Restart=on-failure restarts on a non-zero exit or a signal, but not on a clean exit 0 — which is what you want, so a deliberate shutdown stays shut down. RestartSec=5 keeps a crash loop from hammering the journal and Discord's gateway.
If your code already loads .env through dotenv, drop the EnvironmentFile line and leave the file where it is. Both work; do not do both, or you will spend an hour wondering which value won. Note that systemd's parser is not dotenv: it does not understand export FOO=bar, and multi-line values will not survive. Keep the file to plain KEY=value lines if you use it.
Then:
sudo systemctl daemon-reload
sudo systemctl enable --now discordbot
sudo systemctl status discordbot
daemon-reload is the step everyone forgets after editing the unit. Without it systemd keeps running the old definition and you conclude, wrongly, that your change did nothing.
6. Firewall: one rule, nothing else
sudo ufw allow 22/tcp
sudo ufw enable
A Discord bot makes outbound connections to Discord's gateway and REST API. It does not listen on a port, so it needs no inbound rule beyond SSH. If you changed your SSH port, allow that port instead. ufw enable asks you to confirm because it may disrupt the existing connection — answer y only after you have allowed the port you are actually connected on. That prompt is the lockout this paragraph warns about, wearing a hat.
7. Prove it survives a reboot
sudo reboot
Log back in and run:
systemctl status discordbot
journalctl -u discordbot -f
journalctl -u discordbot -f is your log tail from now on — not a file in the app directory, not nohup.out. For the crash you missed overnight, journalctl -u discordbot --since '10 min ago' gives you the window without paging through a week of noise. If you see the unit cycling every five seconds, Restart= is working and your code is not; read the stack trace above the restart line.
A crash loop writes a line every RestartSec, which is how a bad deploy fills the disk. Cap the journal before that happens:
sudo sed -i 's/^#\?SystemMaxUse=.*/SystemMaxUse=200M/' /etc/systemd/journald.conf
sudo systemctl restart systemd-journald
sudo journalctl --vacuum-size=200M
Where this falls short
Nobody patches the OS for you and nobody upgrades Node when a dependency drops support for your version. Put a monthly reminder in your calendar for sudo apt upgrade && sudo apt autoremove and npm outdated in the app directory; the alternative is a box that has not seen a security update since you provisioned it.
Hourly billing is easy to forget. Set a calendar reminder for the day after you stop needing the instance, or destroy it the moment the project ends — a forgotten box bills quietly for as long as it exists.
systemd restarts on crash, but it does not fix the crash. A bot with a bug in its message handler will restart every five seconds forever, reconnect to Discord's gateway on each loop, and fill the journal until the cap you set in step 7 kicks in. Restart=on-failure is a safety net, not a substitute for reading the trace.
A git pull deploy is not zero-downtime. You stop the unit, pull, install, and start it again, and the bot is offline for those seconds — commands sent in that window fail. For a hobby bot nobody notices; for a bot in busy servers, schedule deploys for quiet hours.
One VPS is one point of failure with nothing behind it. If the host goes down, the bot is down until it comes back; there is no failover to configure on a single box. And if the bot is in enough guilds that one process cannot hold the connection load, one box and one node index.js is the wrong shape — that is sharding, and it is a different article.
If you genuinely will not patch a server, a managed bot host is the honest choice. It is a trade about your time, not a moral failing.
FAQ
Do I need a domain name or an open port for the bot?
No. The bot connects outbound to Discord; nothing connects in. Leave the firewall at SSH only, and skip the reverse proxy entirely unless you add a web dashboard later.
Will systemd restart my bot if it crashes?
Yes, on any non-zero exit or signal, after RestartSec=5. It will not restart after a clean exit 0, and it will not stop a crash loop — that is your code's problem, and the journal will show it.
How do I update the bot after pushing changes?
cd /home/bot/app && git pull && npm ci --omit=dev, then sudo systemctl restart discordbot. No daemon-reload needed unless you edited the unit file itself.
Can I run two bots on one VPS?
Yes — one unit file per bot, each with its own WorkingDirectory and its own .env. Watch memory: two discord.js processes on 1 GB with no swap is where you start meeting the OOM killer.
Related reading: Cheap VPS Providers for Discord Bot Hosting: A Shortlist That Actually Stays Up, Vultr review: High Frequency vs regular compute after a week
Bottom line
Pick systemd if you have one bot and one entry file: the unit above is the whole supervisor, it ships with the OS, and Restart=on-failure covers the 3 a.m. crash. Pick Docker instead if you already containerize things or the same machine runs other services, because one restart policy beats two.
Try Vultr
Good HK/Tokyo latency from Asia.