以下提供一份完整的 Ubuntu 22.04 Server 安裝與部署指南,涵蓋前端 (amis + HTML5)、後端 (Node.js API)、資料庫 (MariaDB 11.x)、Nginx 反向代理設定,讓外部能直接訪問你的網站。此教學將以 乾淨安裝的 Ubuntu 22.04 為基礎。
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget unzip git ufw
Ubuntu 22.04 預設是 MariaDB 10.x,需要安裝 11.x 官方版本。
sudo apt install -y software-properties-common dirmngr
sudo curl -fsSL https://mariadb.org/mariadb_release_signing_key.asc | sudo gpg --dearmor -o /usr/share/keyrings/mariadb-keyring.gpg
sudo sh -c "echo 'deb [signed-by=/usr/share/keyrings/mariadb-keyring.gpg] https://mirror.mariadb.org/repo/11.5/ubuntu jammy main' > /etc/apt/sources.list.d/mariadb.list"
sudo apt update
sudo apt install -y mariadb-server mariadb-client
sudo systemctl enable mariadb
sudo systemctl start mariadb
sudo mysql_secure_installation
完成後可測試:
mysql -u root -p
建議使用 Node.js 20 LTS。
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs build-essential
檢查版本:
node -v
npm -v
假設你的專案放在 /var/www/myapp:
sudo mkdir -p /var/www/myapp
sudo chown $USER:$USER /var/www/myapp
cd /var/www/myapp
git clone https://your-repo.git .
npm install
node server.js
或 (若使用 npm scripts)
npm start
sudo npm install -g pm2
cd /var/www/myapp
pm2 start server.js --name myapp
pm2 startup systemd
pm2 save
✅ PM2 可以確保 Node.js 斷電或崩潰後自動重啟。
sudo apt install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
檢查是否能打開預設頁面:
http://你的伺服器IP
建立設定檔:
sudo nano /etc/nginx/sites-available/myapp
內容範例(HTTP 80 版本):
server {
listen 80;
server_name mydomain.com; # 改成你的域名或伺服器IP
root /var/www/myapp/public; # 前端 HTML5 / amis 靜態檔案路徑
index index.html index.htm;
location /api/ {
proxy_pass http://127.0.0.1:3000/; # 你的 Node.js API 埠號
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / {
try_files $uri /index.html;
}
}
啟用站點:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
sudo ufw allow OpenSSH
sudo ufw allow 80
sudo ufw allow 443
sudo ufw enable
sudo ufw status
如果有域名,可加上免費 SSL:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d mydomain.com -d www.mydomain.com
SSL 會自動設定 Nginx,並啟用 HTTPS。
http://你的IP/api/ 存取http://你的IP 正常載入✅ 完成後,你的網站將能直接透過 IP 或域名連線,Nginx 會處理靜態前端頁面,並反向代理到 Node.js API,資料由 MariaDB 提供。
部署架構圖 (Ubuntu + Nginx + Node.js + MariaDB)
以下是一個基於 Ubuntu 的部署架構圖文字描述,您可以使用工具如 Draw.io、Lucidchart 或 Visio 將其可視化:
+-----------------------------------------------------------------------+
| Ubuntu Server (22.04 LTS) |
| |
| +---------------------+ +---------------------+ |
| | Nginx (80/443) | | Node.js (App) | |
| | - Reverse Proxy |<---->| - Port: 3000 | |
| | - SSL Termination | | - PM2 Process Mgr | |
| | - Static Files | +---------------------+ |
| +---------------------+ |
| | |
| v |
| +---------------------+ |
| | MariaDB (3306) | |
| | - Database Server |<--------------------------------------------+
| | - Remote Access | (Node.js App 連接 MariaDB 存取資料) |
| +---------------------+ |
| |
+-----------------------------------------------------------------------+
Ubuntu Server
Nginx
http://localhost:3000)。Node.js 應用
3000),由 PM2 管理進程。MariaDB
3306 端口,儲存應用數據。ufw 開放必要端口(80, 443, 22)。bind-address 和防火牆規則。fail2ban 防護 SSH。mysqldump)。如果需要更詳細的圖形或具體配置步驟,可以進一步說明!