在 EC2 上創建 WordPress 伺服器 (LEMP)
用 AWS EC2 建立新伺服器
建立新個體
- 開啟 AWS
- 進入 EC2
- 在右上角選擇伺服器建置地區
- 點選「啟動執行個體」以建立新個體
- AMI (Amazon Machine Image; 應用程式和作業系統映像)
選擇 Ubuntu Server 24.04 LTS (HVM), SSD Volume Type. 64-bit (x86) - 網路設定
- 設定儲存:1x 30 GiB gp3 根磁碟區
- 啟動執行個體
網路設定
防火牆(安全群組)
→ 傳入規則:
開放 SSL(TCP 22)/HTTP(TCP 80)/HTTPS(TCP 443)
安裝
使用 SSH 連線
ssh -i .../....pem ubuntu@XXX.XXX.XXX.XXXHost blog.a1go.ai
HostName XXX.XXX.XXX.XXX
User ubuntu
IdentityFile .../....pem
⚠️ VS Code Remote – SSH 會需要在 EC2 安裝VS Code Server
建議用Ctrl+Shift+‵開新 Terminal 使用ssh指令
※ Amazon Linux 使用者名稱為 ec2-user 而非 ubuntu 1Default user names
安裝與設定 LEMP
- 更新系統套件:
sudo apt update && sudo apt upgrade -y - Nginx:
- 安裝 Nginx:
sudo apt install -y nginx - 確認 Nginx 狀態:
sudo systemctl status nginx⇒ Active: active (running)
- 安裝 Nginx:
- MySQL:
- 安裝 MySQL:
sudo apt install -y mysql-server - 確認 MySQL 狀態:
sudo systemctl status mysql⇒ Active: active (running)
- 安裝 MySQL:
- PHP:
- 安裝 PHP:
sudo apt install -y php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-gd php-intl php-bcmath - 確認 PHP狀態:
sudo systemctl status php8.3-fpm
- 安裝 PHP:
- 安裝 WordPress:
cd ~
wget https://wordpress.org/latest.tar.gz
tar -xzf latest.tar.gz
sudo mv wordpress /var/www/html/wordpress
sudo chown -R www-data:www-data /var/www/html/wordpress
sudo chmod -R 755 /var/www/html/wordpress
rm latest.tar.gz - 設定 MySQL:
sudo mysqlCREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '<PASSWORD>';GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';FLUSH PRIVILEGES;EXIT;
- 設定
wp-config.php:- 自範本建立
wp-config.php並開啟:
sudo cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
sudo nano /var/www/html/wordpress/wp-config.php - 找到下列三行,並修改:
define( ‘DB_NAME’, ‘wordpress‘ );
define( ‘DB_USER’, ‘wpuser‘ );
define( ‘DB_PASSWORD’, ‘<PASSWORD>‘ );
- 自範本建立
- 設定 Nginx Virtual Host:
sudo nano /etc/nginx/sites-available/wordpress- 貼上:
server { listen 80; server_name _; root /var/www/html/wordpress; index index.php index.html; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ { expires 30d; add_header Cache-Control "public, no-transform"; } client_max_body_size 64M; }
- 啟用設定並重載 Nginx:
sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t
sudo systemctl reload nginx
⇒nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
安裝 phpMyAdmin
sudo apt install -y phpmyadminWeb server to reconfigure automatically:[ ] apache2
[ ] lighttpd<Ok>
→ 兩個都不選,Tab至<Ok>- Configure database for phpmyadmin with dbconfig-common?
→<Yes>
更改 phpMyAdmin 的 Port
sudo nano /etc/nginx/sites-available/phpmyadmin- 貼上:
server { listen <PORT>; server_name _; root /usr/share/phpmyadmin; index index.php index.html; location / { try_files $uri $uri/ =404; } location ~ \.php$ { fastcgi_pass unix:/run/php/php8.3-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } } - 啟用設定並重載 Nginx:
sudo ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
⇒nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful - 新增安全群組:
-
安全群組名稱:
TCP/<PORT> -
描述:
phpMyAdmin -
類型:
自訂 TCP - 連接埠範圍:
<PORT> - 來源:
隨處 - IPv4
-
- 執行個體 → 右鍵 → 變更安全群組 → 新增安全群組
TCP/<PORT>
⭐ (建立並啟用 SWAP)
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab- 確認 Swap 是否已建立並啟用:
free -h
(建立 AMI 映像)
- 執行個體 → 右鍵 → 映像和範本 → 建立映像
- 描述:
LEMP Stack - Ubuntu 24.04 / Nginx / MySQL / PHP / WordPress / phpMyAdmin
(移轉 WordPress)
All-in-One WP Migration
變更上傳檔案大小限制
- 編輯 Nginx 設定
sudo nano /etc/nginx/sites-available/wordpressclient_max_body_size 1024M;
- 編輯 PHP 設定
- sudo nano /etc/php/8.3/fpm/php.ini
upload_max_filesize = 1024M
post_max_size = 1024M
memory_limit = 1024M
max_execution_time = 300
- 重啟 Nginx 和 PHP 讓設定生效:
sudo systemctl reload nginx
sudo systemctl restart php8.3-fpm
域名設定
設定 Host Records
取得 SSL 憑證
- 安裝 Certbot:
sudo apt install -y certbot python3-certbot-nginx - 更改 Nginx 設定:
sudo nano /etc/nginx/sites-available/wordpressserver_name _;→server_name {DOMAIN_NAME};sudo nginx -t && sudo systemctl reload nginx
- 申請 SSL 憑證:
sudo certbot --nginx -d blog.a1go.ai
Last Updated on 2026/05/08 by A1go