aslain.dev
0%
01 Hizmetler 02 Hakkımda 03 Projeler 04 Stack 05 Blog 06 İletişim
← Tüm makaleler Discord Bots

Discord Bot VPS Setup: Run It 24/7 with pm2

Running your bot on your own computer works fine, right up until you shut it down or it goes to sleep. For a permanent solution you need a proper Discord bot VPS setup: you move the bot onto a small cloud server and keep it alive 24/7 with a process manager. In this guide I'll walk through preparing an Ubuntu VPS from scratch, installing Node.js, and running a discord.js bot permanently with pm2 so it starts back up automatically even after a reboot.

Why a VPS and why pm2?

A bot has to hold a constant connection (a WebSocket) to Discord. If that connection drops, the bot goes offline and stops answering commands. A VPS is a Linux machine with uninterrupted internet and power, running 365 days a year — exactly what you need.

So why not just run node index.js? Because the moment you close the terminal, the process dies. pm2 is a process manager: it keeps the bot running in the background, restarts it automatically if it crashes, collects its logs, and brings the bot back up on its own when the server reboots.

  • Auto-restart: if the bot throws an error and crashes, pm2 restarts it within seconds.
  • Boot resilience: the bot starts automatically even after the VPS reboots.
  • Log management: stdout/stderr are watchable with a single command.
  • Zero cost: pm2 is open source and free.

1. Choosing a VPS and the first connection

A Discord bot uses very few resources; the smallest plan with 1 vCPU and 1 GB RAM is more than enough. From a provider like Hetzner, DigitalOcean, Contabo or similar, create a server with Ubuntu 22.04 LTS or 24.04 LTS pre-installed. The provider will give you an IP address and a root password (or an SSH key).

Connect from your own machine via SSH:

ssh root@YOUR_SERVER_IP

The first job is updating the packages:

apt update && apt upgrade -y

2. A sudo user and basic security

Doing everything as root is risky. Let's create a separate user and grant it privileges:

adduser bot
usermod -aG sudo bot

You can now switch to this user with su - bot. Let's also set up a simple firewall; the bot's outbound connection to Discord needs no open port, so leaving only SSH open is enough:

sudo ufw allow OpenSSH
sudo ufw enable

3. Installing the right Node.js version

The Node.js in Ubuntu's own repository is often outdated; discord.js v14 requires at least Node.js 16.11, and in practice a current LTS release. Let's install the latest LTS (for example 20.x) using the NodeSource repository:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs

Verify the installation:

node -v
npm -v

4. Getting your bot's code onto the server

The cleanest method is to pull the code via Git. If your bot lives in a GitHub repository:

git clone https://github.com/username/bot-repo.git
cd bot-repo
npm install

Never hard-code secrets like your token or push them to the repository. Instead, create a .env file:

nano .env

Put the token inside:

DISCORD_TOKEN=your_secret_token

To make the bot read this value you can use the dotenv package (npm install dotenv) and add require('dotenv').config() at the very top of your entry file. Always add .env to your .gitignore.

5. Running the bot permanently with pm2

Let's install pm2 globally:

sudo npm install -g pm2

Start the bot and give it a readable name:

pm2 start index.js --name discord-bot

If your bot's entry file is different (for example bot.js or src/index.js), use that path. To see the status:

pm2 status

To follow the logs live:

pm2 logs discord-bot

Other commonly used commands:

  • pm2 restart discord-bot — restarts the bot (after you update the code).
  • pm2 stop discord-bot — stops it.
  • pm2 delete discord-bot — removes it from the list.
  • pm2 monit — shows CPU/RAM usage live.

6. Keeping the bot alive across reboots

This is where the real magic is. When the VPS reboots after an update or an outage, we want the bot to come back on its own. First, have pm2 generate a startup script:

pm2 startup

This command prints a single line starting with sudo env PATH=...; copy that line and run it. Then save the current process list:

pm2 save

Now even if you reboot the server with sudo reboot, pm2 kicks in at boot and brings the discord-bot process back up automatically. Don't be afraid to test it.

7. When you update the code

When you add a new feature, the flow is simple: pull the changes, install dependencies if needed, restart the process:

cd bot-repo
git pull
npm install
pm2 restart discord-bot

If you didn't add any new dependency, you can skip the npm install step.

Frequently Asked Questions

The bot keeps crashing — what is pm2 doing?

pm2 automatically restarts the crashed process, but if the root cause persists it can enter an endless restart loop. Read the error with pm2 logs discord-bot; it's usually an invalid token, a missing intents setting, or an unhandled Promise rejection. Fix the issue in the code rather than relying on restarts alone.

Why pm2 instead of screen or nohup?

screen and nohup keep the bot in the background but won't restart it on a crash, won't start it on boot, and offer no log management. pm2 gives you all of that in a single tool; for a production environment it's far more robust.

Can I run multiple bots on the same VPS?

Yes. Use a separate folder and a separate pm2 start ... --name for each bot. Even a small VPS comfortably handles several lightweight bots; you can watch resource usage with pm2 monit.

Want to ship your bot with confidence? If you need help with Discord bot development, VPS setup, and stable 24/7 hosting, get in touch — let's get your project up and running together.

Bu kategorideki tüm yazılar →

Devamı için