ติดตั้ง ntfy บน VPS ระบบแจ้งเตือน Push Notification Self-Hosted

ติดตั้ง ntfy บน VPS: Push Notification Self-Hosted

ตุลาคม 2026 · อ่าน 8 นาที

ntfy (อ่านว่า "notify") เป็น pub/sub notification service แบบ open-source ที่ใช้งานผ่าน HTTP แบบง่ายที่สุด — ส่งการแจ้งเตือนผ่าน curl ได้ทันทีโดยไม่ต้องสมัครบัญชีหรือติดตั้ง SDK เลย สามารถใช้ public server ที่ ntfy.sh หรือ host เองบน VPS ของคุณได้ เหมาะมากสำหรับการส่งแจ้งเตือนจาก cron job, backup script, deploy pipeline หรือ server monitoring โดยรับการแจ้งเตือนบนมือถือผ่าน app ntfy (Android/iOS)

ntfy คืออะไร และทำไมต้อง Self-Host

ntfy ใช้โมเดล topic-based pub/sub — ผู้ส่ง (publisher) POST ข้อความไปที่ URL https://ntfy.example.com/TOPIC และผู้รับ (subscriber) ที่ subscribe topic นั้นจะได้รับการแจ้งเตือนทันที ความพิเศษของ ntfy คือ:

การ self-host ช่วยให้ topic ของคุณเป็น private ทั้งหมด เหมาะสำหรับข้อมูลสำคัญอย่าง server alerts หรือ script output ที่ไม่อยากให้ผ่าน public server

ความต้องการของระบบ

ส่วนประกอบขั้นต่ำ
RAM128 MB
Disk1 GB (สำหรับ message cache)
OSUbuntu 20.04+ / Debian 11+
Port80/443 (หรือ port custom)
Domainแนะนำใช้ subdomain เช่น notify.example.com

ติดตั้ง ntfy ด้วย apt (Ubuntu/Debian)

วิธีนี้เป็นวิธีแนะนำสำหรับ Ubuntu/Debian — ได้รับ binary เดียว ใช้ systemd manage service:

# เพิ่ม repository
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://archive.heckel.io/apt/pubkey.txt | sudo gpg --dearmor -o /etc/apt/keyrings/archive.heckel.io.gpg

sudo sh -c "echo 'deb [arch=amd64 signed-by=/etc/apt/keyrings/archive.heckel.io.gpg] https://archive.heckel.io/apt debian main' > /etc/apt/sources.list.d/archive.heckel.io.list"

# ติดตั้ง ntfy
sudo apt update
sudo apt install ntfy

# ตรวจสอบ version
ntfy --version

ติดตั้ง ntfy ด้วย Docker Compose

สำหรับผู้ใช้ Docker วิธีนี้ง่ายและ portable กว่า:

# สร้าง directory
mkdir -p /opt/ntfy/{config,cache}
cd /opt/ntfy

# สร้าง docker-compose.yml
cat > docker-compose.yml <<'EOF'
services:
  ntfy:
    image: binwiederhier/ntfy
    container_name: ntfy
    restart: unless-stopped
    command: serve
    environment:
      - TZ=Asia/Bangkok
    volumes:
      - /opt/ntfy/config:/etc/ntfy
      - /opt/ntfy/cache:/var/cache/ntfy
    ports:
      - "127.0.0.1:2586:80"
EOF

docker compose up -d

ตั้งค่า ntfy (server.yml)

สร้างไฟล์ config หลักสำหรับ ntfy:

sudo nano /etc/ntfy/server.yml
# /etc/ntfy/server.yml
base-url: "https://notify.example.com"
listen-http: ":80"
cache-file: /var/cache/ntfy/cache.db
cache-duration: "12h"
attachment-cache-dir: /var/cache/ntfy/attachments
attachment-total-size: "5G"
attachment-file-size: "15M"
attachment-expiry-duration: "3h"

# เปิด authentication (แนะนำ)
auth-file: /var/lib/ntfy/user.db
auth-default-access: "deny-all"

# log
log-level: info
Note: ถ้าใช้ Docker ให้วางไฟล์นี้ที่ /opt/ntfy/config/server.yml และปรับ path ที่ใช้ใน volumes ให้ถูกต้อง

ตั้งค่า Nginx Reverse Proxy

ntfy ต้องการ WebSocket สำหรับ real-time notification ต้องตั้งค่า Nginx ให้ถูกต้อง:

sudo nano /etc/nginx/sites-available/ntfy
server {
    listen 80;
    server_name notify.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name notify.example.com;

    ssl_certificate /etc/letsencrypt/live/notify.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/notify.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:2586;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # สำหรับ long-polling และ WebSocket
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}
sudo ln -s /etc/nginx/sites-available/ntfy /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

# ขอ SSL certificate
sudo certbot --nginx -d notify.example.com

จัดการ User และ Access Control

ntfy มีระบบ auth ในตัว สร้าง admin user และ user ทั่วไป:

# สร้าง admin user (เข้าถึงทุก topic)
sudo ntfy user add --role=admin myadmin
# ป้อน password เมื่อถูกถาม

# สร้าง user ทั่วไป
sudo ntfy user add monitoring

# กำหนดสิทธิ์ publish/subscribe topic เฉพาะ
sudo ntfy access monitoring server-alerts rw   # read+write
sudo ntfy access monitoring backups ro          # read-only

# ดู ACL ทั้งหมด
sudo ntfy access

ส่งการแจ้งเตือนด้วย curl

เมื่อ ntfy พร้อมแล้ว ส่ง notification ได้ทันทีจาก shell:

# ส่งแบบง่ายที่สุด (public topic)
curl -d "Backup เสร็จแล้ว!" https://notify.example.com/server-alerts

# ส่งพร้อม title, priority, และ tags (ด้วย auth)
curl \
  -u "monitoring:PASSWORD" \
  -H "Title: Disk Usage Warning" \
  -H "Priority: high" \
  -H "Tags: warning,floppy_disk" \
  -d "Disk usage บน VPS01 เกิน 85% แล้ว" \
  https://notify.example.com/server-alerts

# ส่งพร้อม action button (เปิด URL เมื่อกด)
curl \
  -u "monitoring:PASSWORD" \
  -H "Title: Deploy สำเร็จ" \
  -H "Actions: view, เปิดเว็บ, https://example.com" \
  -d "Deploy version 2.1.3 เสร็จแล้ว" \
  https://notify.example.com/deploys

ใช้ ntfy กับ Cron Job และ Script

ใช้ ntfy แจ้งเตือนเมื่อ cron job หรือ backup script เสร็จสิ้น:

#!/bin/bash
# /opt/scripts/backup-notify.sh

NTFY_URL="https://notify.example.com/server-alerts"
NTFY_AUTH="monitoring:PASSWORD"

notify() {
  curl -s \
    -u "$NTFY_AUTH" \
    -H "Title: $1" \
    -H "Priority: ${3:-default}" \
    -H "Tags: ${4:-white_check_mark}" \
    -d "$2" \
    "$NTFY_URL" > /dev/null
}

# ทำ backup
rsync -az /var/www/ [email protected]:/backups/www/
STATUS=$?

if [ $STATUS -eq 0 ]; then
  notify "Backup สำเร็จ" "rsync backup เสร็จเรียบร้อยเวลา $(date '+%H:%M')" "default" "white_check_mark"
else
  notify "Backup ล้มเหลว!" "rsync backup error code: $STATUS" "urgent" "rotating_light"
fi
# เพิ่มใน crontab
crontab -e

# backup ทุกคืน 02:00 และแจ้งเตือน
0 2 * * * /opt/scripts/backup-notify.sh
Tip: Priority มี 5 ระดับ: min, low, default, high, urgent — ระดับ urgent จะทำให้ notification มีเสียงดังและ break through Do Not Disturb บนมือถือ

รับการแจ้งเตือนบนมือถือ

ติดตั้ง app ntfy และ subscribe topic:

ตรวจสอบว่า ntfy ทำงานถูกต้อง

# ตรวจสอบ service status (apt install)
sudo systemctl status ntfy

# ตรวจสอบด้วย Docker
docker compose -f /opt/ntfy/docker-compose.yml ps
docker logs ntfy --tail 20

# ทดสอบส่ง notification แบบ basic
curl -s https://notify.example.com/health | python3 -m json.tool

# ทดสอบส่ง message จริง
curl -d "Test from VPS" https://notify.example.com/test-topic

# ตรวจสอบ message ที่ส่งไปแล้ว (JSON stream)
curl -s "https://notify.example.com/test-topic/json?poll=1" | head -5

# ตรวจสอบ user list
sudo ntfy user list
Tip: endpoint /health ของ ntfy จะ return {"healthy":true} เมื่อ service พร้อม ใช้ Uptime Kuma monitor endpoint นี้เพื่อแจ้งเตือนถ้า ntfy เองล่ม

แก้ปัญหาที่พบบ่อย

Error: "upstream connect error or disconnect/reset before headers"

เกิดจาก Nginx ไม่สามารถเชื่อมต่อกับ ntfy ได้ ตรวจสอบว่า ntfy กำลังฟังที่ port ที่ถูกต้องและ Nginx ชี้ถูก:

# ตรวจสอบว่า ntfy ฟัง port ใด
ss -tlnp | grep ntfy
# หรือ Docker
docker port ntfy

# ตรวจสอบ Nginx config
sudo nginx -T | grep proxy_pass

# reload หลังแก้
sudo systemctl reload nginx

Error: "403 Forbidden — Access denied"

เกิดจาก auth-default-access: deny-all ใน config แต่ไม่ได้ใส่ credentials ในคำสั่ง curl หรือ user ไม่มีสิทธิ์ topic นั้น:

# ตรวจสอบ ACL ของ user
sudo ntfy access monitoring

# เพิ่มสิทธิ์ให้ topic ที่ต้องการ
sudo ntfy access monitoring server-alerts rw

# ทดสอบด้วย -u flag
curl -u "monitoring:PASSWORD" -d "test" https://notify.example.com/server-alerts

WebSocket ไม่ทำงาน / notification ไม่ real-time

Nginx ต้องมี header Upgrade และ Connection สำหรับ WebSocket:

# ตรวจสอบ Nginx config
grep -A5 "location /" /etc/nginx/sites-available/ntfy

# ต้องมีบรรทัดเหล่านี้:
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection "upgrade";
# proxy_read_timeout 3600s;

sudo nginx -t && sudo systemctl reload nginx

Error: "database is locked" ใน logs

เกิดจาก ntfy หลาย instance พยายาม access SQLite database พร้อมกัน ต้องให้มีเพียง process เดียว:

# ตรวจสอบ ntfy processes ที่รันอยู่
ps aux | grep ntfy

# ถ้ามีมากกว่า 1 ให้ stop ทั้งหมด
sudo systemctl stop ntfy
sudo pkill -f ntfy

# แล้ว start ใหม่ครั้งเดียว
sudo systemctl start ntfy
sudo systemctl status ntfy

ไม่ได้รับ notification บนมือถือ iOS

iOS ใช้ APNs ทำให้ ntfy ต้องส่งผ่าน upstream server ของ ntfy.sh ตรวจสอบการตั้งค่า:

# เพิ่มใน server.yml สำหรับ iOS push
upstream-base-url: "https://ntfy.sh"

# restart ntfy หลังแก้ config
sudo systemctl restart ntfy

คำถามที่พบบ่อย

ntfy ต่างจาก Gotify อย่างไร?

ทั้งคู่เป็น self-hosted notification server แต่ ntfy มีจุดเด่นที่ API แบบ HTTP plain — ส่งได้ด้วย curl โดยไม่ต้อง API key พิเศษ นอกจากนี้ ntfy มี app Android แบบ Firebase-free รองรับ pub/sub หลาย subscriber ต่อ topic และมี web UI ในตัว Gotify เหมาะกว่าถ้าต้องการ token-based authentication ที่เข้มงวดกว่า

topic ของ ntfy ปลอดภัยแค่ไหน?

บน public server (ntfy.sh) topic ที่ชื่อง่ายๆ เช่น alerts หรือ myserver มีความเสี่ยงสูงเพราะใครก็ subscribe ได้ การ self-host พร้อม auth-default-access: deny-all และสร้าง user มี password ทำให้ topic ปลอดภัยอย่างสมบูรณ์ ใช้ชื่อ topic ที่ random เพิ่มความปลอดภัยอีกชั้น

ntfy รองรับ attachment หรือไม่?

รองรับ — ส่งไฟล์แนบได้โดยตรงผ่าน HTTP PUT request ขนาดสูงสุดกำหนดได้ใน config (attachment-file-size) ไฟล์จะเก็บไว้บน server ตาม attachment-expiry-duration ที่กำหนด เหมาะสำหรับส่ง log file หรือ screenshot จาก monitoring script