📑 이 챕터에서 다룰 내용
- 들어가며 — 8단계 종합 개요
- 단계 0 — 사전 prep (Provisioning 대기 중)
- 단계 1 — Contabo VPS 발급 + 첫 SSH
- 단계 2 — Ubuntu 24.04 보안 setup (45분)
- 단계 3 — Discord 봇 (Node.js, 90분)
- 단계 4 — Telegram 봇 (Python, 90분)
- 단계 5 — systemd + pm2 자동 재시작 (60분)
- 단계 6 — Position C 5차원 (60분)
- 단계 7 — Sentry + Budget Alert (60분)
- 단계 8 — GitHub Actions 자동 배포 (60분)
- 운영 시작 — 운영 상태 종합
- 운영 명령 빠른 참조
- 📌 cheat sheet 정리
이 cheat sheet 한 페이지에 Contabo VPS 발급 직후부터 두 봇 24/7 운영까지 필요한 모든 명령이 순서대로 담겨 있어요. 각 단계 상세 내용은 1편~8편을 참조하고, 실제 작업할 때는 이 페이지를 옆에 열어두고 따라가세요. 약 7~8시간 = 하루 작업으로 두 봇 24/7 운영을 시작할 수 있습니다.
| 단계 | 내용 | 예상 시간 | 완료 검증 |
|---|---|---|---|
| 0 | 사전 prep (Provisioning 대기 동안) | 30~60분 | 4 토큰 안전 보관 |
| 1 | Contabo VPS 발급 + ssh 접속 | 30분~몇 시간 | uname -a |
| 2 | Ubuntu 24.04 보안 setup | 45분 | sudo ufw status |
| 3 | Discord 봇 (Node.js) | 90분 | !ask 안녕 |
| 4 | Telegram 봇 (Python) | 90분 | /ask 안녕 |
| 5 | systemd + pm2 자동 재시작 | 60분 | ssh 종료 후 봇 작동 확인 |
| 6 | ⚖️ Position C 5차원 강화 | 60분 | 베팅 키워드 자동 차단 |
| 7 | Sentry + Budget Alert | 60분 | 첫 에러 Sentry 도착 |
| 8 | GitHub Actions 자동 배포 | 60분 | git push → 자동 배포 |
Contabo VPS Provisioning 시작 시점부터 계산하면 14일 안에 전 단계 검증 + 환불 결정이 가능합니다.
Contabo VPS를 주문하면 발급까지 30분~몇 시간이 걸려요. 그 시간을 낭비하지 말고 4가지 토큰을 미리 발급해두세요.
[★ 4 토큰 발급 + 안전 보관] 1. Anthropic Console (https://console.anthropic.com) → API 키 (sk-ant-...) → Budget Alert ($20 hard limit) 2. Discord Developer Portal (https://discord.com/developers) → Application 생성 → Bot Token (MTk...) → MESSAGE CONTENT INTENT ON ★ 3. Telegram BotFather → /newbot → Token (1234:ABC...) → /setcommands (ask·help·about·terms·privacy) → /setprivacy → Enable 4. (선택) GitHub Private repo + Discord Alert Webhook
prep 완료 시 → Contabo 발급 직후 즉시 봇 운영 시작 가능합니다.
4개 토큰은 1Password 또는 Bitwarden에 안전하게 보관하세요. 절대 GitHub 등 공개 저장소에 업로드하지 마세요.
# Contabo 가입 → Cloud VPS 10 → Frankfurt EU → Ubuntu 24.04 LTS # 12개월 약정 (Setup Fee €0) # Provisioning 대기 (30분~몇 시간) # 이메일 도착 → IP·비밀번호 확인 # 로컬에서 ssh 접속 ssh -i ~/.ssh/id_ed25519 root@152.42.123.45 # yes → 접속 # 시스템 확인 uname -a # x86_64 free -h # 7.6Gi nproc # 4 df -h # 100G curl -s ipinfo.io # Frankfurt (또는 선택한 위치) ✅
Contabo는 다른 사용자가 쓰다가 반납한 IP를 재사용할 수 있어요. 이전 사용자가 스팸 등으로 블랙리스트에 올라 있으면 봇 운영에 지장이 생깁니다.
spamhaus.org/lookup/에서 본인 IP를 조회하세요. "IP not listed"가 나와야 정상입니다.
✅ 검증: 프롬프트 root@vmi...:~# 도달
# 시스템 업데이트 sudo apt update sudo apt upgrade -y # 기본 도구 sudo apt install -y curl wget git vim htop tmux build-essential # 시간대 한국 sudo timedatectl set-timezone Asia/Seoul # 호스트네임 hostnamectl set-hostname tsv-bot-frankfurt exec bash
sudo vim /etc/ssh/sshd_config # PermitRootLogin no # PasswordAuthentication no # PubkeyAuthentication yes # ClientAliveInterval 300 sudo systemctl restart ssh
sudo apt install -y ufw sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw allow 22/tcp sudo ufw allow 443/tcp sudo ufw enable
sudo apt install -y fail2ban sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sudo vim /etc/fail2ban/jail.local # bantime = 1h # findtime = 10m # maxretry = 5 sudo systemctl enable fail2ban sudo systemctl restart fail2ban
# 자동 업데이트 sudo apt install -y unattended-upgrades sudo dpkg-reconfigure --priority=low unattended-upgrades # swap 4GB sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf sudo sysctl -p
sudo adduser ubuntu # 비밀번호 입력 sudo usermod -aG sudo ubuntu sudo cp -r /root/.ssh /home/ubuntu/.ssh sudo chown -R ubuntu:ubuntu /home/ubuntu/.ssh sudo chmod 700 /home/ubuntu/.ssh sudo chmod 600 /home/ubuntu/.ssh/authorized_keys # 새 세션에서 ubuntu 접속 ssh -i ~/.ssh/id_ed25519 ubuntu@152.42.123.45 # 검증 sudo ufw status verbose # active sudo fail2ban-client status # 작동 free -h # swap: 4.0Gi
✅ 검증: ssh 키 인증만, ufw active, fail2ban 작동
# Node.js 22 LTS curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash source ~/.bashrc nvm install 22 nvm use 22 nvm alias default 22 # 작업 디렉토리 mkdir ~/tsv-bot cd ~/tsv-bot # package.json + 패키지 npm init -y npm install discord.js @anthropic-ai/sdk dotenv npm install --save-dev typescript @types/node ts-node nodemon npx tsc --init # package.json 수정 — "type": "module" 추가 # scripts: build/start/dev
# .env (4 토큰 paste) cat > .env << 'EOF' DISCORD_TOKEN=MTk5OTk... DISCORD_APP_ID=... ANTHROPIC_API_KEY=sk-ant-... ANTHROPIC_MODEL=claude-sonnet-4-6 LOG_DIR=./logs EOF chmod 600 .env # .gitignore cat > .gitignore << 'EOF' node_modules/ .env logs/ dist/ *.log .DS_Store EOF
# src/ 디렉토리 + 코드 (3편 본문 4 파일) mkdir -p src logs # - src/messages.ts (Position C footer + system prompt) # - src/claude.ts (Anthropic SDK wrapper) # - src/logger.ts (LogOnTable jsonl) # - src/bot.ts (메인 봇) # 빌드 + 실행 npm run build npm start # 디스코드 채널에서 테스트 # !ask 안녕 # 정상 시 로그 확인 ls logs/ cat logs/$(date +%Y-%m-%d).jsonl | head
✅ 검증: 디스코드에서 !ask 안녕 → 봇 응답 + footer
# uv 설치 curl -LsSf https://astral.sh/uv/install.sh | sh source ~/.bashrc # Python 3.12 uv python install 3.12 # 작업 디렉토리 mkdir ~/tsv-tg-bot cd ~/tsv-tg-bot # 가상환경 uv venv source .venv/bin/activate # 패키지 uv pip install python-telegram-bot anthropic python-dotenv
# .env cat > .env << 'EOF' TELEGRAM_TOKEN=1234:ABC... ANTHROPIC_API_KEY=sk-ant-... ANTHROPIC_MODEL=claude-sonnet-4-6 LOG_DIR=./logs EOF chmod 600 .env # .gitignore cat > .gitignore << 'EOF' .venv/ __pycache__/ *.pyc .env logs/ .DS_Store EOF # src/ 디렉토리 + 코드 (4편 본문 4 파일) mkdir -p src logs # - src/messages.py # - src/claude.py # - src/logger.py # - src/bot.py # 실행 python src/bot.py # Telegram 에서 테스트 # /ask 안녕
✅ 검증: Telegram 에서 /ask 안녕 → 봇 응답 + footer
# pm2 전역 설치 sudo npm install -g pm2 cd ~/tsv-bot # ecosystem.config.cjs (5편 본문) # 자동 재시작 + 메모리 제한 + 로그 회전 pm2 start ecosystem.config.cjs pm2 status # 부팅 시 자동 시작 pm2 startup # 안내된 sudo 명령 실행 pm2 save
sudo tee /etc/systemd/system/tsv-tg-bot.service > /dev/null << 'EOF' [Unit] Description=TSV Telegram Bot After=network.target [Service] Type=simple User=ubuntu WorkingDirectory=/home/ubuntu/tsv-tg-bot ExecStart=/home/ubuntu/tsv-tg-bot/.venv/bin/python /home/ubuntu/tsv-tg-bot/src/bot.py Restart=always RestartSec=10 MemoryMax=500M StandardOutput=append:/home/ubuntu/tsv-tg-bot/logs/systemd-out.log StandardError=append:/home/ubuntu/tsv-tg-bot/logs/systemd-error.log [Install] WantedBy=multi-user.target EOF sudo systemctl daemon-reload sudo systemctl enable tsv-tg-bot sudo systemctl start tsv-tg-bot
# 검증 pm2 status # online sudo systemctl status tsv-tg-bot # active # ★ ssh 종료 검증 exit ssh ubuntu@your-ip pm2 status # ★ 여전히 online ✅ sudo systemctl status tsv-tg-bot # ★ 여전히 active ✅
✅ 검증: ssh 종료 후 봇 여전히 작동, !ask·/ask 응답
# Node.js 봇 — safety_filter.ts 추가 cd ~/tsv-bot # src/safety_filter.ts (6편 본문) # - GAMBLING_KEYWORDS # - ABSOLUTE_KEYWORDS_HIGH_RISK # - checkUserMessage / checkAssistantResponse # bot.ts 수정 — 사용자 검출 + 응답 검출 통합 # 슬래시 명령 추가 # - src/commands.ts # - registerSlashCommands() # - handleSlashCommand() # 빌드 + 재시작 npm run build pm2 restart tsv-discord-bot # 디스코드 테스트 # !ask 베팅 추천 → 차단 ✅ # /about → About 본문 ✅
# Python 봇 — safety_filter.py 추가 cd ~/tsv-tg-bot # src/safety_filter.py (6편 본문) # bot.py 수정 — about·terms·privacy 명령 + 검출 통합 # 재시작 sudo systemctl restart tsv-tg-bot # Telegram 테스트 # /ask 베팅 추천 → 차단 ✅ # /about → About 본문 ✅
✅ 검증: 베팅 키워드 자동 차단, /about·/terms·/privacy 응답
# Sentry 가입 (https://sentry.io) → Node.js + Python 프로젝트 → DSN 복사 # Node.js cd ~/tsv-bot npm install @sentry/node # .env 추가 echo 'SENTRY_DSN=https://...@sentry.io/...' >> .env # src/sentry.ts + bot.ts 통합 (7편 본문) npm run build pm2 restart tsv-discord-bot
# Python cd ~/tsv-tg-bot source .venv/bin/activate uv pip install sentry-sdk # .env 추가 echo 'SENTRY_DSN=https://...@sentry.io/...' >> .env # src/sentry_init.py + bot.py 통합 sudo systemctl restart tsv-tg-bot
# 매일 운영 리포트 cron mkdir -p ~/scripts ~/reports # ~/scripts/daily_report.sh (7편 본문) chmod +x ~/scripts/daily_report.sh crontab -e # 30 9 * * * /home/ubuntu/scripts/daily_report.sh # Anthropic API 사용량 추적 # usage_tracker.ts/py 통합 (7편 본문) # 일별 비용 cron # 0 9 * * * /home/ubuntu/scripts/daily_cost.sh
✅ 검증: 의도적 에러 → Sentry 도착, 매일 09:30 리포트
# GitHub Repo prep cd ~/tsv-bot git init git add . git commit -m "Initial commit" # Deploy 키 생성 (서버에서) ssh-keygen -t ed25519 -C "github-deploy@tsv-bot" -f ~/.ssh/deploy_key -N "" cat ~/.ssh/deploy_key.pub # → GitHub Repo Settings → Deploy keys → Add (read-only X) # GitHub Actions 키 (별도) ssh-keygen -t ed25519 -C "github-actions" -f ~/.ssh/gha_key -N "" cat ~/.ssh/gha_key.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys cat ~/.ssh/gha_key # → GitHub Secret SSH_PRIVATE_KEY
# GitHub Secrets 등록 # Repo Settings → Secrets and variables → Actions # - SSH_HOST: 152.42.123.45 # - SSH_USER: ubuntu # - SSH_PORT: 22 # - SSH_PRIVATE_KEY: ~/.ssh/gha_key 내용 전체 # - DISCORD_DEPLOY_WEBHOOK: (선택) # workflow 파일 # .github/workflows/deploy.yml (8편 본문) # .github/workflows/deploy-tg.yml git add .github/ git commit -m "Add GitHub Actions deploy workflows" # GitHub remote 등록 git remote add origin git@github.com:junho/tsv-bot.git git branch -M main git push -u origin main # ★ 자동 배포 트리거 확인 # → GitHub Actions 탭 → workflow 실행 ✅
✅ 검증: git push → 자동 빌드 + ssh 배포 + Discord 알림
[인프라]
- Contabo Cloud VPS 10 (Frankfurt EU, 4 vCPU + 8GB RAM)
- Ubuntu 24.04 + 보안 6단계 (ssh·ufw·fail2ban·swap·자동업데이트·사용자분리)
- 14일 환불 보장 기간 내 검증 완료
[봇]
- Node.js Discord 봇 (pm2)
- Python Telegram 봇 (systemd)
- 두 봇 동시 운영 + 자동 재시작
- ⚖️ Position C 5차원 일관 적용
[운영]
- Sentry 에러 자동 추적
- Anthropic API 사용량 + 비용 추적
- 매일 09:30 운영 리포트 (Discord webhook)
- 매일 09:00 일별 비용 알림
[배포]
- GitHub Actions 자동 배포
- pm2 reload + systemctl restart 무중단
- 5분 롤백 가능
[비용]
- Contabo: €4.50/월 (~$4.80)
- Anthropic API: ~$5~30/월
- Sentry: $0 (무료 5K events)
- GitHub: $0 (Private repo 무료)
- ★ 합계: ~$10~35/월
# 봇 상태 pm2 status # Discord 봇 sudo systemctl status tsv-tg-bot # Telegram 봇
pm2 logs tsv-discord-bot --lines 50 sudo journalctl -u tsv-tg-bot -n 50 -f
pm2 restart tsv-discord-bot sudo systemctl restart tsv-tg-bot
htop # CPU·메모리 df -h # 디스크 free -h # RAM sudo ufw status # 방화벽 sudo fail2ban-client status
# 일별 운영 리포트 (수동 실행) ~/scripts/daily_report.sh # 일별 API 비용 ~/scripts/daily_cost.sh
# 배포 git push origin main # GitHub Actions 자동 배포 # 응급 — 롤백 cd ~/tsv-bot git log --oneline -5 git checkout <prev-commit> npm ci && npm run build pm2 reload tsv-discord-bot
- 1️⃣ 핵심 한 줄: Contabo VPS 발급 → 8단계 (~7~8시간, 하루) → 두 봇 24/7 운영
- 2️⃣ 단계 0: Provisioning 대기 중 4 토큰 발급
- 3️⃣ 단계 1~2: VPS 접속 + Ubuntu 보안 6단계 (Recycled IP 검증 필수)
- 4️⃣ 단계 3~4: Discord 봇(Node.js) + Telegram 봇(Python) — 각 90분
- 5️⃣ 단계 5: pm2 + systemd — ssh 종료 후도 봇 계속 작동
- 6️⃣ 단계 6: ⚖️ Position C 5차원 — 베팅 키워드 자동 차단
- 7️⃣ 단계 7: Sentry 에러 추적 + 매일 운영 리포트 cron
- 8️⃣ 단계 8: GitHub Actions 자동 배포 — git push 한 번으로 배포
- 9️⃣ 14일 검증 일정: Day 1~3 단계 1~5, Day 4~7 단계 6~8, Day 8~14 안정 운영
- 🔟 월 운영 비용: ~$10~35 (Contabo €4.50 + Anthropic $5~30)
- E1 다층 보호: 4 토큰 분리 + ssh + ufw + fail2ban + Position C
- R4 자동 회복: pm2 + systemd 자동 재시작
- E4 LogOnTable: jsonl 누적 + 매일 리포트
- "Day 0 시스템": ssh 종료 후 봇 영향 없음
이 cheat sheet 한 페이지 = Junho 1인 운영자가 봇 운영을 시작하는 데 필요한 모든 것입니다.