ติดตั้ง Mattermost Self-Hosted Team Chat บน VPS

📅 6 ตุลาคม 2026 · 🏷️ Collaboration, Chat, Self-Hosted

ติดตั้ง Mattermost บน VPS: Self-Hosted Team Chat

Mattermost คือแอปพลิเคชัน team chat แบบ open-source ที่ใช้แทน Slack ได้ 100% มีทั้ง channel, direct message, thread, file sharing, webhook และ bot integration ข้อแตกต่างสำคัญคือ ข้อมูลทั้งหมดอยู่บน VPS ของคุณ ไม่ผ่านเซิร์ฟเวอร์บุคคลที่สาม

บทความนี้จะพาติดตั้ง Mattermost บน VPS Ubuntu/Debian โดยใช้ PostgreSQL เป็นฐานข้อมูลและ Nginx เป็น reverse proxy พร้อม SSL certificate จาก Let's Encrypt

เปรียบเทียบ Mattermost vs Slack

คุณสมบัติMattermost (Self-Hosted)Slack (Cloud)
ค่าใช้จ่ายฟรี (ค่า VPS เท่านั้น)$8–15/user/เดือน
ความเป็นส่วนตัวข้อมูลอยู่ที่เซิร์ฟเวอร์ตัวเองข้อมูลอยู่บน Slack cloud
Message historyไม่จำกัดจำกัด (แผน Free)
Customizationปรับแต่งได้เต็มที่จำกัดตามแผน
RAM ที่ต้องการ2GB ขึ้นไปไม่ต้องจัดการ

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

ขั้นตอนที่ 1 — ติดตั้ง PostgreSQL

Mattermost รองรับทั้ง MySQL และ PostgreSQL แต่ทีมพัฒนาแนะนำ PostgreSQL เพราะ performance และ stability ดีกว่า

# อัปเดต package list
sudo apt update && sudo apt upgrade -y

# ติดตั้ง PostgreSQL
sudo apt install -y postgresql postgresql-contrib

# เปิดใช้งานและสตาร์ท
sudo systemctl enable --now postgresql

# สร้าง database user สำหรับ Mattermost
sudo -u postgres psql <<EOF
CREATE USER mmuser WITH PASSWORD 'mmuser_password_change_this';
CREATE DATABASE mattermost WITH OWNER mmuser ENCODING 'UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8' TEMPLATE=template0;
GRANT ALL PRIVILEGES ON DATABASE mattermost TO mmuser;
EOF
Tip: เปลี่ยน mmuser_password_change_this เป็น password ที่แข็งแกร่ง ควรใช้ random string อย่างน้อย 20 ตัวอักษร

ขั้นตอนที่ 2 — ดาวน์โหลดและติดตั้ง Mattermost

# ดาวน์โหลด Mattermost Team Edition (ฟรี)
cd /tmp
wget https://releases.mattermost.com/10.1.0/mattermost-10.1.0-linux-amd64.tar.gz

# แตกไฟล์และย้ายไป /opt
tar -xzf mattermost-10.1.0-linux-amd64.tar.gz
sudo mv mattermost /opt/mattermost

# สร้าง directory สำหรับเก็บ data
sudo mkdir -p /opt/mattermost/data

# สร้าง user สำหรับรัน Mattermost (ไม่ให้รันเป็น root)
sudo useradd --system --user-group mattermost
sudo chown -R mattermost:mattermost /opt/mattermost
sudo chmod -R g+w /opt/mattermost

ขั้นตอนที่ 3 — ตั้งค่า Mattermost

# แก้ไขไฟล์ config
sudo nano /opt/mattermost/config/config.json

หาและแก้ไข section "SqlSettings":

"SqlSettings": {
    "DriverName": "postgres",
    "DataSource": "postgres://mmuser:mmuser_password_change_this@localhost/mattermost?sslmode=disable&connect_timeout=10",
    ...
}

และ section "ServiceSettings":

"ServiceSettings": {
    "SiteURL": "https://chat.yourdomain.com",
    "ListenAddress": ":8065",
    ...
}

ขั้นตอนที่ 4 — สร้าง systemd Service

sudo nano /etc/systemd/system/mattermost.service
[Unit]
Description=Mattermost Team Chat
After=network.target postgresql.service

[Service]
Type=notify
ExecStart=/opt/mattermost/bin/mattermost
TimeoutStartSec=3600
KillMode=mixed
Restart=always
RestartSec=10
WorkingDirectory=/opt/mattermost
User=mattermost
Group=mattermost
LimitNOFILE=49152

[Install]
WantedBy=multi-user.target
# โหลด systemd และสตาร์ท Mattermost
sudo systemctl daemon-reload
sudo systemctl enable --now mattermost

# ตรวจสอบ status
sudo systemctl status mattermost

ขั้นตอนที่ 5 — ตั้งค่า Nginx Reverse Proxy

ติดตั้ง Nginx และสร้าง config สำหรับ Mattermost:

sudo apt install -y nginx

sudo nano /etc/nginx/sites-available/mattermost
upstream backend {
    server localhost:8065;
    keepalive 32;
}

server {
    listen 80;
    server_name chat.yourdomain.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name chat.yourdomain.com;

    ssl_certificate /etc/letsencrypt/live/chat.yourdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/chat.yourdomain.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;

    location ~ /api/v[0-9]+/(users/)?websocket$ {
        proxy_pass http://backend;
        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;
        proxy_set_header X-Frame-Options SAMEORIGIN;
        proxy_read_timeout 600s;
    }

    location / {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        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;
        proxy_set_header X-Frame-Options SAMEORIGIN;
        client_max_body_size 50M;
    }
}
# เปิดใช้งาน site
sudo ln -s /etc/nginx/sites-available/mattermost /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

ขั้นตอนที่ 6 — ขอ SSL Certificate

# ติดตั้ง Certbot
sudo apt install -y certbot python3-certbot-nginx

# ขอ certificate (Nginx ต้องรันอยู่และ domain ชี้ IP แล้ว)
sudo certbot --nginx -d chat.yourdomain.com

# ทดสอบ auto-renew
sudo certbot renew --dry-run

หลังได้ certificate แล้ว reload Nginx อีกครั้ง:

sudo systemctl reload nginx

ขั้นตอนที่ 7 — สร้าง Admin Account แรก

เปิด browser ไปที่ https://chat.yourdomain.com แล้วทำตามขั้นตอน:

  1. คลิก Create Account
  2. กรอกอีเมลและรหัสผ่าน — account แรกจะได้เป็น System Admin อัตโนมัติ
  3. สร้าง team แรกและเชิญสมาชิก
  4. ไปที่ System Console → Site Configuration เพื่อตั้งค่าเพิ่มเติม
Tip: ตั้งค่า SMTP ใน System Console เพื่อให้ส่ง email notification ได้ ไปที่ Email Settings และกรอก SMTP server ของคุณ

หากคุณกำลังมองหา VPS ที่มี resource เหมาะกับการรัน Mattermost สำหรับทีมขนาดกลาง ลองพิจารณา VPS จาก asiagb.com ที่ให้ RAM 4GB ในราคาที่คุ้มค่า เหมาะสำหรับการรัน self-hosted services หลายอย่างพร้อมกัน


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

# ตรวจสอบ status service
sudo systemctl status mattermost

# ดู log ล่าสุด
sudo journalctl -u mattermost -n 50 --no-pager

# ทดสอบ port ภายใน
curl -s http://localhost:8065/api/v4/system/ping | python3 -m json.tool

# ตรวจสอบ database connection
sudo -u mattermost /opt/mattermost/bin/mattermost db migrate

# ตรวจสอบ Nginx config
sudo nginx -t && sudo systemctl status nginx
Tip: คำสั่ง curl localhost:8065/api/v4/system/ping ควรตอบ {"status":"OK"} — ถ้าไม่ตอบแสดงว่า Mattermost ยังไม่ขึ้น ให้ดู log ด้วย journalctl -u mattermost -f

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

Error: "Failed to connect to database: pq: password authentication failed"

รหัสผ่าน database ใน config.json ไม่ตรงกับที่ตั้งใน PostgreSQL ให้ตรวจสอบ DataSource string และรีเซ็ต password หากจำเป็น

# รีเซ็ต password ใน PostgreSQL
sudo -u postgres psql -c "ALTER USER mmuser WITH PASSWORD 'new_password';"

# แก้ไข config.json
sudo nano /opt/mattermost/config/config.json
# อัปเดต DataSource ให้ตรงกับ password ใหม่
sudo systemctl restart mattermost

Error: "address already in use :8065"

Port 8065 ถูกใช้งานโดย process อื่น ให้หา process นั้นและหยุดมัน

# ดูว่า process ไหนใช้ port 8065
sudo ss -tlnp | grep 8065
sudo lsof -i :8065

# kill process ที่ไม่ต้องการ (แทน PID ด้วยเลข process จริง)
sudo kill -9 
sudo systemctl start mattermost

Error: "WebSocket connection failed" ใน browser

Nginx ไม่ได้ตั้งค่า WebSocket Upgrade ให้ถูกต้อง หรือ firewall บล็อก WebSocket connection

# ตรวจสอบ Nginx config มี WebSocket location หรือยัง
sudo grep -n "websocket" /etc/nginx/sites-available/mattermost

# ตรวจสอบ Nginx error log
sudo tail -50 /var/log/nginx/error.log

# reload หลังแก้ไข config
sudo nginx -t && sudo systemctl reload nginx

Error: "SiteURL must not be blank" หรือ notification ไม่ทำงาน

ลืมตั้งค่า SiteURL ใน config.json หรือตั้งเป็น http://localhost:8065 แทนที่จะเป็น domain จริง

# แก้ไข SiteURL ใน config.json
sudo nano /opt/mattermost/config/config.json
# เปลี่ยนเป็น: "SiteURL": "https://chat.yourdomain.com"

sudo systemctl restart mattermost

Error: ไฟล์ attachment อัปโหลดไม่ได้ หรือขึ้น "413 Request Entity Too Large"

Nginx มี limit ขนาดไฟล์ที่ส่งได้ ต้องเพิ่ม client_max_body_size ใน Nginx config

# แก้ไข Nginx config
sudo nano /etc/nginx/sites-available/mattermost
# เพิ่ม/แก้ไขใน location /: client_max_body_size 100M;

# อัปเดต Mattermost file size limit ด้วย (ใน config.json)
# "FileSettings": { "MaxFileSize": 104857600 }  # 100MB

sudo nginx -t && sudo systemctl reload nginx
sudo systemctl restart mattermost

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

Mattermost Team Edition ฟรีรองรับผู้ใช้กี่คน?

Mattermost Team Edition (ฟรี) ไม่มีจำกัดจำนวนผู้ใช้ สามารถใช้กับทีมขนาดใหญ่ได้ ความแตกต่างจาก Enterprise Edition อยู่ที่ features เพิ่มเติม เช่น SSO/SAML, compliance exports และ advanced analytics ซึ่ง Team Edition ไม่มี

อัปเดต Mattermost เป็น version ใหม่ทำอย่างไร?

ดาวน์โหลด tarball version ใหม่ หยุด service สำรองข้อมูล แตกไฟล์ใหม่ทับ directory เดิม (ยกเว้น data/ และ config/) แล้ว restart service ควรอ่าน release notes ก่อนเสมอเพราะบาง version อาจต้อง migrate database

สำรองข้อมูล Mattermost ทำอย่างไร?

ต้องสำรอง 2 ส่วน: (1) PostgreSQL database ด้วย pg_dump mattermost > backup.sql และ (2) directory /opt/mattermost/data/ ที่เก็บไฟล์ attachment ทั้งหมด แนะนำทำ backup ทุกวันและเก็บไว้นอก VPS หลัก