Back to Home

Deploy Telegram Bot in 8 Minutes | Developer's Guide

Practical Guide to Deploying a Telegram Bot on VPS in 8 Minutes. Detailed Server Setup, Project Transfer via SFTP, PM2 Configuration for Auto-Start. Solving Common Deployment Issues.

How to Deploy a Telegram Bot in 8 Minutes: Practical Guide
Advertisement 728x90

Quick Deployment of a Telegram Bot on VPS: From Local Run to 24/7 Uptime

You've finished developing your Telegram bot, but running it locally doesn't ensure constant availability. Most developers struggle with deploying to production, overwhelmed by endless info on Docker and Kubernetes. In reality, you can deploy it in just 8 minutes with minimal resources. This guide shows how to deploy an aiogram bot on a cheap VPS without complex setups, using basic Linux tools.

Preparing the Infrastructure

Choosing a cloud provider is crucial for reliable bot operation. Timeweb Cloud provides access to servers in 12 geographic regions, including locations outside Russia. This is essential for using APIs blocked in the RF (Groq, ChatGPT, and other AI services). For bots without external dependencies, Russian data centers are fine.

Key server parameters:

Google AdInline article slot
  • OS: Ubuntu 24.04 LTS
  • Configuration: minimal (1 vCPU, 1 GB RAM, 25 GB SSD)
  • Cost: from 290 ₽/month
  • Backups: recommended to enable

When creating the instance, check the backups section. Backups protect against:

  • Server crashes
  • SQLite database corruption
  • Code update errors
  • Accidental file deletion

Timeweb's backup system lets you restore the project state from the last 7 days at 24-hour intervals. This is crucial for preventing data loss when testing new features.

Setting Up the Server Environment

After activating the server, connect via SSH:

Google AdInline article slot
ssh root@your_IP_address

Update system packages and install required components:

sudo apt update && sudo apt upgrade -y
sudo apt install python3 python3-pip git -y
npm install pm2 -g

Important: check the Python version before installing dependencies:

python3 --version

For projects using a virtual environment, run:

Google AdInline article slot
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

Transferring the Project and Launching

Use SFTP to transfer files. In FileZilla, enter:

  • Host: server IP address
  • Login: root
  • Password: from control panel
  • Port: 22

After copying the project to the server:

  • Navigate to the project directory
  • Activate the virtual environment
  • Install dependencies
  • Launch the bot via PM2
cd /root/your_project
source venv/bin/activate
pm2 start main.py --name "telegram-bot"

PM2 ensures auto-restart after reboots and monitors crashes. Check process status:

pm2 status

Maintenance and Updates

When making code changes, follow this strict checklist:

  • Edit files via SFTP (FileZilla → View/Edit)
  • Save changes (Ctrl+S)
  • Confirm sync in FileZilla
  • Restart the process in PM2
pm2 restart telegram-bot

For troubleshooting, use:

  • pm2 logs — view live logs
  • pm2 monit — monitor resources
  • pm2 resurrect — recover after crashes

Key points

  • Always use .env for storing tokens
  • Set up automatic backups every 24 hours
  • Test updates in an isolated branch
  • Monitor memory usage with htop
  • Regularly update system packages

Performance Optimization

For high-load bots (over 1000 active users), recommended steps:

  • Setting up a swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
  • Limiting PM2 resources

In ecosystem.config.js:

module.exports = {
  apps: [{
    name: 'telegram-bot',
    script: 'main.py',
    instances: 1,
    autorestart: true,
    max_memory_restart: '300M'
  }]
}
  • Firewall setup
sudo ufw allow OpenSSH
sudo ufw allow 22/tcp
sudo ufw enable

A common newbie mistake is running the bot without daemonization. Closing the SSH session kills the process. PM2 solves this, but it requires proper autostart configuration:

pm2 startup
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u root --hp /root

For availability monitoring, add a simple health-check endpoint to your bot code:

from aiogram import Router

router = Router()

@router.message(Command('health'))
async def health_check(message: Message):
    await message.answer('Bot is running')

Regularly check logs for Telegram API connection errors. Typical issues:

  • Invalid token in .env
  • IP address blocked
  • Rate limits exceeded
  • Webhook handling errors

When using webhooks, set up Nginx as a reverse proxy with an SSL certificate. For low-load bots, polling mode is enough and requires no extra infrastructure.

— Editorial Team

Advertisement 728x90

Read Next