Nguyen Quang Thao

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

  1. Mua tên miền ở nguồn bất kỳ: PaVietnam, goDaddy…
  2. Trỏ tên miền vào Cloudflare Dashboard (search Google rất nhiều guideline)

Cấu hình trên máy

  1. 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
  2. Login tạo & tạo tunnel
    cloudflared tunnel login
    # Mở browser đăng nhập rôif chạy tiếp 
    cloudflared tunnel create home-server

    Lúc này output sẽ có 1 file id json, đường dẫn thường nằm ở ~/cloudflared/<TUNNEL_ID>.json

  3. Tạo DNS cho tunnel
    cloudflared tunnel route dns home-server yourdomain.com

    Verify bằng lệnh dig yourdomain.com

  4. Tạo file config cho cloudflared
    vim ~/.cloudflared/config.yml
    tunnel: <TUNNEL_ID>
    credentials-file: /home/username/.cloudflared/<TUNNEL_ID>.json
    
    ingress:
      - hostname: ssh.home.yourdomain.com
        service: ssh://localhost:22
      - service: http_status:404

    Lư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
  5. 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 validate

    Chạ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.target

Sau đó 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.yml

Thêm sub-domain và port sau đó verify

cloudflared tunnel --config /etc/cloudflared/config.yml ingress validate

Route DNS cho sub-domain

cloudflared tunnel route dns home-server ten-subdomain-moi.your-domain

Restart service

sudo systemctl restart cloudflared
sudo systemctl status cloudflared

Lệnh watch log của service

journalctl -u <SERVICE_NAME> -f