{"id":22588,"date":"2026-11-28T12:03:06","date_gmt":"2026-11-28T09:03:06","guid":{"rendered":"https:\/\/www.hyyat.com\/knowledgebase\/run-telegram-bot-on-vps\/"},"modified":"2026-11-28T12:03:10","modified_gmt":"2026-11-28T09:03:10","password":"","slug":"run-telegram-bot-on-vps","status":"publish","type":"docs","link":"https:\/\/www.hyyat.com\/en\/knowledgebase\/run-telegram-bot-on-vps\/","title":{"rendered":"Run a Telegram Bot 24\/7 on a VPS: Python and systemd Step by Step"},"content":{"rendered":"<div class=\"hkb\" dir=\"ltr\">\n<div class=\"hkb-tldr\"><b>In short<\/b>A bot on your own computer stops when you switch it off. On a VPS it runs <b>24\/7<\/b>. Put the bot in a folder with a Python virtual environment, keep the <b>token<\/b> in a protected file, then create a <b>systemd service<\/b> that starts it on boot and restarts it if it stops. A <b>2GB<\/b> plan is enough for most bots.<\/div>\n<figure class=\"hkb-fig\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.hyyat.com\/zasogooh\/2026\/11\/hkb-vu-hero-vu-tgbot-en.webp\" alt=\"Running a Telegram bot on a VPS\" width=\"1200\" height=\"630\" loading=\"lazy\" \/><\/figure>\n<p>Telegram bots take orders, answer customers and send alerts. A common problem is running the bot on a personal computer, so it stops when the internet drops or the machine is turned off. A server fixes that: it is always online, and systemd restarts the bot if it crashes.<\/p>\n<h2>Choosing resources<\/h2>\n<div class=\"hkb-tbl\">\n<table>\n<thead>\n<tr>\n<th>Use<\/th>\n<th>Suitable plan<\/th>\n<th>Why<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>A reply or alert bot<\/td>\n<td>VPS 2GB<\/td>\n<td>A light Python bot usually uses under 200 MB<\/td>\n<\/tr>\n<tr>\n<td>Several bots or a database<\/td>\n<td>VPS 4GB<\/td>\n<td>Room for several services and MySQL or PostgreSQL<\/td>\n<\/tr>\n<tr>\n<td>A bot using local AI models<\/td>\n<td>VPS 16GB<\/td>\n<td>Models need a lot of memory<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2>1. Prepare the server and a user<\/h2>\n<p>Do not run the bot as root. Create a dedicated user:<\/p>\n<pre class=\"hkb-code\">apt update &amp;&amp; apt install -y python3 python3-venv\nadduser --system --group --home \/opt\/mybot mybot<\/pre>\n<h2>2. Copy the code and install libraries<\/h2>\n<pre class=\"hkb-code\">cd \/opt\/mybot\npython3 -m venv venv\n.\/venv\/bin\/pip install python-telegram-bot\n# copy bot.py into \/opt\/mybot<\/pre>\n<h2>3. Store the token safely<\/h2>\n<p>Never hard-code the token. Put it in an environment file only the bot user can read:<\/p>\n<pre class=\"hkb-code\">echo &quot;BOT_TOKEN=your-token-here&quot; &gt; \/opt\/mybot\/.env\nchown -R mybot:mybot \/opt\/mybot\nchmod 600 \/opt\/mybot\/.env<\/pre>\n<p>In your code, read it with <code>os.environ[\"BOT_TOKEN\"]<\/code>.<\/p>\n<h2>4. Create a systemd service<\/h2>\n<p>File <code>\/etc\/systemd\/system\/mybot.service<\/code>:<\/p>\n<pre class=\"hkb-code\">[Unit]\nDescription=Telegram bot\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nUser=mybot\nWorkingDirectory=\/opt\/mybot\nEnvironmentFile=\/opt\/mybot\/.env\nExecStart=\/opt\/mybot\/venv\/bin\/python bot.py\nRestart=always\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n<pre class=\"hkb-code\">systemctl daemon-reload\nsystemctl enable --now mybot\nsystemctl status mybot\njournalctl -u mybot -f<\/pre>\n<p>You can generate the service file with your own options using our <a href=\"https:\/\/www.hyyat.com\/en\/systemd-service-generator\/\">systemd service file generator<\/a>.<\/p>\n<h2>Polling or webhook?<\/h2>\n<div class=\"hkb-tbl\">\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>Polling<\/th>\n<th>Webhook<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>How it works<\/td>\n<td>The bot keeps asking Telegram<\/td>\n<td>Telegram sends updates to your URL<\/td>\n<\/tr>\n<tr>\n<td>Needs a domain and HTTPS<\/td>\n<td>No<\/td>\n<td>Yes<\/td>\n<\/tr>\n<tr>\n<td>Suits<\/td>\n<td>Most bots and new projects<\/td>\n<td>High-traffic bots<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div class=\"hkb-note warn\"><b>A mistake to avoid:<\/b> Pushing the token file to GitHub with your code. Whoever gets the token fully controls the bot. If that happens, create a new token in BotFather at once.<\/div>\n<div class=\"hkb-note\"><b>At Hyyat Host:<\/b> A VPS keeps your bot running around the clock: <a href=\"https:\/\/www.hyyat.com\/en\/vps-hosting\/\">VPS plans<\/a>, with our own control panel for power, console and snapshots, and Arabic and English support.<\/div>\n<h2>Frequently asked questions<\/h2>\n<div class=\"hkb-faq\">\n<details>\n<summary>Can I run the bot on shared hosting?<\/summary>\n<p>Usually not, because shared hosting does not allow programs that run constantly in the background. A permanent bot needs a VPS.<\/p>\n<\/details>\n<details>\n<summary>How do I update the bot code?<\/summary>\n<p>Copy the new files into the bot folder, then run <code>systemctl restart mybot<\/code>.<\/p>\n<\/details>\n<details>\n<summary>Can I run more than one bot?<\/summary>\n<p>Yes. Create a separate folder and a systemd service with a different name for each bot.<\/p>\n<\/details>\n<\/div>\n<div class=\"hkb-rel\"><b>Related guides<\/b><\/p>\n<ul>\n<li><a href=\"https:\/\/www.hyyat.com\/en\/systemd-service-generator\/\">systemd Service File Generator<\/a><\/li>\n<li><a href=\"https:\/\/www.hyyat.com\/en\/vps-setup-script\/\">New VPS Setup Commands, Step by Step<\/a><\/li>\n<li><a href=\"https:\/\/www.hyyat.com\/en\/knowledgebase\/vps-firewall-manage-open-ports\/\">Firewall: Control the Open Ports on Your VPS<\/a><\/li>\n<li><a href=\"https:\/\/www.hyyat.com\/en\/knowledgebase\/when-to-move-from-shared-hosting-to-vps\/\">When Should You Move from Shared Hosting to a VPS? 7 Clear Signs<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.hyyat.com\/en\/kb\/servers-without-control-panel\/\">All guides for servers without a panel \u2192<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In shortA bot on your own computer stops when you switch it off. On a VPS it runs 24\/7. Put the bot in a folder with a Python virtual environment, keep the token in a protected file, then create a systemd service that starts it on boot and restarts it if it stops. A 2GB [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":22575,"comment_status":"open","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"_templately_pack_id":"","_templately_imported_at":"","_templately_source":"","_templately_import_session_id":"","footnotes":""},"doc_category":[2733],"doc_tag":[],"class_list":["post-22588","docs","type-docs","status-publish","has-post-thumbnail","hentry","doc_category-servers-without-control-panel"],"year_month":"2026-11","word_count":515,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"admin","author_nicename":"admin","author_url":"https:\/\/www.hyyat.com\/en\/author\/admin\/"},"doc_category_info":[{"term_name":"Servers Without a Control Panel","term_url":"https:\/\/www.hyyat.com\/en\/kb\/servers-without-control-panel\/"}],"doc_tag_info":[],"knowledge_base_info":[],"knowledge_base_slug":[],"_links":{"self":[{"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/docs\/22588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/comments?post=22588"}],"version-history":[{"count":0,"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/docs\/22588\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/media\/22575"}],"wp:attachment":[{"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/media?parent=22588"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/doc_category?post=22588"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/www.hyyat.com\/en\/wp-json\/wp\/v2\/doc_tag?post=22588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}