燈泡教程 AWS Cloud9 - AWS Cloud9

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

燈泡教程 AWS Cloud9

本教學課程可讓您在AWS Cloud9開發環境Linux中設定和執行 LAMP (MySQL、Apache HTTP 伺服器和PHP)。

遵循本教學課程並建立此範例可能會導致您的AWS 帳戶. 其中包括 Amazon Elastic Compute Cloud (Amazon EC2) 等 AWS 服務的可能費用。如需詳細資訊,請參閱 Amazon EC2 定價

先決條件

在您使用此範例前,請務必確認您的設定符合下列要求:

  • 您必須已有 AWS Cloud9 EC2 開發環境。本範例假設您已具備 EC2 環境,且該環境已連線到執行 Amazon Linux 或 Ubuntu Server 的 Amazon EC2 執行個體。如果您有不同類型的環境或作業系統,您可能需要依照此範例的說明來設定相關工具。如需詳細資訊,請參閱在 AWS Cloud9 中建立環境

  • 您現有環境的 AWS Cloud9 IDE 已開啟。當您開啟環境時,AWS Cloud9 會在 Web 瀏覽器中開啟該環境的 IDE。如需詳細資訊,請參閱在 AWS Cloud9 中開啟環境

步驟 1:安裝工具

在此步驟中,您將安裝下列工具:

  • Apache HTTP Server,這是 Web 伺服器主機。

  • PHP,這是一種指令碼語言,尤其適用於 Web 開發並可內嵌到 HTML。

  • MySQL,這是一種關聯式資料庫管理系統。

然後,先啟動 Apache HTTP Server 再啟動 MySQL,來完成此步驟。

  1. 確保已在執行個體上安裝最新安全性更新和錯誤修正。若要這樣做,請在 AWS Cloud9 IDE 的終端機工作階段中,執行 yum update (若為 Amazon Linux) 或 apt update (若為 Ubuntu Server) 命令。(若要啟動新終端機工作階段,請在選單列上,選擇 Window (視窗)、New Terminal (新增終端機)。

    針對 Amazon Linux:

    sudo yum -y update

    針對 Ubuntu Server:

    sudo apt -y update
  2. 檢查是否已安裝 Apache HTTP Server。若要這麼做,請執行 httpd -v (若為 Amazon Linux) 或 apache2 -v (若為 Ubuntu Server) 命令。

    如果成功,輸出會包含 Apache HTTP Server 版本號碼。

    如果您看到錯誤,則執行 install 命令來安裝 Apache HTTP Server。

    針對 Amazon Linux:

    sudo yum install -y httpd24

    針對 Ubuntu Server:

    sudo apt install -y apache2
  3. 執行 php -v 命令,確認是否已安裝 PHP。

    如果成功,輸出會包含 PHP 版本號碼。

    如果您看到錯誤,則執行 install 命令來安裝 PHP。

    針對 Amazon Linux:

    sudo yum install -y php56

    針對 Ubuntu Server:

    sudo apt install -y php libapache2-mod-php php-xml
  4. 執行 mysql --version 命令,確認是否已安裝 MySQL。

    如果成功,輸出會包含 MySQL 版本編號。

    如果您看到錯誤,則執行 install 命令來安裝 MySQL。

    針對 Amazon Linux:

    sudo yum install -y mysql-server

    針對 Ubuntu Server:

    sudo apt install -y mysql-server
  5. 在您安裝 Apache HTTP Server、PHP 和 MySQL 之後,請啟動 Apache HTTP Server,然後執行以下命令來確認它已啟動。

    若為 Amazon Linux (您可能需要執行此命令兩次):

    sudo service httpd start && sudo service httpd status

    若為 Ubuntu Server (返回命令提示字元,按下 q):

    sudo service apache2 start && sudo service apache2 status
  6. 啟動 MySQL,然後執行以下命令來確認它已啟動。

    針對 Amazon Linux:

    sudo service mysqld start && sudo service mysqld status

    若為 Ubuntu Server (返回命令提示字元,按下 q):

    sudo service mysql start && sudo service mysql status

步驟 2:設定 MySQL

在此步驟中,您設定 MySQL 以遵循 MySQL 安全最佳實務。這些安全最佳實務包括為根帳戶設定密碼,以及移除可從本機主機外部存取的根帳戶。要注意的其他最佳實務是移除匿名使用者、移除測試資料庫,以及移除允許任何人存取名稱開頭為 test_ 之資料庫的權限。

然後,練習啟動與結束 MySQL 命令列用戶端,即可完成此步驟。

  1. 在 AWS Cloud9 IDE 的終端機工作階段中執行以下命令,針對 MySQL 安裝實作 MySQL 安全最佳實務。

    sudo mysql_secure_installation
  2. 出現提示時,回答下列指定的問題。

    針對 Amazon Linux:

    1. 輸入根目錄目前使用的密碼 (若無密碼,請按 Enter) – 按 Enter (若沒有密碼)。

    2. 設定根密碼 - 輸入 Y,然後按 Enter

    3. 新密碼 - 輸入密碼,然後按 Enter

    4. 重新輸入新密碼 - 重新輸入密碼,然後按 Enter。(請務必將密碼存放在安全位置,以供日後使用。)

    5. 移除匿名使用者 - 輸入 Y,然後按 Enter

    6. 不允許從遠端進行根登入 - 輸入 Y,然後按 Enter

    7. 移除測試資料庫和其存取權 - 輸入 Y,然後按 Enter

    8. 立即重新載入權限表格 - 輸入 Y,然後按 Enter

    針對 Ubuntu Server:

    1. 是否要設定 VALIDATE PASSWORD 外掛程式 - 輸入 y,然後按 Enter

    2. 有三種層級的密碼驗證政策 - 輸入 012,然後按 Enter

    3. 新密碼 - 輸入密碼,然後按 Enter

    4. 重新輸入新密碼 - 重新輸入密碼,然後按 Enter。請務必將密碼存放在安全位置,以供日後使用。

    5. 是否要繼續使用已提供的密碼 - 輸入 y,然後按 Enter

    6. 移除匿名使用者 - 輸入 y,然後按 Enter

    7. 不允許從遠端進行根登入 - 輸入 y,然後按 Enter

    8. 移除測試資料庫和其存取權 - 輸入 y,然後按 Enter

    9. 立即重新載入權限表格 - 輸入 y,然後按 Enter

  3. 若要直接與 MySQL 互動,請執行以下命令,以根使用者身分啟動 MySQL 命令列用戶端。出現提示時,輸入您先前設定的根使用者密碼,然後按 Enter。當您使用 MySQL 命令列用戶端時,提示會變更為 mysql>

    sudo mysql -uroot -p
  4. 若要結束 MySQL 命令列用戶端,請執行以下命令。提示會變回 $

    exit;

步驟 3:設定網站

在此步驟中,您將使用建議的擁有者和存取許可來設定 Apache HTTP Server 的預設網站根目錄。然後,在預設網站根目錄內建立 PHP 型網頁。

接著,啟用傳入的 Web 流量以檢視該網頁,方法是在 Amazon EC2 中設定安全群組,並在與此 EC2 環境相關聯的 Amazon Virtual Private Cloud (Amazon VPC) 中設定網路存取控制清單 (網路 ACL)。每個 EC2 環境必須同時與 Amazon EC2 中的安全群組以及 Amazon VPC 中的網路 ACL 相關聯。不過,即使 AWS 帳戶中的預設網路 ACL 允許環境的所有傳入和傳出流量,預設安全群組仍僅允許透過連接埠 22 使用 SSH 的傳入流量。如需詳細資訊,請參閱AWS Cloud9 開發環境的 VPC 設定

接著,從 AWS Cloud9 IDE 外部成功檢視網頁,您就能完成此步驟。

  1. 使用建議的擁有者和存取許可來設定 Apache HTTP Server 的預設網站根目錄 (/var/www/html)。若要這樣做,在 AWS Cloud9 IDE 的終端機工作階段中依序執行以下六個命令。若要了解每個命令的作用,請閱讀每個命令的 # 字元後面的資訊。

    針對 Amazon Linux:

    sudo groupadd web-content # Create a group named web-content. sudo usermod -G web-content -a ec2-user # Add the user ec2-user (your default user for this environment) to the group web-content. sudo usermod -G web-content -a apache # Add the user apache (Apache HTTP Server) to the group web-content. sudo chown -R ec2-user:web-content /var/www/html # Change the owner of /var/www/html and its files to user ec2-user and group web-content. sudo find /var/www/html -type f -exec chmod u=rw,g=rx,o=rx {} \; # Change all file permissions within /var/www/html to user read/write, group read-only, and others read/execute. sudo find /var/www/html -type d -exec chmod u=rwx,g=rx,o=rx {} \; # Change /var/www/html directory permissions to user read/write/execute, group read/execute, and others read/execute.

    針對 Ubuntu Server:

    sudo groupadd web-content # Create a group named web-content. sudo usermod -G web-content -a ubuntu # Add the user ubuntu (your default user for this environment) to the group web-content. sudo usermod -G web-content -a www-data # Add the user www-data (Apache HTTP Server) to the group web-content. sudo chown -R ubuntu:web-content /var/www/html # Change the owner of /var/www/html and its files to user ubuntu and group web-content. sudo find /var/www/html -type f -exec chmod u=rw,g=rx,o=rx {} \; # Change all file permissions within /var/www/html to user read/write, group read-only, and others read/execute. sudo find /var/www/html -type d -exec chmod u=rwx,g=rx,o=rx {} \; # Change /var/www/html directory permissions to user read/write/execute, group read/execute, and others read/execute.
  2. 執行以下命令,在 Apache HTTP Server 的預設網站根資料夾 (即 /var/www/html) 中建立名稱為 index.php 的 PHP 型網頁。

    針對 Amazon Linux:

    sudo touch /var/www/html/index.php && sudo chown -R ec2-user:web-content /var/www/html/index.php && sudo chmod u=rw,g=rx,o=rx /var/www/html/index.php && sudo printf '%s\n%s\n%s' '<?php' ' phpinfo();' '?>' >> /var/www/html/index.php

    上述的 Amazon Linux 命令也會將檔案的擁有者變更為 ec2-user、將檔案的群組變更為 web-content,以及為使用者將檔案的許可變更為讀取/寫入,和為群組及其他對象變更為讀取/執行。

    針對 Ubuntu Server:

    sudo touch /var/www/html/index.php && sudo chown -R ubuntu:web-content /var/www/html/index.php && sudo chmod u=rw,g=rx,o=rx /var/www/html/index.php && sudo printf '%s\n%s\n%s' '<?php' ' phpinfo();' '?>' >> /var/www/html/index.php

    Ubuntu Server 的上述命令也會將檔案的擁有者變更為 ubuntu、將檔案的群組變更為 web-content,以及若為使用者,將檔案的許可變更為讀取/寫入,若為群組及其他,則變更為讀取/執行。

    如果成功,上述命令會以下列內容建立 index.php 檔案。

    <?php phpinfo(); ?>
  3. 啟用透過連接埠 80 傳入的 Web 流量以檢視新網頁,方法是在 Amazon VPC 中設定網路 ACL,以及在與此 EC2 環境相關聯的 Amazon EC2 設定安全群組 。若要這樣做,請依照下列順序執行以下八個命令,一次一個。若要了解每個命令的作用,請閱讀每個命令的 # 字元後面的資訊。

    重要

    執行以下命令後,所有 EC2 環境以及與此環境的安全群組和網路 ACL 相關聯的 Amazon EC2 執行個體都能接受透過連接埠 80 傳入的 Web 流量。這可能意外導致 EC2 環境以及此執行個體以外的 Amazon EC2 執行個體能夠接受透過連接埠 80 傳入的 Web 流量。

    注意

    以下第二個到第四個命令可讓安全群組允許透過連接埠 80 傳入的 web 流量。如果您有一個預設安全群組,僅允許透過連接埠 22 傳入的 SSH 流量,則必須先執行第一個命令,接著執行第二個到第四個命令。不過,如果您有一個自訂安全群組,已允許透過連接埠 80 傳入的 Web 流量,則可以略過執行那些命令。

    以下第五個到第八個命令可讓網路 ACL 允許透過連接埠 80 傳入的 web 流量。如果您有一個預設網路 ACL,已允許透過所有連接埠傳入的流量,則可以安全地略過執行那些命令。不過,假設您有自訂網路 ACL,不允許透過連接埠 80 傳入的 Web 流量。然後,執行第一個命令,接著執行第五到第八個命令。

    MY_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) # Get the ID of the instance for the environment, and store it temporarily. MY_SECURITY_GROUP_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SecurityGroups[0].GroupId' --output text) # Get the ID of the security group associated with the instance, and store it temporarily. aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --cidr 0.0.0.0/0 --port 80 # Add an inbound rule to the security group to allow all incoming IPv4-based traffic over port 80. aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --ip-permissions IpProtocol=tcp,Ipv6Ranges='[{CidrIpv6=::/0}]',FromPort=80,ToPort=80 # Add an inbound rule to the security group to allow all incoming IPv6-based traffic over port 80. MY_SUBNET_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SubnetId' --output text) # Get the ID of the subnet associated with the instance, and store it temporarily. MY_NETWORK_ACL_ID=$(aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=$MY_SUBNET_ID --query 'NetworkAcls[].Associations[0].NetworkAclId' --output text) # Get the ID of the network ACL associated with the subnet, and store it temporarily. aws ec2 create-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --protocol tcp --rule-action allow --rule-number 10000 --cidr-block 0.0.0.0/0 --port-range From=80,To=80 # Add an inbound rule to the network ACL to allow all IPv4-based traffic over port 80. Advanced users: change this suggested rule number as desired. aws ec2 create-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --protocol tcp --rule-action allow --rule-number 10100 --ipv6-cidr-block ::/0 --port-range From=80,To=80 # Add an inbound rule to the network ACL to allow all IPv6-based traffic over port 80. Advanced users: change this suggested rule number as desired.
  4. 取得 Web 伺服器根目錄內 index.php 檔案的 URL。若要這樣做,請執行以下命令,並使用新的 Web 瀏覽器標籤或 AWS Cloud9 IDE 以外的其他 Web 瀏覽器,前往顯示的 URL。如果成功,網頁會顯示 Apache HTTP Server、MySQL、PHP 和其他相關設定的相關資訊。

    MY_PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4) && echo http://$MY_PUBLIC_IP/index.php # Get the URL to the index.php file within the web server root.

步驟 4:清理

假設您想要繼續使用此環境,但想要停用透過連接埠 80 傳入的 Web 流量。然後,依照下列順序逐一執行以下八個命令,以刪除您先前在安全群組和網路 ACL 中設定,並與環境相關聯的相應傳入流量規則。若要了解每個命令的作用,請閱讀每個命令的 # 字元後面的資訊。

重要

執行以下命令後,所有 EC2 環境以及與此環境的安全群組和網路 ACL 相關聯的 Amazon EC2 執行個體都會拒絕透過連接埠 80 傳入的 Web 流量。這可能意外導致 EC2 環境以及此執行個體以外的 Amazon EC2 執行個體拒絕透過連接埠 80 傳入的 Web 流量。

注意

以下第五個到第八個命令會移除現有規則,以封鎖網路 ACL 不允許透過連接埠 80 傳入的 web 流量。如果您有一個預設網路 ACL,已允許透過所有連接埠傳入的流量,則可以略過執行那些命令。不過,假設您有一個自訂網路 ACL,其現有規則允許透過連接埠 80 傳入的 Web 流量,而且您想要刪除那些規則。然後,您需要執行第一個命令,接著執行第五到第八個命令。

MY_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) # Get the ID of the instance for the environment, and store it temporarily. MY_SECURITY_GROUP_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SecurityGroups[0].GroupId' --output text) # Get the ID of the security group associated with the instance, and store it temporarily. aws ec2 revoke-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --cidr 0.0.0.0/0 --port 80 # Delete the existing inbound rule from the security group to block all incoming IPv4-based traffic over port 80. aws ec2 revoke-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --ip-permissions IpProtocol=tcp,Ipv6Ranges='[{CidrIpv6=::/0}]',FromPort=80,ToPort=80 # Delete the existing inbound rule from the security group to block all incoming IPv6-based traffic over port 80. MY_SUBNET_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SubnetId' --output text) # Get the ID of the subnet associated with the instance, and store it temporarily. MY_NETWORK_ACL_ID=$(aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=$MY_SUBNET_ID --query 'NetworkAcls[].Associations[0].NetworkAclId' --output text) # Get the ID of the network ACL associated with the subnet, and store it temporarily. aws ec2 delete-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --rule-number 10000 # Delete the existing inbound rule from the network ACL to block all IPv4-based traffic over port 80. Advanced users: if you originally created this rule with a different number, change this suggested rule number to match. aws ec2 delete-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --rule-number 10100 # Delete the existing inbound rule from the network ACL to block all IPv6-based traffic over port 80. Advanced users: if you originally created this rule with a different number, change this suggested rule number to match.

如果您已完成使用此環境,請將環境刪除,以免您的 AWS 帳戶持續產生費用。如需說明,請參閱 刪除 AWS Cloud9 中的環境