ServerLoomRapide et Sécurisé
Connexion Commencer

How to host a Discord bot 24/7 on a VPS (Node.js + PM2)

Your bot goes offline when your PC turns off. Move it to an Ubuntu VPS, keep the token safe and let PM2 restart it automatically after crashes and reboots.

Débutant 8 min de lecture Mis à jour le 5 déc. 2026 Équipe ServerLoom
Sur cette page

A Discord bot running on your own PC goes offline every time you turn it off. On a small VPS it runs 24/7, and PM2 restarts it automatically after a crash or a reboot. This guide uses a Node.js bot (discord.js) on Ubuntu; Python bots work too (see the tip at the end).

What you need

  • A small Ubuntu VPS — 1 vCore and 1 GB RAM is enough for most bots; music bots and big servers need 2 GB or more.
  • Your bot's code and its token from the Discord Developer Portal.

Step 1: Install Node.js (LTS)

Connect with ssh root@YOUR-SERVER-IP, then install the current long-term-support version of Node.js:

Bash
apt update && apt install -y curl gitcurl -fsSL https://deb.nodesource.com/setup_lts.x | bash -apt install -y nodejsnode -v

Step 2: Create a user and upload your bot

Run the bot as a normal user, never as root. Then get your code onto the server — with git clone (private repo: use a deploy key) or by uploading the folder with WinSCP / FileZilla (SFTP). Don't upload the node_modules folder.

Bash
adduser --disabled-password --gecos "" botsu - botgit clone https://github.com/YOUR-NAME/YOUR-BOT.git mybotcd mybotnpm install

Step 3: Keep the token safe in .env

Never write the token inside your code. Put it in a .env file that only your user can read (with discord.js, load it with dotenv or Node's --env-file option):

Bash
echo 'DISCORD_TOKEN=paste-your-token-here' > .envchmod 600 .env
Attention

Never commit .env to GitHub and never post your token in Discord. If it leaks, click "Reset Token" in the Discord Developer Portal right away.

Step 4: Run the bot 24/7 with PM2

Install PM2 once (as root), then start your bot as the bot user. Replace index.js with your bot's main file:

Bash
# as rootnpm install -g pm2 # as the bot user, inside the bot foldersu - botcd mybotpm2 start index.js --name mybotpm2 savepm2 startup

pm2 startup prints one sudo env PATH=... command — copy it and run it as root. From now on the bot starts by itself after every reboot.

Everyday commands

  • pm2 status — is the bot online?
  • pm2 logs mybot — see errors live (Ctrl + C to leave).
  • pm2 restart mybot — after you changed the code.
  • Update from GitHub: git pull && npm install && pm2 restart mybot.
Astuce de pro

Python bot? PM2 runs it too: pm2 start bot.py --name mybot --interpreter python3 (install your packages in a virtual environment first).

Next steps

Your VPS is on the internet: take 15 minutes to secure your Linux VPS (SSH keys, firewall, automatic bans). Running several bots? Start each one with its own --name — one small VPS can run many bots.

Prêt à essayer ? Essayez-le sur un VPS ServerLoom

AMD Ryzen haute fréquence, stockage NVMe, accès root complet et protection DDoS ServerLoom Shield — en ligne en quelques minutes.

Voir les offres VPS Ryzen
Bloqué à une étape ? Notre équipe vous aide 24h/24 et 7j/7. Demander sur Discord

Besoin d’aide ?

Choisissez comment nous contacter