

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

# 安装代 CodeDeploy 理
<a name="codedeploy-agent-operations-install"></a>

要 CodeDeploy 在 EC2 实例或本地服务器上使用，必须先安装 CodeDeploy 代理。我们建议使用安装和更新 CodeDeploy 代理 AWS Systems Manager。有关 Systems Manager 的详细信息，请参阅[什么是 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html)。创建部署组时，可以在控制台中使用 Systems Manager 设置 CodeDeploy代理的安装和预设更新。

您也可以使用命令行直接从 S3 存储桶安装 CodeDeploy 代理。

有关要安装的推荐版本，请参阅[CodeDeploy 代理的版本历史记录](codedeploy-agent.md#codedeploy-agent-version-history)。

**Topics**
+ [使用安装 CodeDeploy 代理 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)
+ [使用命令行安装 CodeDeploy 代理](codedeploy-agent-operations-install-cli.md)

# 使用安装 CodeDeploy 代理 AWS Systems Manager
<a name="codedeploy-agent-operations-install-ssm"></a>

您可以使用 AWS 管理控制台 或将 CodeDeploy 代理安装 AWS CLI 到您的 Amazon EC2 或本地实例 AWS Systems Manager。您可以选择安装特定版本或选择始终安装最新版本的代理。有关的更多信息 AWS Systems Manager，请参阅[什么是 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html)。

 安装和更新 CodeDeploy 代理的推荐方法 AWS Systems Manager 是使用。您也可以从 Amazon S3 存储桶安装 CodeDeploy 代理。有关使用 Amazon S3 下载链接的信息，请参阅 [使用命令行安装 CodeDeploy 代理](codedeploy-agent-operations-install-cli.md)。

**Topics**
+ [先决条件](#install-codedeploy-agent-prereqs)
+ [安装代 CodeDeploy 理](#download-codedeploy-agent-on-EC2-Instance)

## 先决条件
<a name="install-codedeploy-agent-prereqs"></a>

按照 [入门 CodeDeploy](getting-started-codedeploy.md) 中的步骤设置 IAM 权限和 AWS CLI。

如果使用 System CodeDeploy s Manager 在本地服务器上安装代理，则必须向 Amazon EC2 Systems Manager 注册本地服务器。有关更多信息，请参阅《AWS Systems Manager 用户指南》**中的[在混合环境中设置 Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-managedinstances.html)。

## 安装代 CodeDeploy 理
<a name="download-codedeploy-agent-on-EC2-Instance"></a>

在使用 Systems Manager 安装 CodeDeploy 代理之前，必须确保已为系统管理器正确配置实例。

### 安装或更新 SSM Agent
<a name="update-SSM-Agent-EC2instance"></a>

在 Amazon EC2 实例上， CodeDeploy 代理要求该实例运行版本 2.3.274.0 或更高版本。在安装 CodeDeploy 代理之前，请先在实例上更新或安装 SSM 代理（如果您尚未这样做）。

SSM 代理已预安装在由 AMIs 提供的某些 Amazon EC2 上。 AWS有关更多信息，请参阅[预装了 SSM 代理的 Amazon 系统映像 (AMIs)](https://docs.aws.amazon.com/systems-manager/latest/userguide/ami-preinstalled-agent.html)。

**注意**  
确保 CodeDeploy代理也支持实例的操作系统。有关更多信息，请参阅 [CodeDeploy 代理支持的操作系统](codedeploy-agent.md#codedeploy-agent-supported-operating-systems)。

有关在运行 Linux 的实例上安装或更新 SSM 代理的信息，请参阅《AWS Systems Manager 用户指南》**中的[在 Linux 实例上安装和配置 SSM 代理](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-agent.html)。

有关在运行 Windows 服务器的实例上安装或更新 SSM 代理的信息，请参阅《AWS Systems Manager 用户指南》**中的[在 Windows 实例上安装和配置 SSM 代理](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-install-ssm-win.html)。

### （可选）验证 Systems Manager 的先决条件
<a name="install-codedeploy-agent-minimum-requirements"></a>

在使用 Systems Manager 运行命令安装 CodeDeploy 代理之前，请验证您的实例是否满足 Systems Manager 的最低要求。有关更多信息，请参阅《AWS Systems Manager 用户指南》**中的[设置 AWS Systems Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-setting-up.html)。

### 安装代 CodeDeploy 理
<a name="install-codedeploy-agent-EC2"></a>

使用 SSM，您可以 CodeDeploy 一次安装或设置安装新版本的时间表。

 要安装 CodeDeploy 代理，请在按照 dist [AWS Systems Manager ributor 安装或更新软件包中的步骤选择软件包](https://docs.aws.amazon.com/systems-manager/latest/userguide/distributor-working-with-packages-deploy.html)。`AWSCodeDeployAgent`

# 使用命令行安装 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-cli"></a>

**注意**  
我们建议使用安装 CodeDeploy 代理 AWS Systems Manager ，以便能够配置代理的预设更新。有关更多信息，请参阅 [使用安装 CodeDeploy 代理 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)。

使用以下主题通过命令行安装和运行 CodeDeploy 代理。

**Topics**
+ [安装适用于亚马逊 Linux 或 RHEL 的 CodeDeploy 代理](codedeploy-agent-operations-install-linux.md)
+ [为 Ubuntu 服务器安装 CodeDeploy 代理](codedeploy-agent-operations-install-ubuntu.md)
+ [安装适用于 Windows 服务器的 CodeDeploy 代理](codedeploy-agent-operations-install-windows.md)

# 安装适用于亚马逊 Linux 或 RHEL 的 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-linux"></a>

登录到实例，并依次运行以下命令。首先运行命令 `sudo yum update` 被认为是使用 `yum` 安装软件包的最佳做法，但如果您不想更新所有软件包，也可以跳过它。

```
sudo yum update
```

```
sudo yum install ruby
```

```
sudo yum install wget
```

（可选）要清理 AMI 中以前的代理缓存信息，请运行以下脚本：

```
#!/bin/bash
CODEDEPLOY_BIN="/opt/codedeploy-agent/bin/codedeploy-agent"
$CODEDEPLOY_BIN stop
yum erase codedeploy-agent -y
```

转到您的主目录：

```
cd /home/ec2-user
```

**注意**  
在前面的命令中，`/home/ec2-user` 表示 Amazon Linux 或 RHEL Amazon EC2 实例的默认用户名。如果您的实例是使用某个自定义 AMI 创建的，该 AMI 所有者可能已指定不同的默认用户名。

下载 CodeDeploy 代理安装程序：

```
wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install
```

*bucket-name*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称，*region-identifier*也是您所在地区的标识符。

例如：

`https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install`

有关存储桶名称和区域标识符的列表，请参阅[各区域的资源工具包存储桶名称](resource-kit.md#resource-kit-bucket-names)。

为 `install` 文件设置执行权限：

```
chmod +x ./install
```

要安装最新版本的 CodeDeploy 代理，请执行以下操作：
+ 

  ```
  sudo ./install auto
  ```

要安装特定版本的 CodeDeploy 代理，请执行以下操作：
+ 列出您所在区域的可用版本：

  ```
  aws s3 ls s3://aws-codedeploy-region-identifier/releases/ --region region-identifier | grep '\.rpm$'
  ```
+ 安装以下版本之一：

  ```
  sudo ./install auto -v releases/codedeploy-agent-version.noarch.rpm
  ```
**注意**  
AWS 支持 CodeDeploy 代理的最新次要版本。目前，最新的次要版本是 1.7.x。

要检查服务是否正在运行，请运行以下命令：

```
systemctl status codedeploy-agent
```

如果 CodeDeploy 代理已安装并正在运行，您应该会看到类似的消息`The AWS CodeDeploy agent is running`。

如果您看到类似于 `error: No AWS CodeDeploy agent running` 的消息，请启动该服务并依次运行以下两个命令：

```
systemctl start codedeploy-agent
```

```
systemctl status codedeploy-agent
```

# 为 Ubuntu 服务器安装 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-ubuntu"></a>

**注意**  
我们建议使用安装 CodeDeploy 代理 AWS Systems Manager ，以便能够配置代理的预设更新。有关更多信息，请参阅 [使用安装 CodeDeploy 代理 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)。

**在 Ubuntu CodeDeploy 服务器上安装代理**

1. 登录到实例。

1. 依次输入以下命令：

   ```
   sudo apt update
   ```

   ```
   sudo apt install ruby-full
   ```

   ```
   sudo apt install wget
   ```

1. 输入以下命令：

   ```
   cd /home/ubuntu
   ```

   */home/ubuntu*表示 Ubuntu 服务器实例的默认用户名。如果您的实例是使用某个自定义 AMI 创建的，该 AMI 所有者可能已指定不同的默认用户名。

1. 输入以下命令：

   ```
   wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install
   ```

   *bucket-name*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称，*region-identifier*也是您所在地区的标识符。

   例如：

   `https://aws-codedeploy-us-east-2.s3.us-east-2.amazonaws.com/latest/install`

   有关存储桶名称和区域标识符的列表，请参阅[各区域的资源工具包存储桶名称](resource-kit.md#resource-kit-bucket-names)。

1. 输入以下命令：

   ```
   chmod +x ./install
   ```

1. 请执行以下操作之一：
   + 要在 Ubuntu 服务器支持的任何版本（20.04 *除外*）上安装最新版本的 CodeDeploy 代理，请执行以下操作：

     ```
     sudo ./install auto
     ```
   + 要在 Ubuntu Server 20.04 上安装最新版本的 CodeDeploy 代理，请执行以下操作：
**注意**  
将输出写入临时日志文件是一种变通方法，当我们在 Ubuntu Server 20.04 上解决 `install` 脚本的一个已知错误时，应该使用这种方法。

     ```
     sudo ./install auto > /tmp/logfile
     ```
   + 要在任何支持的 Ubuntu 服务器版本（20.04 *除外*）上安装特定版本的 CodeDeploy 代理，请执行以下操作：
     + 列出您所在区域的可用版本：

       ```
       aws s3 ls s3://aws-codedeploy-region-identifier/releases/ --region region-identifier | grep '\.deb$'
       ```
     + 安装以下版本之一：

       ```
       sudo ./install auto -v releases/codedeploy-agent-###.deb
       ```
**注意**  
AWS 支持 CodeDeploy 代理的最新次要版本。目前，最新的次要版本是 1.7.x。
   + 要在 Ubuntu Server 20.04 上安装特定版本的 CodeDeploy 代理，请执行以下操作：
     + 列出您所在区域的可用版本：

       ```
       aws s3 ls s3://aws-codedeploy-region-identifier/releases/ --region region-identifier | grep '\.deb$'
       ```
     + 安装以下版本之一：

       ```
       sudo ./install auto -v releases/codedeploy-agent-###.deb > /tmp/logfile
       ```
**注意**  
将输出写入临时日志文件是一种变通方法，当我们在 Ubuntu Server 20.04 上解决 `install` 脚本的一个已知错误时，应该使用这种方法。
**注意**  
AWS 支持 CodeDeploy 代理的最新次要版本。目前，最新的次要版本是 1.7.x。

**检查服务是否正在运行**

1. 输入以下命令：

   ```
   systemctl status codedeploy-agent
   ```

   如果 CodeDeploy 代理已安装并正在运行，您应该会看到类似的消息`The AWS CodeDeploy agent is running`。

1. 如果您看到类似于 `error: No AWS CodeDeploy agent running` 的消息，请启动该服务并依次运行以下两个命令：

   ```
   systemctl start codedeploy-agent
   ```

   ```
   systemctl status codedeploy-agent
   ```

# 安装适用于 Windows 服务器的 CodeDeploy 代理
<a name="codedeploy-agent-operations-install-windows"></a>

在 Windows 服务器实例上，您可以使用以下方法之一来下载和安装 CodeDeploy 代理：
+ 使用 AWS Systems Manager （推荐）
+ 运行一系列 Windows PowerShell 命令。
+ 选择直接下载链接。
+ 运行 Amazon S3 复制命令。

**注意**  
安装 CodeDeploy 代理的文件夹是`C:\Program Data\Amazon\CodeDeploy`。确保此路径上没有目录连接或符号链接。

**Topics**
+ [使用 Systems Manager](#codedeploy-agent-operations-install-system-manager)
+ [使用 Windows PowerShell](#codedeploy-agent-operations-install-windows-powershell)
+ [使用直接链接](#codedeploy-agent-operations-install-windows-direct-link)
+ [使用 Amazon S3 复制命令](#codedeploy-agent-operations-install-windows-s3-copy)

## 使用 Systems Manager
<a name="codedeploy-agent-operations-install-system-manager"></a>

按照中的[使用安装 CodeDeploy 代理 AWS Systems Manager](codedeploy-agent-operations-install-ssm.md)说明安装代 CodeDeploy理。

## 使用 Windows PowerShell
<a name="codedeploy-agent-operations-install-windows-powershell"></a>

登录到该实例，然后在 Windows 中运行以下命令 PowerShell：

1.  要求从 Internet 下载的所有脚本和配置文件由可信发布者签名。如果系统提示您更改执行策略，请键入“**Y**”。

   ```
    Set-ExecutionPolicy RemoteSigned
   ```

1.  加载 AWS Tools for Windows PowerShell.

   ```
   Import-Module AWSPowerShell
   ```

1.  创建一个下载 CodeDeploy 代理安装文件的目录。

   ```
   New-Item -Path "c:\temp" -ItemType "directory" -Force
   ```

1. 使用`Set-AWSCredential`和`Initialize-AWSDefaultConfiguration`命令配置 AWS 凭证。有关更多信息，请参阅《* PowerShell 用户指南》AWS 工具*中的[使用 AWS 凭证](https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html)。

1.  下载 CodeDeploy 代理安装文件。
**注意**  
AWS 支持 CodeDeploy 代理的最新次要版本。目前，最新的次要版本是 1.7.x。

   要安装最新版本的 CodeDeploy代理，请执行以下操作：
   + 

     ```
     powershell.exe -Command Read-S3Object -BucketName bucket-name -Key latest/codedeploy-agent.msi -File c:\temp\codedeploy-agent.msi
     ```

   要安装特定版本的 CodeDeploy代理，请执行以下操作：
   + 

     ```
     powershell.exe -Command Read-S3Object -BucketName bucket-name -Key releases/codedeploy-agent-###.msi -File c:\temp\codedeploy-agent.msi
     ```

   *bucket-name*是包含您所在地区的 CodeDeploy 资源包文件的 Amazon S3 存储桶的名称。例如，对于美国东部（俄亥俄州）区域，请*bucket-name*替换为`aws-codedeploy-us-east-2`。有关存储桶名称的列表，请参阅[各区域的资源工具包存储桶名称](resource-kit.md#resource-kit-bucket-names)。

1.  运行 CodeDeploy 代理安装文件。

   ```
   c:\temp\codedeploy-agent.msi /quiet /l c:\temp\host-agent-install-log.txt
   ```

要检查服务是否正在运行，请运行以下命令：

```
powershell.exe -Command Get-Service -Name codedeployagent
```

 如果 CodeDeploy 代理刚刚安装且尚未启动，则在运行**Get-Service**命令后，在 “**状态**” 下，您应该会看到**Start...**：

```
Status     Name                DisplayName
------     ----                -----------
Start...   codedeployagent    CodeDeploy Host Agent Service
```

如果 CodeDeploy 代理已经在运行，则在运行**Get-Service**命令后，在 “**状态**” 下，您应该会看到**Running**：

```
Status     Name                DisplayName
------     ----                -----------
Running    codedeployagent    CodeDeploy Host Agent Service
```

## 使用直接链接
<a name="codedeploy-agent-operations-install-windows-direct-link"></a>

如果 Windows Server 实例上的浏览器安全设置提供了权限（例如，向`https://s3.*.amazonaws.com`），则可以使用您所在地区的直接链接下载 CodeDeploy 代理，然后手动运行安装程序。

链接是：

```
https://s3.region.amazonaws.com/aws-codedeploy-region/latest/codedeploy-agent.msi
```

... 您要部署应用程序的 AWS 区域在哪里。*region*

例如：

```
https://s3.af-south-1.amazonaws.com/aws-codedeploy-af-south-1/latest/codedeploy-agent.msi
```

**重要**  
从与您的 CodeDeploy应用程序相同的地区获取`.msi`文件。选择不同的区域可能会导致运行 `.msi` 文件时 `codedeploy-agent-log` 文件中出现 `inconsistent region` 错误。

## 使用 Amazon S3 复制命令
<a name="codedeploy-agent-operations-install-windows-s3-copy"></a>

如果实例上安装了，则可以使用 Amazon S3 [cp](https://docs.aws.amazon.com/cli/latest/reference/s3/cp.html) 命令下载 CodeDeploy 代理，然后手动运行安装程序。 AWS CLI 有关信息，请参阅[AWS Command Line Interface 在微软 Windows 上安装](https://docs.aws.amazon.com/cli/latest/userguide/awscli-install-windows.html)。

Amazon S3 命令是：

```
aws s3 cp s3://aws-codedeploy-region/latest/codedeploy-agent.msi codedeploy-agent.msi --region region
```

... 您要部署应用程序的 AWS 区域在哪里。*region*

例如：

```
aws s3 cp s3://aws-codedeploy-af-south-1/latest/codedeploy-agent.msi codedeploy-agent.msi --region af-south-1
```