How to deploy a persistent Python bot on a Linux VPS using a dedicated user, virtual environment, systemd, logs and a firewall.
In plain English
This guide assumes a long-running service on a Linux VPS. Your home PC can be off, but the VPS still needs proper service management, updates, logs and secrets.
Keep the server boring
For a small bot, reliability usually improves when the server has fewer moving parts. A common stack is Ubuntu, one dedicated application directory, one Python virtual environment, systemd, and either SQLite or a managed database.
Suggested layout
/opt/bot/
app/
main.py
broker.py
strategy.py
data/
bot.db
logs/
venv/
.env
Install into a virtual environment
python3 -m venv /opt/bot/venv
/opt/bot/venv/bin/pip install -r /opt/bot/app/requirements.txt
A virtual environment avoids mixing bot dependencies with the operating system's Python packages.
Run under systemd
Do not rely on an SSH terminal staying open. systemd can start the process at boot, restart it after a crash, and put stdout/stderr into the journal.
[Unit]
Description=Bot service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=bot
WorkingDirectory=/opt/bot/app
EnvironmentFile=/opt/bot/.env
ExecStart=/opt/bot/venv/bin/python /opt/bot/app/main.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Operations commands
sudo systemctl daemon-reload
sudo systemctl enable --now bot.service
sudo systemctl status bot.service
journalctl -u bot.service -n 100 --no-pager
journalctl -u bot.service -f
Firewall
If the bot does not need a public web server, do not expose one. If it does, put HTTPS/reverse proxy or Cloudflare in front, and bind internal services to localhost or a private interface when possible.
Before you rely on this in production
- Service starts after reboot.
- Firewall exposes only required ports.
- Secrets have restricted permissions.
- Logs and disk usage are monitored.

