How to Host a Discord Bot 24/7 on a Cloud Server
Your Discord bot works great on your computer — but it goes offline whenever you close your laptop. Here's how to deploy it to a cloud server so it stays online 24/7, automatically restarts after crashes, and runs without your computer being on.
Why You Need a Cloud Server for Your Bot
Running a Discord bot on your personal computer has major limitations:
- It goes offline when your computer sleeps or shuts down — Your server members can't use the bot while you're away
- Your home internet isn't reliable enough — Power outages, router restarts, and ISP issues cause downtime
- It uses your computer's resources — RAM and CPU that could be used for other tasks
- No automatic crash recovery — If the bot crashes at 3 AM, nobody restarts it until morning
A cloud server solves all these problems. It's always on, always connected, and can automatically restart your bot if it crashes.
Option 1: Managed Bot Hosting (Easiest)
The simplest approach is using a dedicated bot hosting service with a game panel like Pterodactyl. This is ideal if you don't want to manage a server or learn Linux commands.
How It Works on Azion Cloud
- Create a bot hosting service — Select the Bot Hosting plan from our hosting page
- Choose your runtime — Select the egg matching your bot's language:
- Node.js — For discord.js bots
- Python — For discord.py or py-cord bots
- Java — For JDA bots
- Generic — For custom runtimes
- Upload your bot files — Use the built-in file manager or SFTP to upload your bot's code
- Set environment variables — Add your bot token and any API keys in the Startup tab
- Start the bot — Click the Start button. The panel handles crash recovery automatically.
That's it. No SSH, no Linux commands, no process managers. The panel monitors your bot and restarts it if it crashes.
Option 2: VPS with PM2 (Node.js Bots)
If you're running a Node.js bot and want full control, deploy to a VPS using PM2 — a production process manager that handles auto-restart, logging, and monitoring.
Step 1: Connect to Your VPS
ssh root@your-server-ip
Step 2: Install Node.js
# Install Node.js 20 LTS
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
# Verify installation
node --version
npm --version
Step 3: Upload and Install Your Bot
# Create directory for your bot
mkdir -p /opt/discord-bot
cd /opt/discord-bot
# Upload your bot files via SFTP, then install dependencies
npm install
Step 4: Set Up PM2
# Install PM2 globally
npm install -g pm2
# Start your bot with PM2
pm2 start index.js --name "my-discord-bot"
# Enable auto-restart on server reboot
pm2 startup
pm2 save
# View bot status
pm2 status
# View bot logs
pm2 logs my-discord-bot
PM2 provides:
- Auto-restart on crash — If your bot throws an unhandled error and exits, PM2 restarts it within seconds
- Auto-start on boot — If the VPS reboots (updates, maintenance), your bot starts automatically
- Log management — Saves stdout and stderr to log files for debugging
- Monitoring —
pm2 monitshows real-time CPU and memory usage
Option 3: VPS with systemd (Python Bots)
For Python bots, systemd is the standard Linux process manager. It's built into every modern Linux distribution.
Step 1: Install Python and Dependencies
sudo apt update
sudo apt install python3 python3-pip python3-venv -y
# Create project directory
mkdir -p /opt/discord-bot
cd /opt/discord-bot
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install your bot's dependencies
pip install discord.py
# or: pip install -r requirements.txt
Step 2: Create a systemd Service
sudo nano /etc/systemd/system/discord-bot.service
Paste this configuration:
[Unit]
Description=My Discord Bot
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/opt/discord-bot
ExecStart=/opt/discord-bot/venv/bin/python3 bot.py
Restart=always
RestartSec=10
Environment=DISCORD_TOKEN=your-token-here
[Install]
WantedBy=multi-user.target
Step 3: Enable and Start
# Reload systemd to pick up the new service
sudo systemctl daemon-reload
# Start the bot
sudo systemctl start discord-bot
# Enable auto-start on boot
sudo systemctl enable discord-bot
# Check status
sudo systemctl status discord-bot
# View logs
journalctl -u discord-bot -f
The Restart=always directive tells systemd to restart your bot whenever it exits, with a 10-second delay between restarts. This handles crashes, unhandled exceptions, and any situation where the process stops.
Security Best Practices
Never Hardcode Your Token
Your Discord bot token is equivalent to a password. Never put it directly in your source code or commit it to Git. Use environment variables or a .env file:
# .env file (add to .gitignore!)
DISCORD_TOKEN=your-token-here
DATABASE_URL=mongodb://localhost:27017/mybot
In your bot code (Node.js):
require('dotenv').config();
client.login(process.env.DISCORD_TOKEN);
Use a Firewall
If using a VPS, configure a firewall to only allow necessary ports:
sudo ufw allow ssh
sudo ufw enable
Discord bots only make outgoing connections — they don't need any incoming ports open (except SSH for management).
Keep Dependencies Updated
Regularly update your bot's dependencies to patch security vulnerabilities:
# Node.js
npm update
npm audit fix
# Python
pip install --upgrade discord.py
Troubleshooting Common Issues
- Bot goes offline after a few hours: Check if you're handling the
disconnectandreconnectevents properly. discord.js and discord.py handle reconnection automatically in recent versions. - Token invalid error: Regenerate your token in the Discord Developer Portal and update it on your server.
- High memory usage: Check for memory leaks — common causes are unbounded caches, event listeners that aren't cleaned up, and large data collections stored in memory.
- Rate limiting: If your bot sends too many API requests, Discord will throttle it. Implement proper rate limit handling and avoid bulk operations.
Which Option Should You Choose?
Managed bot hosting — Best if you want simplicity. No Linux knowledge needed. Just upload and run.
VPS with PM2/systemd — Best if you want full control, need to run multiple bots, or want to host other services alongside your bot.
Azion Cloud offers both options. Our bot hosting provides a managed Pterodactyl panel experience, while our VPS plans give you full root access to set up PM2 or systemd yourself. Either way, your bot stays online 24/7.