如何在 Linux VPS 上使用 Nginx 安裝 WordPress

作者 ONIE 工程團隊 · 更新於 2026年11月12日 · 閱讀時間 1 分鐘

本指南說明如何在 Ubuntu 22.04 或 24.04 VPS 上以 LEMP 架構部署 WordPress:Linux、Nginx、MariaDB 與 PHP-FPM。2 vCPU 與 2 GB RAM 的伺服器是相當舒適的起點;請參閱如何選擇 VPS 規格。您可以在幾分鐘內部署雲端 VPS,並照著本指南操作。開始前,請將 example.com 替換成您的網域,並先把該網域的 DNS A 記錄指向伺服器 IP。

1. 更新伺服器並開啟防火牆

sudo apt update && sudo apt upgrade -y
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

如果尚未強化 SSH 安全設定,請先參閱 SSH 強化指南。

2. 安裝 Nginx、MariaDB 與 PHP

sudo apt install -y nginx mariadb-server php-fpm php-mysql php-curl php-gd php-mbstring php-xml php-zip php-intl unzip curl
sudo mysql_secure_installation

3. 建立資料庫

sudo mysql
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'USE_A_LONG_RANDOM_PASSWORD';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

4. 下載 WordPress

cd /tmp && curl -O https://wordpress.org/latest.tar.gz
tar xzf latest.tar.gz
sudo mv wordpress /var/www/example.com
sudo chown -R www-data:www-data /var/www/example.com
cd /var/www/example.com && sudo -u www-data cp wp-config-sample.php wp-config.php

編輯 wp-config.php:將 DB_NAME、DB_USER 與 DB_PASSWORD 設為上方的值,並從官方產生器 https://api.wordpress.org/secret-key/1.1/salt/ 取得新的驗證金鑰,取代檔案中原有的金鑰。

5. 設定 Nginx

建立 /etc/nginx/sites-available/example.com:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.php;

    location / { try_files $uri $uri/ /index.php?$args; }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }
    location ~ /\.ht { deny all; }
}
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

如果 /run/php/php-fpm.sock 不存在,請改用含版本號的 socket,例如 /run/php/php8.3-fpm.sock。

6. 加入免費 HTTPS 憑證

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Certbot 會安裝一個定時器(timer),自動更新憑證。您可以使用 sudo certbot renew --dry-run 進行檢查。

7. 在瀏覽器完成安裝並強化安全性

開啟 https://example.com,並完成安裝程式。接著:

  • 持續更新 WordPress、佈景主題與外掛,並移除不使用的項目。
  • 使用高強度且各自獨立的管理員密碼,並啟用雙因素登入。
  • 每次重大更新前,先建立快照或備份。
  • 安裝快取外掛,以減輕小型 VPS 的負載。

常見問題

我應該使用哪個 PHP 版本?

請使用您 Ubuntu 版本內建的 PHP 版本,前提是 WordPress 與您的外掛都支援該版本。請查閱官方的系統需求頁面。

一台 VPS 可以架設多個網站嗎?

可以。每個網站各建立一個 Nginx server 區塊、一個資料庫與一個目錄,並確保伺服器有足夠的 RAM 供其使用。

為什麼會看到 502 Bad Gateway?

Nginx 無法連線到 PHP-FPM。請確認 PHP-FPM 正在執行,且 Nginx 設定檔中的 socket 路徑與現存的 socket 相符。

參考資料與延伸閱讀

更多 VPS 指南