Debian VPS 初始環境設定
輸入您的伺服器資訊,自動生成專屬指令。
更新套件庫並安裝基礎工具,猶如茶道前的潔具。
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget git vim htop ufw
sudo apt install -y build-essential python3 python3-pip
創建一般使用者,避免使用 Root 操作,保持系統的謙遜與安全。
# 建立新使用者
adduser username
# 給予 sudo 權限
usermod -aG sudo username
# 切換身份
su - username
設定 SSH 金鑰登入,捨棄脆弱的密碼。
# 本地端生成金鑰 (若已有可跳過)
ssh-keygen -t rsa -b 4096 -C "email"
# 將公鑰傳送至伺服器
ssh-copy-id username@server_ip
# 測試登入
ssh username@server_ip
配置 UFW 防火牆,僅允許必要的端口。
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
安裝 Nginx,作為通往數位世界的門戶。
sudo apt install nginx -y
sudo systemctl start nginx
sudo systemctl enable nginx
# 檢查狀態
systemctl status nginx
使用 Certbot 獲取 HTTPS 憑證,確保通訊隱密。
sudo apt install certbot python3-certbot-nginx -y
# 自動配置 SSL
sudo certbot --nginx -d domain.com
強制 HTTP 轉向 HTTPS,不留死角。
# 編輯 Nginx 設定 (Certbot 通常會自動處理)
sudo nano /etc/nginx/sites-available/default
# 確認 server 區塊包含:
# return 301 https://$host$request_uri;
# 重啟 Nginx
sudo systemctl reload nginx