Back

How to Host a Telegram Bot on a VPS 2026

Articles

06/03/2026

Telegram Bots • VPS Hosting • 2026 Edition

How to Host a Telegram Bot on a VPS (Step-by-Step Beginner Guide)

Want your Telegram bot to stay online 24/7? This guide shows you how to deploy a Telegram bot on a VPS server using a clean, reliable workflow:
server setup, security, Node.js or Python installation, environment variables, process managers (PM2/systemd), logs, updates, and best practices.

by Hamza

Hosting a Telegram bot is easy when you’re testing on your laptop—but it becomes a problem the moment your computer sleeps, restarts,
or loses internet. A VPS (Virtual Private Server) solves this by running your bot in a data center with stable uptime, so your bot
stays online 24/7 for your users.

In this tutorial, you’ll learn a practical production setup that’s beginner-friendly but also scalable. You can host a Telegram bot for moderation,
customer support, notifications, order updates, crypto alerts, marketing automation, or community management—without needing advanced DevOps.

What You’ll Learn

  • What you need before deploying a Telegram bot
  • How to set up a secure VPS (users, firewall, updates)
  • How Telegram bot updates work (long polling vs webhooks)
  • How to deploy with Node.js or Python
  • How to keep your bot running 24/7 (PM2 or systemd)
  • Logging, auto-restarts, and safe updates
  • Best practices for secrets, rate limits, and reliability

1) What You Need Before Hosting a Telegram Bot

Before touching the VPS, make sure you have the basics ready. Hosting is smoother when you prepare your bot and deployment method in advance.

✅ Telegram bot token

Create your bot with @BotFather in Telegram and copy the API token. This token is secret—never commit it to GitHub.

✅ Your bot code

Node.js (Telegraf/node-telegram-bot-api) or Python (python-telegram-bot/aiogram). Both work great on VPS.

✅ A VPS server

Choose Ubuntu 22.04 or 24.04. For most bots, 1 CPU + 1GB RAM is enough to start.

✅ A deployment plan

Decide if you will run long polling (simpler) or webhooks (needs domain + HTTPS).

2) Telegram Updates: Long Polling vs Webhooks

Telegram bots receive messages through “updates.” Your bot needs a way to read these updates. The two common methods are long polling and webhooks.

Long Polling (recommended for beginners)

Your bot asks Telegram’s servers for new updates every few seconds. This is the simplest method and works perfectly on VPS
without needing a domain or HTTPS. Most small-to-medium bots use long polling because it’s reliable and easy to maintain.

Webhooks (best for advanced setups)

Telegram sends updates to your server via an HTTPS URL. This is efficient at scale and reduces polling overhead,
but it requires a public domain, SSL, and a web server or reverse proxy (like Nginx).

In this article we’ll focus on long polling because it’s the fastest way to get a reliable Telegram bot running on a VPS.
At the end, you’ll also find guidance on how to switch to webhooks if you need them.

3) VPS Setup: Secure the Server First

Once your VPS is created, connect via SSH and do basic security steps. This prevents most beginner issues such as brute-force login attempts
or accidental root mistakes.

A) Connect using SSH

ssh root@YOUR_SERVER_IP

B) Update packages

apt update && apt upgrade -y

C) Create a non-root user

adduser botuser
usermod -aG sudo botuser

D) Enable firewall (UFW)

ufw allow OpenSSH
ufw enable
ufw status

At this point, your VPS is updated and protected. Next, install the runtime (Node.js or Python) and deploy your bot code.

4) Deploy a Telegram Bot with Node.js (Telegraf Example)

If your bot is written in Node.js, one popular framework is Telegraf. The steps are the same for most Node bots:
install Node.js, upload your code, install dependencies, configure environment variables, then run using a process manager.

A) Install Node.js + npm

sudo apt install nodejs npm -y
node -v
npm -v

B) Upload your bot (Git recommended)

If your bot is on GitHub, cloning is the cleanest method. If it’s private, use SSH keys or a token-based approach.

sudo apt install git -y
git clone https://github.com/yourusername/telegram-bot.git
cd telegram-bot

C) Install dependencies

npm install

D) Store your token securely (.env)

Do not hard-code the Telegram token in your code. Use environment variables.

nano .env
BOT_TOKEN=YOUR_TELEGRAM_BOT_TOKEN

E) Example minimal Telegraf bot

Your project may already have code, but this example shows the simplest working structure.

// index.js
require("dotenv").config();
const { Telegraf } = require("telegraf");

const bot = new Telegraf(process.env.BOT_TOKEN);

bot.start((ctx) => ctx.reply("Hello! I am online ✅"));
bot.command("ping", (ctx) => ctx.reply("pong 🟢"));
bot.on("text", (ctx) => ctx.reply("You said: " + ctx.message.text));

bot.launch();
console.log("Bot started...");

F) Test run

node index.js

If the bot responds in Telegram, you’re ready to keep it online permanently with PM2.

5) Keep Your Telegram Bot Running 24/7 (PM2)

When you close SSH, any process started normally will stop. A process manager like PM2 keeps your bot alive, restarts it if it crashes,
and can auto-start it after server reboot.

A) Install PM2

sudo npm install pm2 -g

B) Start your bot

pm2 start index.js --name telegram-bot

C) View status + logs

pm2 list
pm2 logs telegram-bot

D) Auto-start on reboot

pm2 save
pm2 startup

After this, your Telegram bot is effectively “production hosted.” Now focus on security, updates, and reliability.

6) Hosting a Telegram Bot with Python (Alternative)

If your bot is written in Python, the process is similar: install Python, set up a virtual environment, install requirements,
store tokens in environment variables, then run your bot with systemd (or PM2 if you prefer, but systemd is common for Python).

A) Install Python and tools

sudo apt update
sudo apt install python3 python3-venv python3-pip -y

B) Create a project folder + venv

mkdir telegram-bot
cd telegram-bot
python3 -m venv venv
source venv/bin/activate

C) Install python-telegram-bot (example)

pip install python-telegram-bot

D) Run with systemd (production)

systemd runs your service in the background and restarts it automatically. You create a service file that points to your Python script.
The main idea: your bot becomes a server “service,” just like Nginx or SSH.

Tip: If you want, I can generate a ready-to-paste systemd service file based on your exact project path and script name.
That avoids common beginner mistakes (wrong working directory, wrong python path, missing env vars).

7) Optional: Switching to Webhooks (Domain + HTTPS)

Long polling is excellent for most bots. But if you’re building a more advanced system (high traffic, multiple bots, or you want cleaner event delivery),
webhooks can be a good option.

  • You need a domain pointing to your VPS IP
  • You need HTTPS (Let’s Encrypt SSL is commonly used)
  • You often use a reverse proxy like Nginx
  • Your bot framework must be configured to listen on a port and handle webhook routes

Many beginners start with long polling, then upgrade to webhooks later—this is a smart path because you learn the basics first.

8) Reliability & Best Practices (Real Production Tips)

🔐 Keep tokens & secrets safe

Store tokens in .env or secure environment files. Add .env to your .gitignore.
Rotate the token immediately if you ever leaked it.

📈 Monitor logs

Use pm2 logs (Node) or journalctl (systemd) to diagnose issues quickly. Logging helps you catch bugs early.

♻️ Safe updates

Use Git pulls and restart your bot gracefully. Keep a backup branch or tag so you can roll back if an update breaks production.

⏳ Handle rate limits

Telegram can rate limit bots if you send too many messages too fast. Add queues/delays for mass notifications and handle retries properly.

A well-hosted bot isn’t only “online”—it’s stable, secure, and easy to maintain. Once your bot is running with PM2 or systemd,
your next focus should be monitoring, safe updates, and protecting secrets.

Conclusion: Hosting a Telegram Bot on a VPS is the Best Upgrade

Running your Telegram bot on a VPS is the simplest way to keep it online 24/7. You get consistent uptime, better performance, and full control
over your environment. Start with long polling, deploy your code with Git, store your token safely, and keep the bot alive with PM2 or systemd.
Once you’ve done this once, deploying new bots becomes fast and repeatable.

If you want a reliable server for your Telegram bot, you can explore
Retzor VPS hosting
for scalable VPS plans that work well for bots, apps, and websites.

© 2026 • Telegram Bot VPS Hosting Guide
Retzor Reviews