Skip to main content

Deploy a Python Discord bot on your VPS

Learn how to upload or clone your Discord bot code, install dependencies, set environment variables, and keep it running 24/7 with systemd.

Andy Wallace avatar
Written by Andy Wallace
Updated over a month ago

This guide explains how to deploy an existing Python Discord bot on a LumaDock VPS running Ubuntu or Debian. You will upload your bot code, install dependencies inside a virtual environment, configure your token securely, and run the bot with systemd so it stays online.


Before you begin

You will need:

  • SSH access to your VPS

  • Your Discord bot token

  • Your bot project folder or Git repository

Recommended OS: Ubuntu 22.04 LTS or Debian 12.


1) Connect to the VPS

From macOS or Linux:

ssh root@SERVER_IP

From Windows, use PowerShell or PuTTY and connect to:

root@SERVER_IP


2) Install Python, pip, Git, and required tools

sudo apt update sudo apt install -y python3 python3-pip python3-venv git


3) Create a dedicated user

sudo adduser --disabled-password --gecos "" bot

sudo usermod -aG sudo bot

Switch to the new user:

sudo -iu bot


4) Upload or clone your bot code

You can clone a repository:

git clone https://github.com/USERNAME/REPO.git ~/discord-bot cd ~/discord-bot

Or upload files via SFTP/WinSCP into:

/home/bot/discord-bot


5) Create a virtual environment and install dependencies

cd ~/discord-bot

python3 -m venv venv

source venv/bin/activate

pip install --upgrade

pip pip install -r requirements.txt

If you do not have a requirements file, install the necessary packages manually.


6) Set your Discord token securely

Exit to root:

exit

Create a secure environment file:

echo 'DISCORD_TOKEN=PUT_YOUR_TOKEN_HERE' | sudo tee /etc/discord-bot.env > /dev/null 

sudo chmod 600 /etc/discord-bot.env

sudo chown root:root /etc/discord-bot.env

Switch back if needed:

sudo -iu bot

Load the token in your Python code:

import os TOKEN = os.getenv("DISCORD_TOKEN")


7) Test-run the bot

cd ~/discord-bot source venv/bin/activate python3 bot.py

If it logs in normally, stop it with Ctrl+C.


8) Run 24/7 with systemd

Create the service:

sudo tee /etc/systemd/system/discord-bot.service > /dev/null <<'EOF'
[Unit]
Description=Discord Bot (Python)
After=network.target

[Service]
Type=simple
User=bot
WorkingDirectory=/home/bot/discord-bot
EnvironmentFile=/etc/discord-bot.env
ExecStart=/home/bot/discord-bot/venv/bin/python3 /home/bot/discord-bot/bot.py
Restart=always
RestartSec=5

NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true

[Install] WantedBy=multi-user.target
EOF

Reload and start:

sudo systemctl daemon-reload

sudo systemctl enable --now discord-bot

sudo systemctl status discord-bot --no-pager

View logs:

journalctl -u discord-bot -e --no-pager


9) Alternative: run with screen

sudo apt install -y screen 

sudo -iu bot

cd ~/discord-bot

source venv/bin/activate

screen -S discordbot python3 bot.py

Detach: Ctrl+A then D


Reattach:

screen -r discordbot


10) Firewall and networking notes

Most Discord bots do not need inbound ports.


If yours exposes a dashboard or webhook:

sudo ufw allow 80/tcp 

sudo ufw allow 443/tcp

If you enable the cloud firewall, make sure SSH (22) and any required bot ports are allowed.


Troubleshooting

  • Bot does not start: check logs with journalctl -u discord-bot.

  • Invalid token: correct /etc/discord-bot.env and restart the service.

  • Missing intents: enable required intents in the Developer Portal.

  • Dependency build errors: install build tools with sudo apt install -y build-essential.

  • SSL/time issues: verify system time with timedatectl.

Security checklist

  • Run the bot as a non-root user

  • Store secrets in environment files, never in source code

  • Keep Ubuntu/Debian updated

  • Use systemd for automatic restarts

  • Open only the ports your bot needs

Your Python Discord bot should now run continuously and restart automatically after crashes or reboots.

Did this answer your question?