
This article provides a guide demonstrating how to host a Discord bot on Ubuntu VPS.
Running your Discord bot on a VPS (Virtual Private Server) gives you full control, better uptime, and the ability to keep your bot online 24/7 without relying on your local computer.
This guide walks you through the steps to host a Discord bot on Ubuntu VPS using Node.js or Python, securing the server, and keeping the bot running permanently with PM2 or systemd.
Requirements
Before you are able to host a Discord bot on Ubuntu VPS, you’ll need:
- An Ubuntu VPS (Ubuntu 22.04 recommended)
- SSH access to the server
- A Discord bot token
- Your bot source code
- Basic terminal knowledge
Popular VPS providers include:
- DigitalOcean
- Vultr
- Linode
- AWS Lightsail
- Hetzner
- Rad Web Hosting VPS
Compare Ubuntu VPS Plans
How to Host a Discord Bot on Ubuntu VPS
To host a Discord bot on Ubuntu VPS, follow the steps below:
-
Connect to Your Ubuntu VPS
Use SSH to connect to the server from your local computer.
ssh root@YOUR_SERVER_IP
Example:
ssh root@192.168.1.10
If this is your first login, confirm the fingerprint by typing:
yes
-
Update the Server
Always update package lists and installed packages first.
apt update && apt upgrade -y
Optional but recommended:
apt install curl wget git unzip -y
-
Create a Non-Root User (Recommended)
Running applications as root is unsafe.
Create a new user:
adduser discordbot
Add the user to the sudo group:
usermod -aG sudo discordbot
Switch to the new user:
su - discordbot
-
Install Node.js or Python
Choose the section based on your bot language.
-
Install Node.js (Discord.js Bots)
Install Node.js LTS:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt install nodejs -y
Verify installation:
node -v npm -v
-
Install Python (discord.py Bots)
Install Python and pip:
sudo apt install python3 python3-pip python3-venv -y
Verify installation:
python3 --version pip3 --version
-
-
Upload Your Discord Bot Files
There are several ways to upload your bot.
-
Git Clone (Recommended)
If your bot is on GitHub:
git clone https://github.com/USERNAME/BOT-REPO.git
Enter the folder:
cd BOT-REPO
-
SCP Upload
From your local computer:
scp -r ./mybot discordbot@YOUR_SERVER_IP:/home/discordbot/
-
SFTP Client
Use an SFTP client like:
Upload files into:
/home/discordbot/
-
-
Install Dependencies
-
Node.js Bots
Inside your bot directory:
npm install
-
Python Bots
Create a virtual environment:
python3 -m venv venv
Activate it:
source venv/bin/activate
Install dependencies:
pip install -r requirements.txt
-
-
Configure Environment Variables
Never hardcode your Discord token directly into source code.
Create a
.envfile:nano .env
Example:
DISCORD_TOKEN=your_bot_token_here
Save and exit:
- CTRL + X
- Y
- ENTER
-
Test the Bot
-
Node.js
node index.js
Or:
npm start
-
Python
python3 bot.py
If everything is correct, your bot should appear online in Discord.
Stop the bot with:
CTRL + C
-
-
Keep the Bot Running 24/7
If you close the terminal, the bot will stop unless you use a process manager.
The best options are:
- PM2
- systemd
-
Using PM2 (Recommended for Beginners)
Install PM2 globally:
sudo npm install -g pm2
Start a Node.js Bot
pm2 start index.js --name discord-bot
Start a Python Bot
pm2 start bot.py --interpreter python3 --name discord-bot
View Running Processes
pm2 list
View Logs
pm2 logs discord-bot
Restart Bot
pm2 restart discord-bot
Stop Bot
pm2 stop discord-bot
Enable Auto-Start on Reboot
pm2 startup
Copy and run the command PM2 provides.
Then save processes:
pm2 save
Your bot will now automatically restart after VPS reboots.
-
Using systemd (Advanced)
Create a service file:
sudo nano /etc/systemd/system/discordbot.service
Example service configuration:
[Unit] Description=Discord Bot After=network.target [Service] Type=simple User=discordbot WorkingDirectory=/home/discordbot/mybot ExecStart=/usr/bin/node index.js Restart=always [Install] WantedBy=multi-user.target
Reload systemd:
sudo systemctl daemon-reload
Enable service:
sudo systemctl enable discordbot
Start service:
sudo systemctl start discordbot
Check status:
sudo systemctl status discordbot
-
Secure Your VPS
Security is important when hosting public bots.
Enable Firewall
Install UFW:
sudo apt install ufw -y
Allow SSH:
sudo ufw allow OpenSSH
Enable firewall:
sudo ufw enable
Check status:
sudo ufw status
Disable Root SSH Login
Edit SSH configuration:
sudo nano /etc/ssh/sshd_config
Find:
PermitRootLogin yes
Change to:
PermitRootLogin no
Restart SSH:
sudo systemctl restart ssh
Use SSH Keys Instead of Passwords
Generate SSH keys locally:
ssh-keygen
Copy key to server:
ssh-copy-id discordbot@YOUR_SERVER_IP
-
Optional Enhancements
Install Fail2Ban
Protect against brute-force attacks:
sudo apt install fail2ban -y
Enable and start:
sudo systemctl enable fail2ban sudo systemctl start fail2ban
Monitor Resource Usage
Install htop:
sudo apt install htop -y
Run:
htop
Auto-Deploy with GitHub
You can automate deployments using:
- GitHub Actions
- Webhooks
- CI/CD pipelines
Example deployment command:
git pull origin main npm install pm2 restart discord-bot
Troubleshooting
Bot Offline
Check logs:
pm2 logs
Or:
journalctl -u discordbot -f
Permission Errors
Fix ownership:
sudo chown -R discordbot:discordbot /home/discordbot/
Missing Dependencies
Reinstall dependencies:
npm install
Or:
pip install -r requirements.txt
Port Already in Use
Find process:
sudo lsof -i :PORT
Kill process:
kill -9 PID
Final Thoughts
Hosting your Discord bot on an Ubuntu VPS gives you:
- 24/7 uptime
- Better performance
- Full control
- Improved reliability
- Easier scaling
For most users, the ideal setup is:
- Ubuntu 22.04
- Node.js LTS or Python 3
- PM2 process manager
- UFW firewall
- SSH key authentication
Once configured properly, your Discord bot can run continuously with minimal maintenance.
Conclusion
You now know how to host a Discord bot on Ubuntu VPS.
Happy bot hosting!









