10 分钟在 VPS 上部署 Telegram Bot(2026 指南)

分步教程:在 VPS 上部署 Python Telegram Bot,并用 systemd 自动重启。真实命令,适用于任何 Ubuntu VPS。12 €/月起。

适用于任何 Ubuntu 22.04/24.04 VPS。总费用:每月 €12 起:运行机器人只需入门套餐。

1. 获取服务器

在套餐页面下单任意套餐:运行机器人,Core VPS 4(12 欧元)就够了。IP、登录名和密码通常几分钟内通过邮件发给您。

2. 连接并准备环境

ssh root@YOUR_SERVER_IP
apt update && apt upgrade -y
apt install -y python3-pip python3-venv

3. 机器人代码

mkdir -p /opt/mybot && cd /opt/mybot
python3 -m venv venv
source venv/bin/activate
pip install "python-telegram-bot==21.*"
cat > bot.py <<'EOF'
from telegram.ext import ApplicationBuilder, CommandHandler
async def start(update, context):
    await update.message.reply_text("Hello from Buran!")
app = ApplicationBuilder().token("YOUR_BOT_TOKEN").build()
app.add_handler(CommandHandler("start", start))
app.run_polling()
EOF

4. Systemd:让机器人重启后自动运行

cat > /etc/systemd/system/mybot.service <<'EOF'
[Unit]
Description=My Telegram Bot
After=network-online.target
[Service]
WorkingDirectory=/opt/mybot
ExecStart=/opt/mybot/venv/bin/python bot.py
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mybot
systemctl status mybot

完成。机器人在崩溃或服务器重启后会自动重新运行。以后需要升级?查看全部套餐。

相关内容

查看套餐指南常见问题