Để triển khai Matrix (Synapse) + Element Web trên Linux sử dụng Docker với tên miền chat.tenmiencuaban.com, bạn có thể làm theo hướng dẫn dưới đây. Mình sẽ đi từ A-Z, dễ hiểu và chuẩn hóa theo môi trường production cơ bản:
🔧 Yêu cầu hệ thống
Máy chủ Linux (Ubuntu/Debian/CentOS/AlmaLinux đều được)
Docker + Docker Compose đã cài sẵn
Tên miền chat.tenmiencuaban.com đã trỏ DNS về IP VPS
Đã mở cổng: 80, 443, 8448
🗂 Cấu trúc thư mục khuyến nghị
/opt/matrix/
├── data/ # Dữ liệu Synapse
├── element-web/ # Dữ liệu Element Web
└── docker-compose.yml # File triển khai
1. Cài đặt Docker & Docker Compose (nếu chưa có)
# Ubuntu/Debian
sudo apt update && sudo apt install -y docker.io docker-compose
# CentOS/RHEL/AlmaLinux
sudo yum install -y docker docker-compose sudo systemctl start docker sudo systemctl enable docker
2. Tạo file docker-compose.yml
version: '3.8' services: synapse: image: matrixdotorg/synapse:latest container_name: synapse restart: unless-stopped volumes: - ./data:/data environment: - SYNAPSE_SERVER_NAME=chat.tenmiencuaban.com - SYNAPSE_REPORT_STATS=yes ports: - 8008:8008 # HTTP (dùng proxy) - 8448:8448 # Federation (để giao tiếp server khác) element: image: vectorim/element-web:latest container_name: element-web restart: unless-stopped volumes: - ./element-web/config.json:/app/config.json ports: - 8080:80 # Được reverse proxy ra chat.tenmiencuaban.com nginx: image: nginx:latest container_name: matrix-nginx restart: unless-stopped ports: - 80:80 - 443:443 volumes: - ./nginx/conf.d:/etc/nginx/conf.d - ./nginx/certs:/etc/nginx/certs
3. Tạo file cấu hình config.json cho Element
{ "default_server_config": { "m.homeserver": { "base_url": "https://chat.tenmiencuaban.com", "server_name": "chat.tenmiencuaban.com" } }, "default_theme": "light", "disable_custom_urls": true, "disable_guests": true, "brand": "WebQuangNam Chat", "show_labs_settings": false, "features": {}, "default_country_code": "VN" }
4. Cấu hình Nginx reverse proxy + SSL
Tạo thư mục:
mkdir -p /opt/matrix/nginx/conf.d
mkdir -p /opt/matrix/nginx/certs
Tạo file /opt/matrix/nginx/conf.d/chat.conf
:
5. Cấp SSL với Let’s Encrypt (nếu bạn chưa có SSL)
Cài Certbot + tạo SSL:
sudo apt install certbot certbot certonly --standalone -d chat.tenmiencuaban.com # Sau khi có: # Copy SSL vào nginx/certs/ cp /etc/letsencrypt/live/chat.tenmiencuaban.com/fullchain.pem /opt/matrix/nginx/certs/ cp /etc/letsencrypt/live/chat.tenmiencuaban.com/privkey.pem /opt/matrix/nginx/certs/
6. Khởi động Docker Compose
cd /opt/matrix docker-compose up -d
7. Tạo tài khoản quản trị viên
docker exec -it synapse register_new_matrix_user -c /data/homeserver.yaml http://localhost:8008 # Ví dụ: # Username: admin # Password: matkhau123 # Make admin: yes
✅ Kiểm tra kết quả
Truy cập: https://chat.tenmiencuaban.com
Đăng nhập tài khoản đã tạo
Dùng thử, tạo phòng chat, mời người khác…