Self-hosted deployment
For complete control over your bot's environment, you can self-host it on your own server. This guide shows you how to deploy your BotKit bot using the Deno runtime on a Linux server with SSH access.
Prerequisites
- A Linux server with SSH access
- Domain name pointing to your server
- Deno installed on your server
Installation steps
Install Deno:
bashcurl -fsSL https://deno.land/install.sh | sh
Clone your bot repository:
bashgit clone https://github.com/yourusername/your-bot.git cd your-bot
Set up environment variables:
bashecho 'export SERVER_NAME="your-domain.com"' >> ~/.bashrc source ~/.bashrc
Setting up HTTPS with Caddy
Caddy is recommended for handling HTTPS and reverse proxy. It automatically obtains and renews SSL/TLS certificates from Let's Encrypt without any manual configuration:
- bash
# Install required packages sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https # Add Caddy repository curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | \ sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | \ sudo tee /etc/apt/sources.list.d/caddy-stable.list # Install Caddy sudo apt update sudo apt install caddy
bash# Add Caddy repository dnf install 'dnf-command(copr)' dnf copr enable @caddy/caddy # Install Caddy dnf install caddy
bash# Install from official repositories sudo pacman -S caddy
TIP
For other installation methods or operating systems, see the official Caddy installation docs.
Create a Caddyfile at /etc/caddy/Caddyfile:
caddyfileyour-domain.com { # Automatic HTTPS tls { # Email for Let's Encrypt notifications email your-email@example.com } # Proxy all requests to your bot reverse_proxy localhost:8000 # Basic security headers header { # Enable HSTS Strict-Transport-Security "max-age=31536000;" # Prevent clickjacking X-Frame-Options "DENY" # XSS protection X-Content-Type-Options "nosniff" X-XSS-Protection "1; mode=block" } }
NOTE
Make sure your domain's DNS A record points to your server's IP address before starting Caddy. Caddy will automatically obtain and manage SSL certificates from Let's Encrypt.
Set proper ownership and permissions:
bashsudo chown root:root /etc/caddy/Caddyfile sudo chmod 644 /etc/caddy/Caddyfile
Restart Caddy:
bashsudo systemctl restart caddy
Check Caddy's status:
bashsudo systemctl status caddy sudo journalctl -u caddy --follow
Creating a systemd service
The systemd is a system and service manager for Linux that starts and manages services. It is adopted by most modern Linux distributions including Debian, Ubuntu, Fedora, and Arch Linux.
To run your bot as a systemd service:
Create a dedicated user for your bot (recommended for security):
bashsudo useradd -r -s /bin/false botkit
Set up the bot's directory:
bashsudo mkdir -p /opt/botkit sudo chown botkit:botkit /opt/botkit
Create a service file at /etc/systemd/system/botkit-bot.service:
ini[Unit] Description=BotKit Bot After=network.target Wants=caddy.service [Service] Type=simple User=botkit Group=botkit Environment=SERVER_NAME=your-domain.com # Add any other environment variables your bot needs Environment=NODE_ENV=production WorkingDirectory=/opt/botkit # Make sure to use the full path to deno ExecStart=/home/botkit/.deno/bin/deno run -A bot.ts # Restart policy Restart=always RestartSec=10 # Security settings NoNewPrivileges=true ProtectSystem=strict ProtectHome=true PrivateTmp=true PrivateDevices=true # Resource limits CPUQuota=80% MemoryMax=1G [Install] WantedBy=multi-user.target
Set proper permissions:
bashsudo chown root:root /etc/systemd/system/botkit-bot.service sudo chmod 644 /etc/systemd/system/botkit-bot.service
Reload systemd, enable and start the service:
bash# Reload systemd to recognize the new service sudo systemctl daemon-reload # Enable service to start on boot sudo systemctl enable botkit-bot # Start the service sudo systemctl start botkit-bot
Verify the service is running:
bash# Check service status sudo systemctl status botkit-bot # View logs sudo journalctl -u botkit-bot -f
TIP
To update your bot:
- Copy new files to /opt/botkit
- Set proper ownership:
sudo chown -R botkit:botkit /opt/botkit
- Restart the service:
sudo systemctl restart botkit-bot
Monitoring logs
Monitor your bot's logs using systemd:
sudo journalctl -u botkit-bot -f