Telegram bot on a server: launching and autostart
How to host a Telegram bot on a VPS: isolation under its own user, a systemd service, token storage, logs, and handling network outages.
Updated August 23, 2026
A bot needs a constantly running process — that's where free hosting ends and VDS begins. A bot barely needs any resources: even VDS-1 (1 core, 2 GB) handles a bot with thousands of users.
Placement
A separate user and directory — so the bot doesn't run as root:
useradd -r -m -d /srv/bot -s /usr/sbin/nologin botuser
cd /srv/bot && sudo -u botuser git clone https://github.com/you/bot.git .
sudo -u botuser python3 -m venv venv
sudo -u botuser venv/bin/pip install -r requirements.txt
For Node.js: npm ci --omit=dev instead of a venv.
Keep the token in an environment file, not in the code:
install -o botuser -g botuser -m 600 /dev/null /srv/bot/.env
echo 'BOT_TOKEN=123456:ABC...' > /srv/bot/.env
The file with the token must not end up in the repository: add it to .gitignore before the first commit.
Service
/etc/systemd/system/tgbot.service:
[Unit]
Description=Telegram bot
After=network-online.target
Wants=network-online.target
[Service]
User=botuser
WorkingDirectory=/srv/bot
EnvironmentFile=/srv/bot/.env
ExecStart=/srv/bot/venv/bin/python -u bot.py
Restart=always
RestartSec=10
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now tgbot
journalctl -u tgbot -f
The -u flag on Python disables output buffering — without it, logs will show up in the journal with a delay of several minutes, or not at all.
Long polling or webhook
Long polling is simpler: the bot polls Telegram itself, and you don't need to expose anything. That's enough for most tasks.
A webhook is faster and cheaper on traffic, but it requires a domain and a certificate:
location /bot/token {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
}
You'll need Let's Encrypt and a reverse proxy. Telegram accepts self-signed certificates reluctantly — use a regular one.
There must never be two copies of the bot running at once: Telegram delivers updates to only one recipient, and the bots will start responding intermittently. After edits, do a restart, not a second launch.
Data
SQLite is enough for settings and statistics — a file next to the bot. A separate database server is only needed if the bot has a web panel or multiple processes.
Common mistakes
| Symptom | Cause |
|---|---|
Conflict: terminated by other getUpdates | Two copies of the bot are running |
| The bot goes silent after a night | Restart=always isn't configured, and the process crashed on a network drop |
Unauthorized | The token was revoked or copied with extra spaces |
| No logs | Output buffering, or writing to a file instead of stdout |
| Works manually, crashes as a service | No environment variables in systemd |
A token that has appeared in a screenshot, a public repository or someone else's chat is considered compromised. Revoke it through BotFather and change the
.envfile — don't count on nobody noticing.