使用 SSH 登入 VPS
ssh root@你的ip位置
安裝必要的環境
更新系統並安裝基本工具
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl wget git unzip
安裝 Nginx
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
安裝 PHP和相關模組
sudo apt install -y php php-fpm php-mysql php-cli php-curl php-xml php-mbstring
sudo systemctl start php8.3-fpm
sudo systemctl enable php8.3-fpm
安裝 MySQL
sudo apt install -y mysql-server
sudo mysql_secure_installation
進入 MySQL 並創建資料庫與用戶
sudo mysql -u root -p
創建資料庫並設定 WordPress
CREATE DATABASE 輸入要新增的database名稱;
CREATE USER '輸入使用者名稱'@'localhost' IDENTIFIED BY '輸入密碼';
GRANT ALL PRIVILEGES ON 輸入要新增的database名稱.* TO '輸入使用者名稱'@'localhost';
FLUSH PRIVILEGES;
EXIT;
下載 WordPress
進入 /var/www 目錄
cd /var/www/
mkdir 資料夾名稱
進入伺服器的網站根目錄(如 /var/www/mywordpress/)
www後接要安裝目錄的位置,本例是 mywordpress
cd /var/www/mywordpress/
下載最新版本的 WordPress
使用 wget 下載最新的 WordPress,解壓縮下載的檔案
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
解壓縮後會出現一個 wordpress 資料夾,這就是網站的根目錄。
設定檔案權限
sudo chown -R www-data:www-data /var/www/mywordpress
sudo chmod -R 755 /var/www
移除壓縮檔
為了節省空間,移除剛下載的 latest.tar.gz
rm latest.tar.gz
配置 WordPress 的 wp-config.php 文件
進入 mywordpress 資料夾,並複製 wp-config-sample.php 為 wp-config.php
cp wp-config-sample.php wp-config.php
編輯 wp-config.php
sudo vim wp-config.php
修改以下內容
define('DB_NAME', 'your_db'); // 對應資料庫名稱
define('DB_USER', 'your_user'); // 對應資料庫用戶
define('DB_PASSWORD', 'your_password'); // 對應資料庫密碼
define('DB_HOST', 'localhost');
打開 WordPress 網站完成安裝
- 在瀏覽器中訪問 http://your_domain_or_ip。
- 根據提示輸入資料庫資訊,完成安裝。
Database Name: your_db
Username: your_username
Password: your_password
Database Host: localhost
配置 Nginx
設定 wordpress 虛擬主機
創建一個新檔案 /etc/nginx/sites-available/mywordpress,內容如下
sudo vim /etc/nginx/sites-available/mywordpress
server {
server_name vialley.com www.vialley.com; # 還沒有網址可先輸入 ip
root /var/www/mywordpress/wordpress;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; # 確保版本正確
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/vialley.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/vialley.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.vialley.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = vialley.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name vialley.com www.vialley.com;
return 404; # managed by Certbot
# 靜態資源快取
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|otf|eot|webp|avif)$ {
expires 30d; # 靜態資源快取有效期 30 天
add_header Cache-Control "public, max-age=2592000";
add_header Last-Modified $date_gmt;
add_header ETag $upstream_http_etag;
access_log off; # 禁用靜態資源的訪問日誌
}
# 動態頁面快取
location / {
try_files $uri $uri/ /index.php;
add_header X-Cache-Enabled "true";
add_header X-SRCache-Store-Status $upstream_cache_status;
}
# PHP 處理
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock; # 根據 PHP 版本調整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
啟用站點設定
創建一個符號連結,將新配置檔案從 sites-available 連結到 sites-enabled
對其他站點執行相同操作,僅需修改 server_name 和 root
ln -s /etc/nginx/sites-available/mywordpress /etc/nginx/sites-enabled/
啟用並重啟 Nginx
測試配置是否正確
nginx -t
如果配置正確,會顯示以下訊息
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果沒有錯誤,重新啟動 Nginx
sudo systemctl reload nginx
開啟防火牆的 HTTP/HTTPS 端口
ufw allow 'Nginx Full'
優先使用 sudo systemctl reload nginx,以平滑更新。
若有問題或需要強制更新,再用 sudo systemctl restart nginx。 修改網站內容(如代碼或媒體文件)時,無需重啟或重新加載 Nginx。
配置域名與 SSL
設定 Namecheap DNS
- 為每個網站設置 A 記錄指向 Hostinger VPS 的 IP
- vialley.com → 123.12.12.30(您的 VPS IP)
等待 DNS 記錄生效(通常需要幾分鐘至數小時)
啟用 HTTPS設置 SSL 憑證(建議使用 Let’s Encrypt
使用 Certbot 設定免費 SSL
遵循指示完成 SSL 證書的安裝
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d vialley.com -d www.vialley.com
以上,大功告成