AWSCertbot (Let's Encrypt)EC2LinuxMySQLNginxPHPSSLUbuntu 24.04

在 EC2 上創建 WordPress 伺服器 (LEMP)

用 AWS EC2 建立新伺服器

建立新個體

  1. 開啟 AWS
  2. 進入 EC2
  3. 在右上角選擇伺服器建置地區
  4. 點選「啟動執行個體」以建立新個體
  5. AMI (Amazon Machine Image; 應用程式和作業系統映像)
    選擇 Ubuntu Server 24.04 LTS (HVM), SSD Volume Type. 64-bit (x86)
  6. 網路設定
  7. 設定儲存:1x 30 GiB gp3 根磁碟區
  8. 啟動執行個體

網路設定

防火牆(安全群組)
→ 傳入規則:
  開放 SSL(TCP 22)/HTTP(TCP 80)/HTTPS(TCP 443) 

安裝

使用 SSH 連線

  • ssh -i .../....pem ubuntu@XXX.XXX.XXX.XXX
  • Host 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

  1. 更新系統套件:sudo apt update && sudo apt upgrade -y
  2. Nginx:
    1. 安裝 Nginx:sudo apt install -y nginx
    2. 確認 Nginx 狀態:sudo systemctl status nginxActive: active (running)
  3. MySQL:
    1. 安裝 MySQL:sudo apt install -y mysql-server
    2. 確認 MySQL 狀態:sudo systemctl status mysqlActive: active (running)
  4. PHP:
    1. 安裝 PHP:sudo apt install -y php-fpm php-mysql php-xml php-mbstring php-curl php-zip php-gd php-intl php-bcmath
    2. 確認 PHP狀態:sudo systemctl status php8.3-fpm
  5. 安裝 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
  6. 設定 MySQL:sudo mysql
    1. CREATE DATABASE wordpress CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
    2. CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '<PASSWORD>';
    3. GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
    4. FLUSH PRIVILEGES;
    5. EXIT;
  7. 設定wp-config.php
    1. 自範本建立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
    2. 找到下列三行,並修改:
      define( ‘DB_NAME’, ‘wordpress‘ );
      define( ‘DB_USER’, ‘wpuser‘ );
      define( ‘DB_PASSWORD’, ‘<PASSWORD>‘ );
  8. 設定 Nginx Virtual Host:
    1. sudo nano /etc/nginx/sites-available/wordpress
    2. 貼上:
      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;
      }
  9. 啟用設定並重載 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

  1. sudo apt install -y phpmyadmin
  2. Web server to reconfigure automatically: 

     [ ] apache2 
     [ ] lighttpd 

    <Ok>
    → 兩個都不選,Tab<Ok>

  3. Configure database for phpmyadmin with dbconfig-common?
    <Yes>

更改 phpMyAdmin 的 Port

  1. sudo nano /etc/nginx/sites-available/phpmyadmin
  2. 貼上:
    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;
        }
    }
  3. 啟用設定並重載 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
  4. 新增安全群組:
    1. 安全群組名稱:TCP/<PORT>

    2. 描述:phpMyAdmin

    3. 類型:自訂 TCP

    4. 連接埠範圍:<PORT>
    5. 來源:隨處 - IPv4
  5. 執行個體 → 右鍵 → 變更安全群組 → 新增安全群組TCP/<PORT>

⭐ (建立並啟用 SWAP)

  1. 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
  2. 確認 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/wordpress
    • client_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 憑證

  1. 安裝 Certbot:sudo apt install -y certbot python3-certbot-nginx
  2. 更改 Nginx 設定:
    1. sudo nano /etc/nginx/sites-available/wordpress
    2. server_name _;server_name {DOMAIN_NAME};
    3. sudo nginx -t && sudo systemctl reload nginx
  3. 申請 SSL 憑證:sudo certbot --nginx -d blog.a1go.ai

Last Updated on 2026/05/08 by A1go

References

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

目錄