Recommended OS: Ubuntu 22.04 LTS (or Debian 12). These steps assume a fresh Ubuntu/Debian server.
- Your server’s IP and root (or sudo) SSH access.
- Your Discord Bot Token from the Discord Developer Portal.
- Optional: a Git repository URL with your bot’s code.
Table of contents
- 1) Connect to the VPS
- 2) Install Python, pip, Git, and tools
- 3) (Recommended) Create a dedicated user
- 4) Upload or clone your bot code
- 5) Create a virtual environment & install dependencies
- 6) Set your Discord token securely
- 7) Test-run the bot
- 8) Run 24/7 with systemd (auto-start & auto-restart)
- 9) Alternative: keep it running with
screen
- 10) Firewall & networking notes
- Troubleshooting
- Security checklist
1) Connect to the VPS
# From macOS/Linux:
ssh root@<SERVER_IP>
# From Windows:
# Use PowerShell (OpenSSH) or PuTTY to connect to: root@<SERVER_IP>
2) Install Python, pip, Git, and tools
sudo apt update
sudo apt install -y python3 python3-pip python3-venv git
3) (Recommended) Create a dedicated user
Run your bot under a non-root account for better security.
sudo adduser --disabled-password --gecos "" bot
# Give "bot" permission to restart the service, etc.
sudo usermod -aG sudo bot
# Switch to the new user
sudo -iu bot
4) Upload or clone your bot code
Option A: Git clone
git clone https://github.com/<username>/<repo>.git ~/discord-bot
cd ~/discord-bot
Option B: Upload from your computer
- Linux/macOS (SCP):
scp -r /path/to/local/bot/ bot@<SERVER_IP>:/home/bot/discord-bot
- Windows: Use WinSCP or FileZilla and connect via SFTP to upload into
/home/bot/discord-bot
.
5) Create a virtual environment & install dependencies
cd ~/discord-bot
python3 -m venv venv
source venv/bin/activate
# If your project has a requirements.txt:
pip install --upgrade pip
pip install -r requirements.txt
# Otherwise install your libs, e.g.
# pip install discord.py python-dotenv
6) Set your Discord token securely
Recommended: use an environment file readable by root and your service, not hard-coded in code.
# Exit back to root to create a global env file
exit
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 to the bot user to continue work if needed
sudo -iu bot
In your Python code, read it with:
import os
TOKEN = os.getenv("DISCORD_TOKEN")
# pass TOKEN to your discord client/login
7) Test-run the bot
cd ~/discord-bot
source venv/bin/activate
python3 bot.py
# Confirm it logs in and responds. Press Ctrl+C to stop.
8) Run 24/7 with systemd (auto-start & auto-restart)
Create a service that starts on boot and restarts on failure.
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
# Security hardening (optional but recommended)
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now discord-bot
sudo systemctl status discord-bot --no-pager
View logs (very useful for debugging):
journalctl -u discord-bot -e --no-pager
# Follow live logs:
journalctl -u discord-bot -f
9) Alternative: keep it running with screen
If you prefer a quick, non-service approach:
sudo apt install -y screen
sudo -iu bot
cd ~/discord-bot && source venv/bin/activate
screen -S discordbot
python3 bot.py
# Press Ctrl+A then D to detach the session.
# Reattach later with:
screen -r discordbot
10) Firewall & networking notes
- By default, you typically do not need any inbound ports for a basic Discord bot (it connects outbound to Discord).
- If your bot also hosts a web dashboard or webhook, open the required ports (e.g., 80/443).
OS firewall (UFW):
sudo ufw status
# If you enabled UFW and you host a web UI:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Troubleshooting
- Service fails to start: Check logs with
journalctl -u discord-bot -e
. Verify paths inExecStart
andWorkingDirectory
. - Invalid token / cannot login: Ensure
DISCORD_TOKEN
is correct in/etc/discord-bot.env
. Runsudo systemctl restart discord-bot
after changes. - Missing intents: If your bot needs privileged intents (Members/Presence), enable them in the Discord Developer Portal and update your code accordingly.
- Dependencies fail to build: Install build tools:
sudo apt install -y build-essential
and retrypip install -r requirements.txt
. - Time/SSL issues: Ensure system time is correct (
timedatectl
) so TLS handshakes don’t fail.
Security checklist
- Run the bot as a non-root user (like
bot
). - Store secrets in environment files, not in source code or Git.
- Keep the OS and packages up to date:
sudo apt update && sudo apt upgrade -y
. - Use systemd for automatic restarts and centralized logging.
- Open only the ports you actually need.
That’s it — your Python Discord bot should now run 24/7 and survive restarts.