> ## Documentation Index
> Fetch the complete documentation index at: https://help.freakhosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Host a Discord Bot 24/7 on a Linux VPS

> Host a Discord bot 24/7 on a VPS: copy your code, keep the token in an .env file and run the bot as a systemd service that restarts on crashes and at boot.

To host a Discord bot 24/7 on a Linux VPS, copy your bot's code to the server, install its runtime (for example Python with a virtual environment), store the bot token in an `.env` file, and run the bot as a systemd service with `Restart=on-failure`. systemd keeps it running after you close SSH, restarts it if it crashes and starts it at boot.

| Difficulty | Time | Works on | Needs |
| - | - | - | - |
| Beginner | 20 minutes | Ubuntu 24.04, Debian 13, AlmaLinux / Rocky 9 / 10 | Root or sudo, your bot's code and token |

## Before you start

* You need a bot application and token from the [Discord Developer Portal](https://discord.com/developers/applications), and working bot code on your PC.
* A Discord bot connects **out** to Discord. You don't need to open any inbound ports for it.
* The examples use Python and [discord.py](https://discordpy.readthedocs.io/), which needs Python 3.8 or newer. For a Node.js bot (discord.js), see the Node.js section further down.

> [!WARNING]
>
> Your bot token is a password. Never put it in your code, in Git or in a screenshot. If it leaks, reset it in the Developer Portal straight away.

## How to set up the bot on your VPS

**1. Create a user for the bot**

```bash theme={null}
sudo useradd -m -d /opt/discordbot -s /bin/bash discordbot
```

Running the bot as its own user limits the damage if the bot is ever compromised.

**2. Install Python and venv**

> [tab] Ubuntu / Debian
>
> ```bash theme={null}
> sudo apt update
> sudo apt install python3 python3-venv python3-pip git
> ```

> [tab] AlmaLinux / Rocky
>
> ```bash theme={null}
> sudo dnf install python3 python3-pip git
> ```

**3. Copy your code to the server**

Switch to the bot user and clone your repository, or upload the files with [SFTP](https://help.freakhosting.com/portals/other/how-to-transfer-files-with-sftp-winscp-and-filezilla.md):

```bash theme={null}
sudo -iu discordbot
git clone https://github.com/example/my-bot.git app
cd app
```

**4. Create a virtual environment and install packages**

```bash theme={null}
python3 -m venv venv
./venv/bin/pip install -U discord.py
```

If your project has a `requirements.txt`, run `./venv/bin/pip install -r requirements.txt` instead.

**5. Store the token in .env**

```bash theme={null}
nano .env
```

```ini theme={null}
DISCORD_TOKEN=paste-your-bot-token-here
```

```bash theme={null}
chmod 600 .env
```

In your code, read it with `os.environ["DISCORD_TOKEN"]`. A minimal `bot.py` looks like this:

```python theme={null}
import os
import discord

intents = discord.Intents.default()
client = discord.Client(intents=intents)


@client.event
async def on_ready():
    print(f"Logged in as {client.user}")


client.run(os.environ["DISCORD_TOKEN"])
```

**6. Test it once**

```bash theme={null}
set -a; . ./.env; set +a
./venv/bin/python bot.py
```

When it prints `Logged in as …`, stop it with **Ctrl+C** and type `exit` to return to your sudo user.

## How to run the bot 24/7 with systemd

**1. Create the service file**

```bash theme={null}
sudo nano /etc/systemd/system/discord-bot.service
```

```ini theme={null}
[Unit]
Description=Discord bot
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=discordbot
WorkingDirectory=/opt/discordbot/app
EnvironmentFile=/opt/discordbot/app/.env
ExecStart=/opt/discordbot/app/venv/bin/python bot.py
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
```

**2. Start it and enable it at boot**

```bash theme={null}
sudo systemctl daemon-reload
sudo systemctl enable --now discord-bot
```

## How to check it worked

```bash theme={null}
sudo systemctl status discord-bot --no-pager
sudo journalctl -u discord-bot -f
```

The status shows `active (running)`, and the log shows `Logged in as …`. Your bot is online in Discord. Press **Ctrl+C** to stop following the log.

## How to update the bot

```bash theme={null}
sudo -iu discordbot
cd app
git pull
./venv/bin/pip install -r requirements.txt
exit
sudo systemctl restart discord-bot
```

## How to run a Node.js bot

For discord.js and other Node.js bots:

1. Install the Node.js version your library requires. discord.js lists it on [discord.js.org](https://discord.js.org/). See [install Node.js and run apps with PM2](https://help.freakhosting.com/portals/other/how-to-install-nodejs-and-run-apps-with-pm2.md).
2. Run `npm ci` (or `npm install`) in the bot folder.
3. Use the systemd service above with `ExecStart=/usr/bin/node index.js`, or keep the bot running with PM2.

Check the real path to `node` with `which node`, because installs through nvm put it elsewhere.

## Frequently asked questions

### Do I need to open a port for my Discord bot?

No. The bot opens an outgoing connection to Discord's gateway. You only need an open port if your bot also runs a web server, for example for a dashboard or webhooks.

### Why does my bot stop when I close PuTTY?

Programs started in an SSH session end when the session closes. Run the bot as a systemd service (above), or at least inside [tmux](https://help.freakhosting.com/portals/other/how-to-keep-ssh-sessions-running-with-tmux-and-screen.md).

### How much RAM does a Discord bot need?

It depends on the library, the number of servers the bot is in and features such as music or caching. Check real use with `systemctl status discord-bot`, which shows the service's memory use, and see [check memory usage](https://help.freakhosting.com/portals/other/how-to-check-memory-usage-on-linux.md).

### Can I run the bot in Docker instead?

Yes. Build an image with your code, pass the token with `--env-file .env` and start it with `--restart unless-stopped`. See [essential Docker commands](https://help.freakhosting.com/portals/other/essential-docker-commands.md).

## Related guides

> [card] [VPS and dedicated server guides](https://help.freakhosting.com/portals/other/vps-and-dedicated-server-guides.md)
>
> Every guide in this section.

> [card] [Create a systemd service](https://help.freakhosting.com/portals/other/how-to-create-a-systemd-service.md)
>
> Every option in a unit file.

> [card] [Read logs with journalctl](https://help.freakhosting.com/portals/other/how-to-read-logs-with-journalctl.md)
>
> Find out why the bot crashed.

> [card] [Node.js and PM2](https://help.freakhosting.com/portals/other/how-to-install-nodejs-and-run-apps-with-pm2.md)
>
> For discord.js bots.

> [card] [Deploy a Python app with Gunicorn](https://help.freakhosting.com/portals/other/how-to-deploy-a-python-app-with-gunicorn.md)
>
> If your bot has a web dashboard.

***

> [!NOTE]
>
> **Checked against the official [discord.py documentation](https://discordpy.readthedocs.io/en/stable/intro.html)** on 4 November 2026; virtual environment, discord.py 2.7.1 install, the example bot and the systemd unit syntax tested on Ubuntu 24.04 LTS. Commands can differ on other distributions or releases.

> [!NOTE]
>
> ### Need Extra Help?
>
> If you encounter any issues, our support team is ready to assist:
>
> * **Live Chat:** Quick assistance via our website.
> * **Support Ticket:** [Open a Ticket](https://freakhosting.com/clientarea/submitticket.php)
> * **Discord:** [Join our Community](https://discord.gg/freakhosting)
> * **Email:** [support@freakhosting.com](mailto:support@freakhosting.com)

> [!TIP]
>
> ### Save on Your Hosting
>
> Ready to upgrade your hosting? Use code **KB20** at checkout for **20% off** your first order!

***

**Last Updated:** November 2026


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.