Skip to content
Back to the blog

Run a Discord bot 24/7 on a VPS with Python

Keep a Discord bot online with Python and systemd: install dependencies, protect its token, review intents and check restarts and logs.

  • Discord bot hosting
  • Discord bot 24/7
  • Python VPS

When you run a bot on your computer, it disconnects when you close the program or turn the machine off. A VPS can keep the process running and start it again after a reboot; it does not configure the Discord bot or fix its errors for you. This guide uses Python with discord.py and systemd on Debian or Ubuntu.

A bot that connects through Discord’s Gateway normally starts an outbound connection and does not need an inbound HTTP port. If your project uses webhook interactions, a web dashboard, voice or other services, check those requirements separately. The exact setup depends on the generated code and permissions the bot needs.

1. Check the bot and its permissions

Find the entry point, requirements.txt or pyproject.toml, and the Python version the project expects. Test the bot locally and confirm its token is not hard-coded or committed. In Discord’s developer portal, create an application and bot account; use a bot token rather than automating a personal user account.

Enable only the intents and permissions required by each feature. For example, reading message content may require the corresponding privileged intent in both the developer portal and your code. Do not grant Administrator just to avoid reviewing permissions. Invite the bot with only the scopes and permissions it needs.

2. Prepare a VPS and Python environment

Choose a Hostbrid VPS with Debian or Ubuntu compatible with the project. Connect over SSH, apply updates and create a dedicated service account:

sudo apt update
sudo apt install git python3-venv
sudo adduser --system --group --home /srv/discord-bot discordbot

Clone the repository into /srv/discord-bot/current, using a read-only deploy key for a private repository. Install dependencies inside a virtual environment:

sudo -u discordbot python3 -m venv /srv/discord-bot/venv
sudo -u discordbot /srv/discord-bot/venv/bin/pip install -r /srv/discord-bot/current/requirements.txt

If your project uses Node.js and discord.js, do not run the Python commands: install a Node version compatible with the declared package and install the project dependencies with npm ci. systemd can run either Python or Node; choose the process that matches your repository.

3. Keep the token out of source code

Store the token in /etc/discord-bot.env, outside the repository and with restricted permissions. Python code should read it from the environment, for example with os.environ["DISCORD_TOKEN"], and pass it to the client without printing it. Do not put it in visible process arguments, Git, an issue or logs. If it leaks, regenerate it in Discord’s developer portal and update the service.

First run the bot manually as discordbot with the environment file loaded. Check its startup message and that it appears online in the expected server. Never publish the token in this guide, even as a complete example.

4. Start it with systemd

After the manual test works, create /etc/systemd/system/discord-bot.service:

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

[Service]
User=discordbot
Group=discordbot
WorkingDirectory=/srv/discord-bot/current
EnvironmentFile=/etc/discord-bot.env
ExecStart=/srv/discord-bot/venv/bin/python /srv/discord-bot/current/bot.py
Restart=on-failure
RestartSec=5
NoNewPrivileges=true
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Replace bot.py with the real entry point. For Node, change ExecStart to the absolute Node binary and entry file; verify the path for your installed version instead of relying on your interactive shell’s PATH. If the bot writes files or a SQLite database, define its data directory and permissions before adding write restrictions.

Enable the service and check its state:

sudo systemctl daemon-reload
sudo systemctl enable --now discord-bot
sudo systemctl status discord-bot
journalctl -u discord-bot -f

Restart=on-failure helps recover a crashed process; it does not fix an invalid token, code error or Discord-side limit. Watch logs and CPU/RAM before choosing a VPS size. Voice, AI models, multiple instances or database tasks can need more resources than a simple bot.

5. Update it and test recovery

Back up local state if the bot stores any. For an update, prepare the new code, install pinned dependencies and restart discord-bot; then confirm that it reconnects and commands still work. Do not run two copies with the same token unless the bot is designed for it.

The bot needs internet access from the VPS; a regular Gateway bot does not require Nginx, a domain or an inbound port. If it uses webhooks or a control website, that component needs its own HTTPS exposure and protection.

Hostbrid offers VPS with root access; this service is self-managed. You maintain Python or Node, the token, dependencies, permissions, updates and backups. Compare VPS options and current prices with the bot’s measured resource use.

Sources: official discord.py, intents, bot account and token, Discord privileged intents and discord.js.

Manage your sites, servers and email.

Forgot your password?

Free, and it takes a minute.

At least 8 characters. Not all digits, not a common one.

We'll email you to confirm the account.