TutorialNov 7, 20264 min read

How to Host a Telegram Bot 24/7 on a VPS

Run a Python or Node Telegram bot around the clock on a small Linux VPS: a systemd service that restarts it, the token kept out of the code, logs, updates, and when you need webhooks instead of long polling.

Short answer: to keep a Telegram bot online 24/7, run it on a small Linux VPS as a systemd service. With long polling (the default in aiogram, python-telegram-bot, grammY and telegraf) the bot needs no domain, no certificate and no open port. A server with 1 vCPU and 1 GB RAM for $3.90 a month is enough for most bots, and a few of them side by side.

Free hosting tiers sleep, restart your bot at random or limit its hours. Your own server keeps it running, and you control the Python version, the database and the files.

01Which Server

BotPlanPrice
One or a few simple bots1 vCPU, 1 GB RAM, 10 GB NVMe$3.90/month
A bot with a database, or many bots1–2 vCPU, 2 GB RAM, 20 GB NVMe$6.50/month
Heavy work: image or file processing, scraping2 vCPU, 6 GB RAM, 40 GB NVMe$13/month

Telegram's servers are in Amsterdam and Singapore, among others, so Amsterdam and Frankfurt are a good default; any of our six locations works for a normal bot.

Nano1 vCPU · 1 GB · 10 GB NVMe$3.90/mobilled monthlyBuy
Most popularSpark1 vCPU · 2 GB · 20 GB NVMe$6.50/mobilled monthlyBuy
Pulse2 vCPU · 2 GB · 20 GB NVMe$6.50/mobilled monthlyBuy
Surge2 vCPU · 6 GB · 40 GB NVMe$13.00/mobilled monthlyBuy
Storm6 vCPU · 10 GB · 40 GB NVMe$26.00/mobilled monthlyBuy
Apex6 vCPU · 10 GB · 80 GB NVMe$39.00/mobilled monthlyBuy

Full root SSH access · NVMe storage · DDoS protection · No setup fee

02Put the Bot on the Server

The example is a Python bot on Ubuntu 24.04 or Debian 12. Log in over SSH as root:

  1. 1Create a user for the bot: adduser --disabled-password --gecos "" bot
  2. 2Install Python tools: apt update && apt install -y python3-venv git
  3. 3Copy the code as that user: su - bot, then git clone your repository or upload the files with scp.
  4. 4Make a virtual environment: python3 -m venv .venv && .venv/bin/pip install -r requirements.txt
  5. 5Store the token in /home/bot/bot.env with one line, BOT_TOKEN=123456:ABC..., then chmod 600 bot.env.
  6. 6Test it once by hand: set -a; . ./bot.env; .venv/bin/python bot.py. Send it a message, then stop it with Ctrl+C.

Read the token in your code with os.environ["BOT_TOKEN"], so it never sits in the source or in Git.

03Run It as a Service

Create /etc/systemd/system/telegram-bot.service as root:

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

[Service]
User=bot
WorkingDirectory=/home/bot/mybot
EnvironmentFile=/home/bot/bot.env
ExecStart=/home/bot/mybot/.venv/bin/python bot.py
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Then start it and make it start at boot:

bash
systemctl daemon-reload
systemctl enable --now telegram-bot
systemctl status telegram-bot

Restart=always brings it back five seconds after a crash. For a Node bot, change ExecStart to /usr/bin/node index.js; the rest stays the same.

04Logs and Updates

Read the log

journalctl -u telegram-bot -f shows everything the bot prints, live.

Deploy a new version

git pull as the bot user, then systemctl restart telegram-bot as root.

Several bots

one folder, one env file and one service file each (bot-shop.service, bot-alerts.service).

Back up the database

(SQLite file or a dump) off the server now and then.

05Long Polling or Webhooks?

Long polling means the bot asks Telegram for new messages itself. It works behind any firewall, needs nothing extra and is fine for almost every bot.

Webhooks mean Telegram sends each update to your server. They need a public HTTPS address: a domain pointing at the server, a certificate (Caddy or nginx with Let's Encrypt) and one of the ports Telegram allows, 443, 80, 88 or 8443. They're worth it for bots with very high traffic or when the bot already runs inside a web app. More on both on our Telegram bots page.

06Keep the Server Safe

  • SSH only from your IP, using the firewall on your server page, and log in with a key.
  • Turn on automatic security updates: apt install -y unattended-upgrades.
  • Revoke a leaked token in @BotFather with /revoke and put the new one in bot.env.

From $3.90 a month for a server with 1 vCPU, 1 GB RAM and 10 GB NVMe, which runs most bots and several small ones side by side.

  • host telegram bot
  • telegram bot hosting
  • run telegram bot 24/7
  • telegram bot vps
  • deploy python telegram bot
  • aiogram hosting

Keep reading

All articles