适用于 AWS Cloud9 的 LAMP 教程 - AWS Cloud9

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

适用于 AWS Cloud9 的 LAMP 教程

此教程可让您在 AWS Cloud9 开发环境内设置和运行 LAMP(Linux、Apache HTTP Server、MySQL 和 PHP)。

按照本教程操作并创建此示例可能会对您的 AWS 账户 收费。这些包括可能对 Amazon Elastic Compute Cloud(Amazon EC2)等 AWS 服务 收取费用。有关更多信息,请参阅 Amazon EC2 定价

先决条件

在使用此示例之前,请确保您的设置满足以下要求:

  • 您必须有现成的 AWS Cloud9 EC2 开发环境。本示例假设您已经有连接到运行 Amazon Linux 或 Ubuntu Server 的 Amazon EC2 实例的 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 服务器:

    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 服务器:

    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 服务器:

    1. 是否要设置“验证密码”插件 – 输入 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 服务器:

    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 服务器:

    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 和与此 EC2 环境关联的安全组 Amazon EC2 中设置网络 ACL。要执行此操作,按照以下顺序运行下面的八个命令,一次运行一个命令。要了解每个命令的作用,请阅读每个命令的 # 字符后的信息。

    重要

    运行以下命令将为所有 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 中删除环境