步驟 2:設定要部署到 Amazon Linux 或 Red Hat Enterprise Linux Amazon EC2執行個體的來源內容 - AWS CodeDeploy

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

步驟 2:設定要部署到 Amazon Linux 或 Red Hat Enterprise Linux Amazon EC2執行個體的來源內容

現在,您可以設定應用程式的來源內容,以將某個項目部署至執行個體。

取得原始程式碼

在本教學課程中,您將 WordPress 內容發佈平台從開發機器部署到目標 Amazon EC2執行個體。若要取得 WordPress 原始程式碼,您可以使用內建命令列呼叫。或者,如果您已於開發電腦上安裝 Git,可以改為使用該項目。

對於這些步驟,我們假設您已將 WordPress 原始程式碼的複本下載到開發機器上的/tmp目錄。(您可以選擇想要的任何目錄,但請記得將這些步驟中指定的所有 /tmp 都替代為您的位置)。

選擇下列兩個選項之一,將 WordPress 來源檔案複製到您的開發機器。第一個選項使用內建命令列呼叫。第二個選項使用 Git。

取得 WordPress 原始程式碼的副本 (內建命令列呼叫)

  1. 呼叫 wget命令,將 WordPress 原始程式碼的複本下載為 .zip 檔案,並下載至目前的目錄:

    wget https://github.com/WordPress/WordPress/archive/master.zip
  2. 呼叫 unzipmkdircprm 命令,以:

    • master .zip 檔案解壓縮至 /tmp/WordPress_Temp 目錄 (資料夾)。

    • 將其解壓縮的內容複製至 /tmp/WordPress 目標資料夾。

    • 刪除暫時 /tmp/WordPress_Temp 資料夾和 master 檔案。

    一次執行一個命令:

    unzip master -d /tmp/WordPress_Temp
    mkdir -p /tmp/WordPress
    cp -paf /tmp/WordPress_Temp/WordPress-master/* /tmp/WordPress
    rm -rf /tmp/WordPress_Temp
    rm -f master

    這可讓您在 /tmp/WordPress 資料夾中擁有一組乾淨的 WordPress 原始程式碼檔案。

若要取得 WordPress 原始碼 (Git) 的副本

  1. 在開發電腦上,下載並安裝 Git

  2. /tmp/WordPress 資料夾中,呼叫 git init 命令。

  3. 呼叫 git clone命令來複製公 WordPress有儲存庫,在/tmp/WordPress目的地資料夾中製作自己的儲存庫複本:

    git clone https://github.com/WordPress/WordPress.git /tmp/WordPress

    這可讓您在 /tmp/WordPress 資料夾中擁有一組乾淨的 WordPress 原始程式碼檔案。

建立指令碼以執行應用程式

接下來,在目錄中建立資料夾和指令碼。 CodeDeploy 使用這些指令碼在目標 Amazon EC2執行個體上設定和部署應用程式修訂版。您可以使用任何文字編輯器來建立指令碼。

  1. 在 WordPress 原始程式碼的複本中建立指令碼目錄:

    mkdir -p /tmp/WordPress/scripts
  2. /tmp/WordPress/scripts 中,建立 install_dependencies.sh 檔案。在檔案中新增下列各行。此install_dependencies.sh指令碼會安裝 ApacheSQL、我的 和 PHP。它也會將我的SQL支援新增至 PHP。

    #!/bin/bash sudo amazon-linux-extras install php7.4 sudo yum install -y httpd mariadb-server php
  3. /tmp/WordPress/scripts 中,建立 start_server.sh 檔案。在檔案中新增下列各行。此start_server.sh指令碼會啟動 Apache 和 My SQL。

    #!/bin/bash systemctl start mariadb.service systemctl start httpd.service systemctl start php-fpm.service
  4. /tmp/WordPress/scripts 中,建立 stop_server.sh 檔案。在檔案中新增下列各行。此stop_server.sh指令碼會停止 Apache 和 My SQL。

    #!/bin/bash isExistApp="pgrep httpd" if [[ -n $isExistApp ]]; then systemctl stop httpd.service fi isExistApp=pgrep mysqld if [[ -n $isExistApp ]]; then systemctl stop mariadb.service fi isExistApp=pgrep php-fpm if [[ -n $isExistApp ]]; then systemctl stop php-fpm.service fi
  5. /tmp/WordPress/scripts 中,建立 create_test_db.sh 檔案。在檔案中新增下列各行。此create_test_db.sh指令碼使用 MySQL 建立test資料庫 WordPress 以供 使用。

    #!/bin/bash mysql -uroot <<CREATE_TEST_DB CREATE DATABASE IF NOT EXISTS test; CREATE_TEST_DB
  6. 最後,在 /tmp/WordPress/scripts 中,建立 change_permissions.sh 指令碼。這用來在 Apache 中變更資料夾許可。

    重要

    此指令碼已更新 /tmp/WordPress 資料夾的許可,讓任何人都可以寫入其中。這是必要的,以便 WordPress 可以在 期間寫入其資料庫步驟 5:更新並重新部署您的 WordPress 應用程式。設定 WordPress 應用程式後,請執行下列命令,將許可更新為更安全的設定:

    chmod -R 755 /var/www/html/WordPress
    #!/bin/bash chmod -R 777 /var/www/html/WordPress
  7. 給予所有指令碼可執行的許可。在命令列輸入:

    chmod +x /tmp/WordPress/scripts/*

新增應用程式規格檔案

接下來,新增應用程式規格檔案 (AppSpec 檔案),由 YAML用來 CodeDeploy :

  • 將應用程式修訂版中的來源檔案映射到目標 Amazon EC2執行個體上的目的地。

  • 指定已部署檔案的自訂許可。

  • 指定部署期間要在目標 Amazon EC2執行個體上執行的指令碼。

AppSpec 檔案必須命名為 appspec.yml。它必須放置在應用程式原始碼的根目錄中。在此教學課程中,根目錄是 /tmp/WordPress

使用文字編輯器,建立名為 appspec.yml 的檔案。在檔案中新增下列各行:

version: 0.0 os: linux files: - source: / destination: /var/www/html/WordPress hooks: BeforeInstall: - location: scripts/install_dependencies.sh timeout: 300 runas: root AfterInstall: - location: scripts/change_permissions.sh timeout: 300 runas: root ApplicationStart: - location: scripts/start_server.sh - location: scripts/create_test_db.sh timeout: 300 runas: root ApplicationStop: - location: scripts/stop_server.sh timeout: 300 runas: root

CodeDeploy 使用此 AppSpec 檔案,將開發機器上的/tmp/WordPress資料夾中的所有檔案複製到目標 Amazon EC2執行個體上的/var/www/html/WordPress資料夾。在部署期間, CodeDeploy 會在部署生命週期root內於指定事件執行指定的指令碼,如目標 Amazon EC2執行個體的/var/www/html/WordPress/scripts資料夾中的 BeforeInstallAfterInstall。如果任何這些指令碼需要超過 300 秒 (5 分鐘) 才能執行, 會 CodeDeploy 停止部署並將部署標記為失敗。

如需這些設定的詳細資訊,請參閱CodeDeploy AppSpec 檔案參考

重要

此檔案中每個項目之間的空格位置和數目十分重要。如果間距不正確, 會 CodeDeploy 引發可能難以偵錯的錯誤。如需詳細資訊,請參閱AppSpec 檔案間距