Aug 1, 2026 · 2 min read
Setup home server
serverubuntusetup
Có cái PC vứt xó không dùng nên hôm nay lôi ra vọc vạch cài nó làm server chạy serving các kiểu hehe
Cài Ubuntu Server
Lên trang chủ Ubuntu và tải về phiên bản server (hoặc bản thường cũng được) ⇒ Cài vào máy
Setup Ubuntu cơ bản
Trỏ tên miền vào server
Setup Cloudflare
- Mua tên miền ở nguồn bất kỳ: PaVietnam, goDaddy…
- Trỏ tên miền vào Cloudflare Dashboard (search Google rất nhiều guideline)
Cấu hình trên máy
- Cài cloudflared trên máy
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb sudo dpkg -i cloudflared.deb - Login tạo & tạo tunnel
cloudflared tunnel login # Mở browser đăng nhập rôif chạy tiếp cloudflared tunnel create home-serverLúc này output sẽ có 1 file id json, đường dẫn thường nằm ở ~/cloudflared/<TUNNEL_ID>.json
- Tạo DNS cho tunnel
cloudflared tunnel route dns home-server yourdomain.comVerify bằng lệnh dig yourdomain.com
- Tạo file config cho cloudflared
vim ~/.cloudflared/config.ymltunnel: <TUNNEL_ID> credentials-file: /home/username/.cloudflared/<TUNNEL_ID>.json ingress: - hostname: ssh.home.yourdomain.com service: ssh://localhost:22 - service: http_status:404Lưu lại bằng :wq
Chạy lệnh này để khởi tạo route tunnel
cloudflared tunnel route dns home-server ssh.home.yourdomain.com - Tạo service để chạy cùng hệ thống
sudo mkdir -p /etc/cloudflared sudo cp ~/.cloudflared/config.yml /etc/cloudflared/config.yml sudo cp ~/.cloudflared/<TUNNEL_ID>.json /etc/cloudflared/Validate xem được chưa bằng lệnh này, ra OK là được
cloudflared tunnel --config /etc/cloudflared/config.yml ingress validateChạy lệnh này để cài service
sudo cloudflared service install sudo systemctl enable cloudflared sudo systemctl start cloudflared sudo systemctl status cloudflared
Deploy 1 service
Cho phép app restart tunnel mỗi lần mà không cần cài pass
Setup chạy nền vĩnh viễn
sudo vi /etc/systemd/system/<service_name>.service[Unit]
Description=Service Description
After=network.target
[Service]
User=username
WorkingDirectory=/path/to/your-service
Environment="ENV1=content"
Environment="ENV2=content"
EnvironmentFile=/path/to/env-file
ExecStart=/usr/bin/python3 app.py
Restart=always
[Install]
WantedBy=multi-user.targetSau đó chạy lệnh bash này để đăng ký service
sudo systemctl enable --now <service_name>Cấu hình sub-domain
sudo vi /etc/cloudflared/config.ymlThêm sub-domain và port sau đó verify
cloudflared tunnel --config /etc/cloudflared/config.yml ingress validateRoute DNS cho sub-domain
cloudflared tunnel route dns home-server ten-subdomain-moi.your-domainRestart service
sudo systemctl restart cloudflared
sudo systemctl status cloudflaredLệnh watch log của service
journalctl -u <SERVICE_NAME> -f