

AWS App Runner 从 2026 年 4 月 30 日起，将不再向新客户开放。如果您想使用 App Runner，请在该日期之前注册。现有客户可以继续正常使用该服务。有关更多信息，请参阅 [AWS App Runner 可用性变更](https://docs.aws.amazon.com/apprunner/latest/dg/apprunner-availability-change.html)。

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

# 基于源代码的 App Runner 服务
<a name="service-source-code"></a>

您可以使用基于两种根本不同的服务源 AWS App Runner 来创建和管理服务：*源代码*和*源图像*。无论源类型如何，App Runner 都会负责启动、运行、扩展和负载平衡您的服务。您可以使用 App Runner 的 CI/CD 功能来跟踪源图像或代码的更改。当 App Runner 发现更改时，它会自动构建（对于源代码）并将新版本部署到您的 App Runner 服务。

本章讨论基于源代码的服务。有关基于源图像的服务的信息，请参阅[基于源图像的 App Runner 服务](service-source-image.md)。

源代码是 App Runner 为您构建和部署的应用程序代码。将 App Runner 指向代码存储库中的[源目录](#service-source-code.source-directory)，然后选择与编程平台版本相对应的合适*运行时*。App Runner 根据运行时的基本映像和您的应用程序代码构建映像。然后，它会启动一个基于此镜像运行容器的服务。

 App Runner 提供了便捷的特定于平台的*托管*运行时。这些运行时中的每一个都根据你的源代码构建一个容器镜像，并在你的镜像中添加语言运行时依赖关系。您无需提供容器配置和构建说明，例如 Dockerfile。

本章的副主题讨论 App Runner 支持的各种平台，即为不同的编程环境和版本提供托管运行时的托管*平台*。

**Topics**
+ [源代码存储库提供者](#service-source-code.providers)
+ [源目录](#service-source-code.source-directory)
+ [应用程序运行器托管平台](#service-source-code.managed-platforms)
+ [终止对托管运行时版本的支持](#service-source-code.managed-platforms.eos)
+ [托管运行时版本和 App Runner 版本](#service-source-code.build-detail)
+ [使用 Python 平台](service-source-code-python.md)
+ [使用 Node.js 平台](service-source-code-nodejs.md)
+ [使用 Java 平台](service-source-code-java.md)
+ [使用 .NET 平台](service-source-code-net6.md)
+ [使用 PHP 平台](service-source-code-php.md)
+ [使用 Ruby 平台](service-source-code-ruby.md)
+ [使用 Go 平台](service-source-code-go1.md)

## 源代码存储库提供者
<a name="service-source-code.providers"></a>

App Runner 通过从源代码存储库中读取源代码来部署您的源代码。App Runner 支持两个源代码存储库提供程序：[GitHub](https://github.com/)和 [Bitbucket](https://bitbucket.org/)。

### 从您的源代码存储库提供商进行部署
<a name="service-source-code.providers.github"></a>

要将源代码从源代码存储库部署到 App Runner 服务，App Runner 需要与该服务建立连接。使用 App Runner 控制[台创建服务](manage-create.md)时，您需要提供连接详细信息和源目录，供 App Runner 部署源代码。

**Connections**  
作为服务创建过程的一部分，您需要提供连接详细信息。当你使用 App Runner API 或时 AWS CLI，连接是一种单独的资源。首先，使用 [CreateConnection](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateConnection.html)API 操作创建连接。然后，在创建服务期间使用 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作提供连接的 ARN。

**源目录**  
创建服务时，还会提供源目录。默认情况下，App Runner 使用存储库的根目录作为源目录。源目录是源代码存储库中存储应用程序源代码和配置文件的位置。生成和启动命令也从源目录执行。当您使用 App Runner API 或创建或更新服务时，您需要在[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)和 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作中提供源目录。 AWS CLI 有关更多信息，请参阅下面的[源目录](#service-source-code.source-directory)部分。

有关创建 App Runner 服务的更多信息，请参阅[创建 App Runner 服务](manage-create.md)。有关 App Runner 连接的更多信息，请参阅[管理 App Runner 连接](manage-connections.md)。

## 源目录
<a name="service-source-code.source-directory"></a>

创建 App Runner 服务时，您可以提供源目录以及存储库和分支。将 “**源目录**” 字段的值设置为存储应用程序源代码和配置文件的存储库目录路径。App Runner 从您提供的源目录路径执行生成和启动命令。

将源目录路径的值输入为从根存储库目录开始的绝对路径。如果未指定值，则默认为存储库顶级目录，也称为存储库根目录。

除了顶级存储库目录之外，您还可以选择提供不同的源目录路径。这支持 monorepo 存储库架构，这意味着多个应用程序的源代码存储在一个存储库中。要通过单个 monorepo 创建和支持多个 App Runner 服务，请在创建每个服务时指定不同的源目录。

**注意**  
如果您为多个 App Runner 服务指定相同的源目录，则这两个服务将分别部署和运行。

如果您选择使用`apprunner.yaml`配置文件来定义服务参数，请将其放在存储库的源目录文件夹中。

如果 “部**署” 触发器**选项设置为 “**自**动”，则您在源目录中提交的更改将触发自动部署。 *只有源目录路径中的更改*才会触发自动部署。重要的是要了解源目录的位置如何影响自动部署的范围。有关更多信息，请参阅中的*自动部署*[部署方法](manage-deploy.md#manage-deploy.methods)。

**注意**  
如果您的 App Runner 服务使用 PHP 托管的运行时，并且您想要指定默认根存储库以外的源目录，那么使用正确的 PHP 运行时版本非常重要。有关更多信息，请参阅 [使用 PHP 平台](service-source-code-php.md)。

## 应用程序运行器托管平台
<a name="service-source-code.managed-platforms"></a>

App Runner 托管平台为各种编程环境提供托管运行时。每个托管运行时都可以根据编程语言或运行时环境的版本轻松构建和运行容器。使用托管运行时时，App Runner 从托管运行时映像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含语言运行时包以及一些工具和常用的依赖包。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

## 终止对托管运行时版本的支持
<a name="service-source-code.managed-platforms.eos"></a>

当托管语言运行时的官方提供商或社区正式宣布某个版本为生命周期结束 (EOL) 时，App Runner 随后会将版本状态声明为 En *d of Support*。如果您的服务在已达到 End of Support 的托管语言运行时版本上运行，则以下政策和建议适用。

**终止对语言运行时版本的 Support 支持：**
+ **现有服务**将继续运行并提供流量，即使它们使用的运行时间已达到 End of Support。但是，它们将在不受支持的运行时上运行，不再接收更新、安全补丁或技术支持。
+ 仍然允许@@ **对使用 End of Support 运行时的现有服务进行更新**，但我们不建议继续为服务使用 End of Support 运行时。
+ 无法使用已到 “终止 Support” 日期的运行时创建@@ **新服务**。

**处于 End of Support 状态的语言运行时版本需要执行的操作：**
+ 如果您的服务**基于源图像**，则无需对该服务采取进一步的操作。
+ 如果您的服务**基于源代码**，请更新您的服务配置以使用支持的运行时版本。为此，请在 A [pp Runner 控制台](https://console.aws.amazon.com/apprunner)中选择支持的运行时版本，更新 [apprunner.yaml](config-file.md) 配置文件中的`runtime`字段，或者使用 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)/[UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作或 IaC 工具来设置参数。`runtime`有关支持的运行时列表，请参阅本章中任何特定运行时的*发布信息*页面。
+ 或者，你可以切换到 App Runner 的**容器镜像源**选项。有关更多详细信息，请参阅[基于图像的服务](service-source-image.md)。

**注意**  
如果你要从 Node.js 12、14 或 16 迁移到 **Node.js 22**，或者从 Python 3.7 或 3.8 迁移到 Py **thon 3.11**，请注意 Node.js 22 和 Python 3.11 使用的是修订后的 App Runner 构建流程，可以提供更快、更高效的构建。为了在升级之前确保兼容性，我们建议您在下一节中查看[构建过程指南](#service-source-code.build-detail)。

下表列出了具有指定终止支持日期的 App Runner 托管运行时版本。


| **运行时版本** | **App Runner Support 终止日期** | 
| --- | --- | 
|  Python 3.8 [支持的运行时](service-source-code-python-releases.md)  |  2025 年 12 月 1 日  | 
|  Python 3.7 [支持的运行时](service-source-code-python-releases.md)  |  2025 年 12 月 1 日  | 
|  Node.js 18 [支持的运行时](service-source-code-nodejs-releases.md)  |  2025 年 12 月 1 日  | 
|  Node.js 16 [支持的运行时](service-source-code-nodejs-releases.md)  |  2025 年 12 月 1 日  | 
|  Node.js 14 [支持的运行时](service-source-code-nodejs-releases.md)  |  2025 年 12 月 1 日  | 
|  Node.js 12 [支持的运行时](service-source-code-nodejs-releases.md)  |  2025 年 12 月 1 日  | 
|  .NET 6 \$1  |  2025 年 12 月 1 日  | 
|  PHP 8.1 \$1  |  2025 年 12 月 31 日  | 
|  Ruby 3.1 \$1  |  2025 年 12 月 1 日  | 
|  Go 1 \$1  | 2025 年 12 月 1 日 | 

**\$1** App Runner 不会为标有星号 (\$1) 的运行时发布任何新的语言版本。这些运行时如下：.NET、PHP、Ruby 和 Go。如果您为这些运行时配置了基于代码的服务，我们建议您执行以下操作之一：
+ 如果适用，请将您的服务配置切换到其他支持的托管运行时。
+ 或者，使用您的首选运行时版本构建自定义容器镜像，然后使用 App Runner 的[基于图像的服务](service-source-image.md)选项进行部署。您可以在 Amazon ECR 中托管您的图片。

## 托管运行时版本和 App Runner 版本
<a name="service-source-code.build-detail"></a>

App Runner 为在最新主版本运行时上运行的应用程序提供了更新的构建流程。修改后的构建过程更快、更高效。它还会创建占用空间较小的最终映像，其中仅包含运行应用程序所需的源代码、构建工件和运行时。

我们将较新的构建过程称为*修订后的 App Runner* 版本，将原始构建过程称为最初的 *App Runner 版本*。为了避免对早期版本的运行时平台进行重大更改，App Runner 仅将修订后的版本应用于特定的运行时版本，通常是新发布的主要版本。

我们在`apprunner.yaml`配置文件中引入了一个新组件，以使修订后的版本向后兼容，适用于非常具体的用例，还可以更灵活地配置应用程序的构建。这是可选[`pre-run`](config-file-ref.md#config-file-ref.run)参数。我们将在以下各节中说明何时使用此参数以及有关构建的其他有用信息。

下表说明了 App Runner 版本的哪个版本适用于特定的托管运行时版本。我们将继续更新此文档，让您随时了解我们当前的运行时间。

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code.html)

**注意**  
列出的某些运行时包括**终止支持（End of Support**）日期。有关更多信息，请参阅 [终止对托管运行时版本的支持](#service-source-code.managed-platforms.eos)。

**重要**  
**Python 3.11** — 我们对使用 Python 3.11 托管运行时的服务的构建配置提出了具体建议。有关更多信息，请参阅 *Python 平台*主题[特定运行时版本的标注](service-source-code-python.md#service-source-code-python.callouts)中的。

### 有关 App Runner 版本和迁移的更多信息
<a name="service-source-code.build-detail.builds-and-migr"></a>

当您将应用程序迁移到使用修订版本的较新运行时时，可能需要稍微修改构建配置。

为了提供迁移注意事项的背景信息，我们将首先描述原始 App Runner 版本和修订版的高级流程。接下来我们将有一节介绍您的服务的特定属性，这些属性可能需要一些配置更新。

#### 最初的 App Runner 版本
<a name="service-source-code.build-detail.v1"></a>

最初的 App Runner 应用程序构建过程利用了该 AWS CodeBuild 服务。初始步骤基于该 CodeBuild 服务精心策划的图像。接下来是 Docker 构建流程，该过程使用适用的 App Runner 托管运行时映像作为基础映像。

一般步骤如下：

1. 在 CodeBuild精心策划的图像中运行`pre-build`命令。

   这些`pre-build`命令是可选的。它们只能在`apprunner.yaml`配置文件中指定。

1. 使用在 CodeBuild 上一步中的同一张图像上运行`build`命令。

   这些`build`命令是必需的。它们可以在 App Runner 控制台、App Runner API 或`apprunner.yaml`配置文件中指定。

1. 运行 Docker 版本，根据您的特定平台和运行时版本的 App Runner 托管运行时映像生成映像。

1. 从我们在**步骤 2** 中生成的图像中复制该`/app`目录。目标是基于我们在**步骤 3** 中生成的 App Runner 托管运行时映像的图像。

1. 在生成的 App Runner 托管运行时映像上再次运行`build`命令。我们再次运行构建命令，从**步骤 4** 中复制到的`/app`目录中的源代码生成构建工件。稍后，App Runner 将部署此映像，以便在容器中运行您的 Web 服务。

   这些`build`命令是必需的。它们可以在 App Runner 控制台、App Runner API 或`apprunner.yaml`配置文件中指定。

1. 运行**步骤 2** 中 CodeBuild 镜像中的`post-build`命令。

   这些`post-build`命令是可选的。它们只能在`apprunner.yaml`配置文件中指定。

构建完成后，App Runner 会部署**步骤 5** 中生成的 App Runner 托管运行时映像，以便在容器中运行您的 Web 服务。

#### 修订后的 App Runner 版本
<a name="service-source-code.build-detail.v2"></a>

修订后的构建过程比上一节中描述的原始构建过程更快、更高效。它消除了先前版本版本中出现的生成命令的重复。它还会创建占用空间较小的最终映像，其中仅包含运行应用程序所需的源代码、构建工件和运行时。

此构建过程使用 Docker 多阶段构建。一般的处理步骤如下：

1. **构建阶段** — 启动 docker 构建过程，该过程在 App Runner 构建映像之上执行`pre-build`和`build`命令。

   1. 将应用程序源代码复制到`/app`目录中。
**注意**  
此`/app`目录被指定为 Docker 构建的每个阶段的工作目录。

   1. 运行 `pre-build` 命令。

      这些`pre-build`命令是可选的。它们只能在`apprunner.yaml`配置文件中指定。

   1. 运行`build`命令。

      这些`build`命令是必需的。它们可以在 App Runner 控制台、App Runner API 或`apprunner.yaml`配置文件中指定。

1. **打包阶段** — 生成最终的客户容器镜像，该镜像也基于 App Runner 运行映像。

   1. 将前一个**生成阶段**的`/app`目录复制到新的运行映像。这包括您的应用程序源代码和前一阶段的构建工件。

   1. 运行`pre-run`命令。如果您需要使用`build`命令修改`/app`目录之外的运行时映像，请将相同或必需的命令添加到`apprunner.yaml`配置文件的这一段。

      这是为支持修订后的 App Runner 版本而引入的新参数。

      这些`pre-run`命令是可选的。它们只能在`apprunner.yaml`配置文件中指定。
**注意**  
只有修订后的版本支持这些`pre-run`命令。如果您的服务使用的是使用原始版本的运行时版本，请不要将它们添加到配置文件中。
如果您不需要使用`build`命令修改`/app`目录之外的任何内容，则无需指定`pre-run`命令。

1. **生成后阶段** — 此阶段从 “*构建” 阶段*恢复并运行`post-build`命令。

   1. 在`/app`目录中运行`post-build`命令。

      这些`post-build`命令是可选的。它们只能在`apprunner.yaml`配置文件中指定。

构建完成后，App Runner 会部署运行映像，以便在容器中运行您的 Web 服务。

**注意**  
配置生成过程`apprunner.yaml`时，不要被误导到的 “运行” 部分中的`env`条目。尽管**步骤 2 (b)** 中引用的`pre-run`命令参数位于 “运行” 部分，但不要使用 “运行” 部分中的`env`参数来配置构建。这些`pre-run`命令仅引用配置文件的 Build 部分中定义的`env`变量。有关更多信息，请参阅 *App Runner 配置文件一章[跑步部分](config-file-ref.md#config-file-ref.run)*中的。

#### 考虑迁移时需要考虑的服务要求
<a name="service-source-code.build-detail.migrating"></a>

如果您的应用程序环境具有这两个要求中的任何一个，则需要通过添加`pre-run`命令来修改构建配置。
+ 如果你需要使用`build`命令修改`/app`目录之外的任何内容。
+ 如果您需要运行两次`build`命令来创建所需的环境。这是一个非常不寻常的要求。绝大多数版本都不会这样做。

**在`/app`目录之外进行修改**
+ [修订后的 App Runner 版本](#service-source-code.build-detail.v2)假设您的应用程序在`/app`目录之外没有依赖关系。
+ 您在`apprunner.yaml`文件、App Runner API 或 App Runner 控制台中提供的命令必须在`/app`目录中生成构件。
+ 您可以修改`pre-build``build`、和`post-build`命令以确保所有构建工件都在`/app`目录中。
+ 如果您的应用程序需要在编译版本中进一步修改为服务生成的映像，则可以在`/app`目录之外使用新`pre-run`命令`apprunner.yaml`。有关更多信息，请参阅 [使用配置文件设置 App Runner 服务选项](config-file.md)。

**运行`build`命令两次**
+ [最初的 App Runner 版本](#service-source-code.build-detail.v1)运行`build`命令两次，第一次是在**步骤 2** 中，然后在**步骤 5** 中再次运行。修订后的 App Runner 版本纠正了这种冗余性，并且只运行一次`build`命令。如果您的应用程序不寻常地要求`build`命令运行两次，则修订后的 App Runner 版本提供了使用`pre-run`参数再次指定和执行相同命令的选项。这样做会保留相同的双重构建行为。

# 使用 Python 平台
<a name="service-source-code-python"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **Python 3. **7** 和 Python 3.8** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

P AWS App Runner ython 平台提供托管运行时。每个运行时都可以轻松构建和运行基于 Python 版本的 Web 应用程序的容器。当你使用 Python 运行时时，App Runner 从托管的 Python 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含某个 Python 版本的运行时包以及一些工具和常用的依赖包。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的 Python 运行时名称和版本，请参见[Python 运行时发布信息](service-source-code-python-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

Python 运行时的版本语法：`major[.minor[.patch]]`

例如：`3.8.5`

以下示例演示了版本锁定：
+ `3.8`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `3.8.5`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [Python 运行时配置](#service-source-code-python.config)
+ [特定运行时版本的标注](#service-source-code-python.callouts)
+ [Python 运行时示例](#service-source-code-python.examples)
+ [Python 运行时发布信息](service-source-code-python-releases.md)

## Python 运行时配置
<a name="service-source-code-python.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API**-调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## 特定运行时版本的标注
<a name="service-source-code-python.callouts"></a>

**注意**  
App Runner 现在基于以下运行时版本为应用程序运行更新的构建流程：Python 3.11、Node.js 22 和 Node.js 18。如果您的应用程序在其中一个运行时版本上运行，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)，了解有关修订后的构建过程的更多信息。使用所有其他运行时版本的应用程序不受影响，它们将继续使用原始的构建过程。

### Python 3.11（修订的 App Runner 版本）
<a name="service-source-code-python.callouts.python311"></a>

在 *apprunner.yaml 中为托管 P* ython 3.11 运行时使用以下设置。
+ 将 “顶部” 部分的`runtime`密钥设置为 `python311`   
**Example**  

  ```
  runtime: python311
  ```
+ 使用代`pip3`替`pip`来安装依赖关系。
+ 请改用`python3`解释器`python`。
+ 将`pip3`安装程序作为`pre-run`命令运行。Python 在`/app`目录之外安装依赖项。由于 App Runner 运行适用于 Python 3.11 的修订版 App Runner 版本，因此通过`apprunner.yaml`文件的 “构建” 部分中的命令安装在`/app`目录之外的任何内容都将丢失。有关更多信息，请参阅 [修订后的 App Runner 版本](service-source-code.md#service-source-code.build-detail.v2)。  
**Example**  

  ```
  run:
    runtime-version: 3.11
    pre-run:  
      - pip3 install pipenv
      - pipenv install
      - python3 copy-global-files.py
    command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  ```

有关更多信息，另请参阅本主题后面[的 Python 3.11 扩展配置文件示例](#service-source-code-python.examples.extended-v2)。

## Python 运行时示例
<a name="service-source-code-python.examples"></a>

以下示例显示了用于构建和运行 Python 服务的 App Runner 配置文件。最后一个示例是可以部署到 Python 运行时服务的完整 Python 应用程序的源代码。

**注意**  
这些示例中使用的运行时版本是*3.7.7*和*3.11*。您可以将其替换为要使用的版本。有关支持的最新 Python 运行时版本，请参阅[Python 运行时发布信息](service-source-code-python-releases.md)。

### 最小的 Python 配置文件
<a name="service-source-code-python.examples.minimal"></a>

此示例显示了可以与 Python 托管运行时配合使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

Python 3.11 使用`pip3`和`python3`命令。有关更多信息，请参阅本主题后面[的 Python 3.11 扩展配置文件示例](#service-source-code-python.examples.extended-v2)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: python3 
build:
  commands:
    build:
      - pip install pipenv
      - pipenv install 
run: 
  command: python app.py
```

### 扩展的 Python 配置文件
<a name="service-source-code-python.examples.extended"></a>

此示例展示了在 Python 托管运行时中使用所有配置密钥的情况。

**注意**  
这些示例中使用的运行时版本是*3.7.7*。您可以将其替换为要使用的版本。有关支持的最新 Python 运行时版本，请参阅[Python 运行时发布信息](service-source-code-python-releases.md)。  
Python 3.11 使用`pip3`和`python3`命令。有关更多信息，请参阅本主题后面的 Python 3.11 扩展配置文件示例。

**Example apprunner.yaml**  

```
version: 1.0
runtime: python3 
build:
  commands:
    pre-build:
      - wget -c https://s3.amazonaws.com/amzn-s3-demo-bucket/test-lib.tar.gz -O - | tar -xz
    build:        
      - pip install pipenv
      - pipenv install
    post-build:
      - python manage.py test
  env:
    - name: DJANGO_SETTINGS_MODULE
      value: "django_apprunner.settings"
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 3.7.7
  command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  network: 
    port: 8000
    env: MY_APP_PORT  
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
  secrets:
    - name: my-secret
      value-from: "arn:aws:secretsmanager:us-east-1:123456789012:secret:testingstackAppRunnerConstr-kJFXde2ULKbT-S7t8xR:username::"
    - name: my-parameter
      value-from: "arn:aws:ssm:us-east-1:123456789012:parameter/parameter-name"
    - name: my-parameter-only-name
      value-from: "parameter-name"
```

### 扩展 Python 配置文件 — Python 3.11（使用修改后的版本）
<a name="service-source-code-python.examples.extended-v2"></a>

此示例显示了在 Python 3.11 托管运行时中使用所有配置密钥的情况。`apprunner.yaml`此示例包括一`pre-run`节，因为此版本的 Python 使用修订后的 App Runner 版本。

只有修订后的 App Runner 版本支持该`pre-run`参数。如果您的应用程序使用原始 App Runner 版本支持的运行时版本，请不要在配置文件中插入此参数。有关更多信息，请参阅 [托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

**注意**  
这些示例中使用的运行时版本是*3.11*。您可以将其替换为要使用的版本。有关支持的最新 Python 运行时版本，请参阅[Python 运行时发布信息](service-source-code-python-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: python311
build:
  commands:
    pre-build:
      - wget -c https://s3.amazonaws.com/amzn-s3-demo-bucket/test-lib.tar.gz -O - | tar -xz
    build:        
      - pip3 install pipenv
      - pipenv install
    post-build:
      - python3 manage.py test
  env:
    - name: DJANGO_SETTINGS_MODULE
      value: "django_apprunner.settings"
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 3.11
  pre-run:  
    - pip3 install pipenv
    - pipenv install
    - python3 copy-global-files.py
  command: pipenv run gunicorn django_apprunner.wsgi --log-file -
  network: 
    port: 8000
    env: MY_APP_PORT  
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
  secrets:
    - name: my-secret
      value-from: "arn:aws:secretsmanager:us-east-1:123456789012:secret:testingstackAppRunnerConstr-kJFXde2ULKbT-S7t8xR:username::"
    - name: my-parameter
      value-from: "arn:aws:ssm:us-east-1:123456789012:parameter/parameter-name"
    - name: my-parameter-only-name
      value-from: "parameter-name"
```

### 完整的 Python 应用程序源码
<a name="service-source-code-python.examples.end2end"></a>

此示例显示了可以部署到 Python 运行时服务的完整 Python 应用程序的源代码。

**Example requirements.txt**  

```
pyramid==2.0
```

**Example server.py**  

```
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
import os

def hello_world(request):
    name = os.environ.get('NAME')
    if name == None or len(name) == 0:
        name = "world"
    message = "Hello, " + name + "!\n"
    return Response(message)

if __name__ == '__main__':
    port = int(os.environ.get("PORT"))
    with Configurator() as config:
        config.add_route('hello', '/')
        config.add_view(hello_world, route_name='hello')
        app = config.make_wsgi_app()
    server = make_server('0.0.0.0', port, app)
    server.serve_forever()
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: python3
build:
  commands:
    build:
      - pip install -r requirements.txt
run:
  command: python server.py
```

# Python 运行时发布信息
<a name="service-source-code-python-releases"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **Python 3. **7** 和 Python 3.8** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

本主题列出了 App Runner 支持的 Python 运行时版本的完整详细信息。


**支持的运行时版本 — 修订的 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-python-releases.html)

**注意**  
**Python 3.11** — 我们对使用 Python 3.11 托管运行时的服务的构建配置提出了具体建议。有关更多信息，请参阅 *Python 平台*主题[特定运行时版本的标注](service-source-code-python.md#service-source-code-python.callouts)中的。
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。


**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-python-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

# 使用 Node.js 平台
<a name="service-source-code-nodejs"></a>

**重要**  
App Runner 将于 2025 年 **12 月 1 日终止对 Node.js 12**、**Node.js 14**、**Node.js 16** 和 **Node.js 18** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

 AWS App Runner Node.js 平台提供托管运行时。使用基于 Node.js 版本的 Web 应用程序，每个运行时都可以轻松构建和运行容器。当你使用 Node.js 运行时时，App Runner 从托管的 Node.js 运行时镜像开始。此镜像基于[亚马逊 Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含某个 Node.js 版本的运行时包和一些工具。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的 Node.js 运行时名称和版本，请参阅[Node.js 运行时发布信息](service-source-code-nodejs-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

Node.js 运行时的版本语法：`major[.minor[.patch]]`

例如：`22.14.0`

以下示例演示了版本锁定：
+ `22.14`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `22.14.0`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [Node.js 运行时配置](#service-source-code-nodejs.config)
+ [特定运行时版本的标注](#service-source-code-nodejs.callouts)
+ [Node.js 运行时示例](#service-source-code-nodejs.examples)
+ [Node.js 运行时发布信息](service-source-code-nodejs-releases.md)

## Node.js 运行时配置
<a name="service-source-code-nodejs.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API**-调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

具体而言，对于 Node.js 运行时，您还可以使用源存储库根目录`package.json`中命名的 JSON 文件配置构建和运行时。使用此文件，您可以配置 Node.js 引擎版本、依赖包和各种命令（命令行应用程序）。诸如 npm 或 yarn 之类的 Package 管理器将此文件解释为其命令的输入。

例如：
+ **npm install**安装中`dependencies`和`devDependencies`节点定义的软件包`package.json`。
+ **npm start**或者**npm run start**运行中`scripts/start`节点定义的命令`package.json`。

下面是一个 `package.json` 示例文件。

### package.json
<a name="service-source-code-nodejs.config.package-json-example"></a>

```
{
  "name": "node-js-getting-started",
  "version": "0.3.0",
  "description": "A sample Node.js app using Express 4",
  "engines": {
    "node": "22.14.0"
  },
  "scripts": {
    "start": "node index.js",
    "test": "node test.js"
  },
  "dependencies": {
    "cool-ascii-faces": "^1.3.4",
    "ejs": "^2.5.6",
    "express": "^4.15.2"
  },
  "devDependencies": {
    "got": "^11.3.0",
    "tape": "^4.7.0"
  }
}
```

有关更多信息`package.json`，请参阅 *npm* Doc [s 网站上创建 package.json 文件](https://docs.npmjs.com/creating-a-package-json-file)。

**提示**  
如果您的`package.json`文件定义了**start**命令，则可以将其用作 App Runner 配置文件中的**run**命令，如下例所示。  

**Example**  
package.json  

  ```
  {
    "scripts": {
      "start": "node index.js"
    }
  }
  ```
apprunner.yaml  

  ```
  run:
    command: npm start
  ```
当你在开发环境**npm install**中运行时，npm 会创建该文件`package-lock.json`。此文件包含 npm 刚安装的软件包版本的快照。此后，当 npm 安装依赖项时，它会使用这些确切的版本。如果你安装 yarn，它会创建一个`yarn.lock`文件。将这些文件提交到您的源代码存储库中，以确保您的应用程序与您开发和测试的依赖项版本一起安装。
您也可以使用 App Runner 配置文件来配置 Node.js 版本并启动命令。执行此操作时，这些定义会覆盖中的定义`package.json`。中的`node``package.json`版本与 App Runner 配置文件中的`runtime-version`值发生冲突会导致 App Runner 构建阶段失败。

## 特定运行时版本的标注
<a name="service-source-code-nodejs.callouts"></a>

### Node.js 22 和 Node.js 18（修订的 App Runner 版本）
<a name="service-source-code-nodejs.callouts.nodejs18"></a>

App Runner 现在基于以下运行时版本为应用程序运行更新的构建流程：Python 3.11、Node.js 22 和 Node.js 18。如果您的应用程序在其中一个运行时版本上运行，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)，了解有关修订后的构建过程的更多信息。使用所有其他运行时版本的应用程序不受影响，它们将继续使用原始的构建过程。

## Node.js 运行时示例
<a name="service-source-code-nodejs.examples"></a>

以下示例显示了用于构建和运行 Node.js 服务的 App Runner 配置文件。

**注意**  
这些示例中使用的运行时版本是*22.14.0*。您可以将其替换为要使用的版本。有关支持的最新的 Node.js 运行时版本，请参阅[Node.js 运行时发布信息](service-source-code-nodejs-releases.md)。

### Node.js 的最小配置文件
<a name="service-source-code-nodejs.examples.minimal"></a>

此示例显示了一个可以与 Node.js 托管运行时配合使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: nodejs22
build:
  commands:    
    build:
      - npm install --production                                  
run:                              
  command: node app.js
```

### Node.js 扩展配置文件
<a name="service-source-code-nodejs.examples.extended"></a>

此示例显示了在 Node.js 托管运行时中使用所有配置密钥的情况。

**注意**  
这些示例中使用的运行时版本是*22.14.0*。您可以将其替换为要使用的版本。有关支持的最新的 Node.js 运行时版本，请参阅[Node.js 运行时发布信息](service-source-code-nodejs-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: nodejs22
build:
  commands:
    pre-build:
      - npm install --only=dev
      - node test.js
    build:
      - npm install --production
    post-build:
      - node node_modules/ejs/postinstall.js
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 22.14.0
  command: node app.js
  network:
    port: 8000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### Node.js 扩展配置文件 — Node.js 22（使用修订后的版本）
<a name="service-source-code-nodejs.examples.extended-v2"></a>

此示例显示了如何在 Node.js 托管运行时中使用所有配置密钥`apprunner.yaml`。此示例包括一`pre-run`节，因为此版本的 Node.js 使用的是修订后的 App Runner 版本。

只有修订后的 App Runner 版本支持该`pre-run`参数。如果您的应用程序使用原始 App Runner 版本支持的运行时版本，请不要在配置文件中插入此参数。有关更多信息，请参阅 [托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

**注意**  
这些示例中使用的运行时版本是*22.14.0*。您可以将其替换为要使用的版本。有关支持的最新的 Node.js 运行时版本，请参阅[Node.js 运行时发布信息](service-source-code-nodejs-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: nodejs22
build:
  commands:
    pre-build:
      - npm install --only=dev
      - node test.js
    build:
      - npm install --production
    post-build:
      - node node_modules/ejs/postinstall.js
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 22.14.0
  pre-run: 
    - node copy-global-files.js
  command: node app.js
  network:
    port: 8000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 带有 Grunt 的 Node.js 应用程序
<a name="service-source-code-nodejs.examples.grunt"></a>

此示例说明如何配置使用 Grunt 开发的 Node.js 应用程序。[Grunt](https://gruntjs.com/) 是一个命令行 JavaScript 任务运行器。它运行重复性任务并管理流程自动化以减少人为错误。Grunt 和 Grunt 插件是使用 npm 安装和管理的。您可以通过将`Gruntfile.js`文件包含在源存储库的根目录中来配置 Grunt。

**Example package.json**  

```
{
  "scripts": {
    "build": "grunt uglify",
    "start": "node app.js"
  },
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.5.0"
  },
  "dependencies": {
    "express": "^4.15.2"
  },
}
```

**Example Gruntfile.js**  

```
module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');

  // Default task(s).
  grunt.registerTask('default', ['uglify']);

};
```

**Example apprunner.yaml**  
这些示例中使用的运行时版本是*22.14.0*。您可以将其替换为要使用的版本。有关支持的最新的 Node.js 运行时版本，请参阅[Node.js 运行时发布信息](service-source-code-nodejs-releases.md)。

```
version: 1.0
runtime: nodejs22
build:
  commands:
    pre-build:
      - npm install grunt grunt-cli
      - npm install --only=dev
      - npm run build
    build:
      - npm install --production
run:
  runtime-version: 22.14.0
  command: node app.js
  network:
    port: 8000
    env: APP_PORT
```

# Node.js 运行时发布信息
<a name="service-source-code-nodejs-releases"></a>

**重要**  
App Runner 将于 2025 年 **12 月 1 日终止对 Node.js 12**、**Node.js 14**、**Node.js 16** 和 **Node.js 18** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

**注意**  
App Runner 的标准弃用政策是，当运行时的任何主要组件达到社区长期支持 (LTS) 终止且安全更新不再可用时，就会弃用该运行时。在某些情况下，App Runner 可能会将运行时的弃用延迟一段时间，超过运行时支持的语言版本的 end-of-support日期。这种情况的一个例子可能是延长对运行时的支持，让客户有时间进行迁移。

本主题列出了 App Runner 支持的 Node.js 运行时版本的完整详细信息。


**支持的运行时版本 — 修订的 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-nodejs-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。


**支持的运行时版本 — 修订的 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-nodejs-releases.html)




**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-nodejs-releases.html)

# 使用 Java 平台
<a name="service-source-code-java"></a>

 AWS App Runner Java 平台提供托管运行时。使用基于 Java 版本的 Web 应用程序，每个运行时都可以轻松构建和运行容器。当你使用 Java 运行时时，App Runner 从托管 Java 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含一个 Java 版本的运行时包和一些工具。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

目前，所有支持的 Java 运行时都基于 Amazon Corretto。有关有效的 Java 运行时名称和版本，请参见[Java 运行时版本信息](service-source-code-java-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

Amazon Corretto 运行时的版本语法：


| **运行时** | **语法** | **示例** | 
| --- | --- | --- | 
|  corretto11  |  `11.0[.openjdk-update[.openjdk-build[.corretto-specific-revision]]]`  |  `11.0.13.08.1`  | 
|  corretto8  |  `8[.openjdk-update[.openjdk-build[.corretto-specific-revision]]]`  |  `8.312.07.1`  | 

以下示例演示了版本锁定：
+ `11.0.13`— 锁定 Open JDK 更新版本。App Runner 仅更新 Open JDK 和 Amazon Corretto 较低级别的版本。
+ `11.0.13.08.1`— 锁定到特定版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [Java 运行时配置](#service-source-code-java.config)
+ [Java 运行时示例](#service-source-code-java.examples)
+ [Java 运行时版本信息](service-source-code-java-releases.md)

## Java 运行时配置
<a name="service-source-code-java.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API** — 调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## Java 运行时示例
<a name="service-source-code-java.examples"></a>

以下示例显示了用于构建和运行 Java 服务的 App Runner 配置文件。最后一个示例是完整的 Java 应用程序的源代码，您可以将其部署到 Corretto 11 运行时服务。

**注意**  
这些示例中使用的运行时版本是*11.0.13.08.1*。您可以将其替换为要使用的版本。有关支持的最新 Java 运行时版本，请参阅[Java 运行时版本信息](service-source-code-java-releases.md)。

### 最小 Corretto 11 配置文件
<a name="service-source-code-java.examples.minimal"></a>

此示例显示了可以与 Corretto 11 托管运行时配合使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅。

**Example apprunner.yaml**  

```
version: 1.0
runtime: corretto11
build:
  commands:    
    build:
      - mvn clean package
run:                              
  command: java -Xms256m -jar target/MyApp-1.0-SNAPSHOT.jar .
```

### 扩展的 Corretto 11 配置文件
<a name="service-source-code-java.examples.extended"></a>

此示例说明如何在 Corretto 11 托管运行时中使用所有配置密钥。

**注意**  
这些示例中使用的运行时版本是*11.0.13.08.1*。您可以将其替换为要使用的版本。有关支持的最新 Java 运行时版本，请参阅[Java 运行时版本信息](service-source-code-java-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: corretto11
build:
  commands:
    pre-build:
      - yum install some-package
      - scripts/prebuild.sh
    build:
      - mvn clean package
    post-build:
      - mvn clean test
  env:
    - name: M2
      value: "/usr/local/apache-maven/bin"
    - name: M2_HOME
      value: "/usr/local/apache-maven/bin"
run:
  runtime-version: 11.0.13.08.1
  command: java -Xms256m -jar target/MyApp-1.0-SNAPSHOT.jar .
  network:
    port: 8000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 完整的 Corretto 11 应用程序源代码
<a name="service-source-code-java.examples.end2end"></a>

此示例显示了可以部署到 Corretto 11 运行时服务的完整 Java 应用程序的源代码。

**Example src/main/java/com/HelloWorld/HelloWorld.java**  

```
package com.HelloWorld;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {

    @RequestMapping("/")
    public String index(){
        String s = "Hello World";
        return s;
    }
}
```

**Example src/main/java/com/HelloWorld/Main.java**  

```
package com.HelloWorld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {

        SpringApplication.run(Main.class, args);
    }
}
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: corretto11
build:
  commands:    
    build:
      - mvn clean package
run:                              
  command: java -Xms256m -jar target/HelloWorldJavaApp-1.0-SNAPSHOT.jar .
  network:
    port: 8080
```

**Example pom.xml**  

```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/>
  </parent>
  <groupId>com.HelloWorld</groupId>
  <artifactId>HelloWorldJavaApp</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <java.version>11</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>11</release>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
```

# Java 运行时版本信息
<a name="service-source-code-java-releases"></a>

本主题列出了 App Runner 支持的 Java 运行时版本的完整详细信息。


**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-java-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

# 使用 .NET 平台
<a name="service-source-code-net6"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **.NET 6** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

 AWS App Runner .NET 平台提供托管运行时。每个运行时都可以轻松构建和运行带有基于.NET 版本的 Web 应用程序的容器。当你使用.NET 运行时时，App Runner 从托管的.NET 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含适用于.NET 版本的运行时包以及一些工具和常用的依赖包。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的.NET 运行时名称和版本，请参阅[.NET 运行时版本信息](service-source-code-dotnet-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

.NET 运行时的版本语法：`major[.minor[.patch]]`

例如：`6.0.9`

以下示例演示了版本锁定：
+ `6.0`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `6.0.9`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [.NET 运行时配置](#service-source-code-net6.config)
+ [NET 运行时示例](#service-source-code-net6.examples)
+ [.NET 运行时版本信息](service-source-code-dotnet-releases.md)

## .NET 运行时配置
<a name="service-source-code-net6.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API** — 调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## NET 运行时示例
<a name="service-source-code-net6.examples"></a>

以下示例显示了用于构建和运行.NET 服务的 App Runner 配置文件。最后一个示例是完整的.NET 应用程序的源代码，您可以将其部署到.NET 运行时服务。

**注意**  
这些示例中使用的运行时版本是*6.0.9*。您可以将其替换为要使用的版本。有关支持的最新.NET 运行时版本，请参阅[.NET 运行时版本信息](service-source-code-dotnet-releases.md)。

### 最小的.NET 配置文件
<a name="service-source-code-net6.examples.minimal"></a>

此示例显示了可在.NET 托管运行时中使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: dotnet6
build:
  commands:    
    build:
      - dotnet publish -c Release -o out
run:                              
  command: dotnet out/HelloWorldDotNetApp.dll
```

### 扩展的.NET 配置文件
<a name="service-source-code-net6.examples.extended"></a>

此示例说明如何使用.NET 托管运行时的所有配置密钥。

**注意**  
这些示例中使用的运行时版本是*6.0.9*。您可以将其替换为要使用的版本。有关支持的最新.NET 运行时版本，请参阅[.NET 运行时版本信息](service-source-code-dotnet-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: dotnet6
build:
  commands:
    pre-build:
      - scripts/prebuild.sh
    build:
      - dotnet publish -c Release -o out
    post-build:
      - scripts/postbuild.sh
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"    
run:
  runtime-version: 6.0.9
  command: dotnet out/HelloWorldDotNetApp.dll
  network:
    port: 5000
    env: APP_PORT
  env:
    - name: ASPNETCORE_URLS
      value: "http://*:5000"
```

### 完整的.NET 应用程序源代码
<a name="service-source-code-net6.examples.end2end"></a>

此示例显示了可以部署到.NET 运行时服务的完整.NET 应用程序的源代码。

**注意**  
 运行以下命令创建一个简单的.NET 6 Web 应用程序：` dotnet new web --name HelloWorldDotNetApp -f net6.0`
 将添加到创建的 `apprunner.yaml` .NET 6 Web 应用程序中。

**Example HelloWorldDotNetApp**  

```
version: 1.0
runtime: dotnet6
build:
  commands:
    build:
      - dotnet publish -c Release -o out
run:
  command: dotnet out/HelloWorldDotNetApp.dll
  network:
    port: 5000
    env: APP_PORT
  env:
    - name: ASPNETCORE_URLS
      value: "http://*:5000"
```

# .NET 运行时版本信息
<a name="service-source-code-dotnet-releases"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **.NET 6** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

本主题列出了 App Runner 支持的.NET 运行时版本的完整详细信息。


**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-dotnet-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

# 使用 PHP 平台
<a name="service-source-code-php"></a>

**重要**  
App Runner 将于 2025 年 12 月 31 日终止对 **PHP 8.1** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

P AWS App Runner HP 平台提供托管运行时。您可以使用每个运行时来构建和运行带有基于 PHP 版本的 Web 应用程序的容器。当你使用 PHP 运行时时，App Runner 从托管的 PHP 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含一个 PHP 版本的运行时包和一些工具。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的 PHP 运行时名称和版本，请参见[PHP 运行时版本信息](service-source-code-php-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

PHP 运行时的版本语法：`major[.minor[.patch]]`

例如：`8.1.10`

以下是版本锁定的示例：
+ `8.1`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `8.1.10`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**重要**  
 如果您想将 App Runner 服务的代码存储库[源目录](service-source-code.md#service-source-code.source-directory)指定到默认存储库根目录以外的位置，则您的 PHP 托管运行时版本必须是 PHP `8.1.22` 或更高版本。之前的 PHP 运行时版本`8.1.22`只能使用默认的根源目录。

**Topics**
+ [PHP 运行时配置](#service-source-code-php.config)
+ [兼容性](#service-source-code-php.compatibility)
+ [PHP 运行时示例](#service-source-code-php.examples)
+ [PHP 运行时版本信息](service-source-code-php-releases.md)

## PHP 运行时配置
<a name="service-source-code-php.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API** — 调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## 兼容性
<a name="service-source-code-php.compatibility"></a>

您可以使用以下 Web 服务器之一在 PHP 平台上运行 App Runner 服务：
+ Apache HTTP Server
+ NGINX

Apache HTTP ServerNGINX并且与 PHP-FPM 兼容。您可以使用以下任一方法*NGINX*来启动*Apache HTTP Server*和：
+ S@@ [upervisord](http://supervisord.org/introduction.html#supervisor-components/)-有关运行的更多信息 *supervisord*，请参阅[运行 supervisord。](http://supervisord.org/running.html#running-supervisord)
+ 启动脚本 

有关如何使用 Ap *ache HTTP 服务器或 *NGINX 在 PHP 平台上配置 App Runn* er* 服务的示例，请参阅。[完整的 PHP 应用程序源码](#service-source-code-php.examples.end2end)

### 文件结构
<a name="service-source-code-php.compatibility.file-structure"></a>

`index.php`必须安装在 Web 服务器`root`目录下的`public`文件夹中。

**注意**  
我们建议将`startup.sh`或`supervisord.conf`文件存储在 Web 服务器的根目录中。确保该`start`命令指向存储`startup.sh`或`supervisord.conf`文件的位置。

 如果您正在使用，以下是文件结构的示例*supervisord*。

```
/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ supervisord.conf
```

以下是使用*启动脚本时的*文件结构示例。

```
/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ startup.sh
```

我们建议您将这些文件结构存储在为 App Runner 服务指定的代码存储库[源目录中](service-source-code.md#service-source-code.source-directory)。

```
/<sourceDirectory>/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ startup.sh
```

**重要**  
 如果您想将 App Runner 服务的代码存储库[源目录](service-source-code.md#service-source-code.source-directory)指定到默认存储库根目录以外的位置，则您的 PHP 托管运行时版本必须是 PHP `8.1.22` 或更高版本。之前的 PHP 运行时版本`8.1.22`只能使用默认的根源目录。  
每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。默认情况下，您的服务将使用最新的运行时，除非您在 [App Runner 配置文件](config-file.md)中使用`runtime-version`关键字指定版本锁定。

## PHP 运行时示例
<a name="service-source-code-php.examples"></a>

以下是用于构建和运行 PHP 服务的 App Runner 配置文件的示例。

### 最小 PHP 配置文件
<a name="service-source-code-php.examples.minimal"></a>

以下示例是可以与 PHP 托管运行时一起使用的最小配置文件。有关最小配置文件的更多信息，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0 
runtime: php81
build:
  commands:
    build:
      - echo example build command for PHP
run:
  command: ./startup.sh
```

### 扩展 PHP 配置文件
<a name="service-source-code-php.examples.extended"></a>

以下示例使用了 PHP 托管运行时的所有配置密钥。

**注意**  
这些示例中使用的运行时版本是*8.1.10*。您可以将其替换为要使用的版本。有关支持的最新的 PHP 运行时版本，请参阅[PHP 运行时版本信息](service-source-code-php-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: php81
build:
  commands:
     pre-build:
      - scripts/prebuild.sh
    build:
      - echo example build command for PHP
    post-build:
      - scripts/postbuild.sh
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 8.1.10
  command: ./startup.sh
  network:
    port: 5000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 完整的 PHP 应用程序源码
<a name="service-source-code-php.examples.end2end"></a>

以下示例是 PHP 应用程序源代码，您可以使用这些源代码通过*Apache HTTP Server*或将其部署到 PHP 运行时服务*NGINX*。这些示例假设您使用默认文件结构。

#### Apache HTTP Server使用运行 PHP 平台 supervisord
<a name="service-source-code-php.examples.end2end.appache-supervisord"></a>

**Example 文件结构**  
+ 该`supervisord.conf`文件可以存储在存储库中的任何地方。确保该`start`命令指向`supervisord.conf`文件的存储位置。
+ `index.php`必须安装在`root`目录下的`public`文件夹中。

```
/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ supervisord.conf
```

**Example superisord.conf**  

```
[supervisord]
nodaemon=true

[program:httpd]
command=httpd -DFOREGROUND
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:php-fpm]
command=php-fpm -F
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: php81
build:
  commands:
    build:
      - PYTHON=python2 amazon-linux-extras install epel
      - yum -y install supervisor
run:
  command: supervisord
  network:
    port: 8080
    env: APP_PORT
```

**Example index.php**  

```
<html>
<head> <title>First PHP App</title> </head>
<body>
<?php
    print("Hello World!");
    print("<br>");
?>
</body>
</html>
```

#### Apache HTTP Server使用运行 PHP 平台 startup script
<a name="service-source-code-php.examples.end2end.appache-startupscript"></a>

**Example 文件结构**  
+ 该`startup.sh`文件可以存储在存储库中的任何地方。确保该`start`命令指向`startup.sh`文件的存储位置。
+ `index.php`必须安装在`root`目录下的`public`文件夹中。

```
/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ startup.sh
```

**Example startup.sh**  

```
#!/bin/bash

set -o monitor

trap exit SIGCHLD

# Start apache
httpd -DFOREGROUND &

# Start php-fpm
php-fpm -F &

wait
```

**注意**  
在将`startup.sh`文件提交到 Git 存储库之前，请务必将其保存为可执行文件。`chmod +x startup.sh`用于为您的`startup.sh`文件设置执行权限。
如果未将`startup.sh`文件另存为可执行文件，请在`apprunner.yaml`文件中以`build`命令`chmod +x startup.sh`形式输入。

**Example apprunner.yaml**  

```
version: 1.0
runtime: php81
build:
  commands:
    build:
      - echo example build command for PHP
run:
  command: ./startup.sh
  network:
    port: 8080
    env: APP_PORT
```

**Example index.php**  

```
<html>
<head> <title>First PHP App</title> </head>
<body>
<?php
    print("Hello World!");
    print("<br>");
?>
</body>
</html>
```

#### NGINX使用运行 PHP 平台 supervisord
<a name="service-source-code-php.examples.end2end.nginx-supervisord"></a>

**Example 文件结构**  
+ 该`supervisord.conf`文件可以存储在存储库中的任何地方。确保该`start`命令指向`supervisord.conf`文件的存储位置。
+ `index.php`必须安装在`root`目录下的`public`文件夹中。

```
/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ supervisord.conf
```

**Example superisord.conf**  

```
[supervisord]
nodaemon=true

[program:nginx]
command=nginx -g "daemon off;"
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0

[program:php-fpm]
command=php-fpm -F
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: php81
build:
  commands:
    build:
      - PYTHON=python2 amazon-linux-extras install epel
      - yum -y install supervisor
run:
  command: supervisord
  network:
    port: 8080
    env: APP_PORT
```

**Example index.php**  

```
<html>
<head> <title>First PHP App</title> </head>
<body>
<?php
    print("Hello World!");
    print("<br>");
?>
</body>
</html>
```

#### NGINX使用运行 PHP 平台 startup script
<a name="service-source-code-php.examples.end2end.nginx-startupscript"></a>

**Example 文件结构**  
+ 该`startup.sh`文件可以存储在存储库中的任何地方。确保该`start`命令指向`startup.sh`文件的存储位置。
+ `index.php`必须安装在`root`目录下的`public`文件夹中。

```
/
├─ public/
│  ├─ index.php
├─ apprunner.yaml
├─ startup.sh
```

**Example startup.sh**  

```
#!/bin/bash

set -o monitor

trap exit SIGCHLD

# Start nginx 
nginx -g 'daemon off;' &

# Start php-fpm
php-fpm -F &

wait
```

**注意**  
在将`startup.sh`文件提交到 Git 存储库之前，请务必将其保存为可执行文件。`chmod +x startup.sh`用于为您的`startup.sh`文件设置执行权限。
如果未将`startup.sh`文件另存为可执行文件，请在`apprunner.yaml`文件中以`build`命令`chmod +x startup.sh`形式输入。

**Example apprunner.yaml**  

```
version: 1.0
runtime: php81
build:
  commands:
    build:
      - echo example build command for PHP
run:
  command: ./startup.sh
  network:
    port: 8080
    env: APP_PORT
```

**Example index.php**  

```
<html>
<head> <title>First PHP App</title> </head>
<body>
<?php
    print("Hello World!");
    print("<br>");
?>
</body>
</html>
```

# PHP 运行时版本信息
<a name="service-source-code-php-releases"></a>

**重要**  
App Runner 将于 2025 年 12 月 31 日终止对 **PHP 8.1** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

本主题列出了 App Runner 支持的 PHP 运行时版本的完整详细信息。


**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-php-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

# 使用 Ruby 平台
<a name="service-source-code-ruby"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **Ruby 3.** 1 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

 AWS App Runner Ruby 平台提供托管运行时。每个运行时都可以轻松地使用基于 Ruby 版本的 Web 应用程序构建和运行容器。当你使用 Ruby 运行时时，App Runner 会从托管的 Ruby 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含某个 Ruby 版本的运行时包和一些工具。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的 Ruby 运行时名称和版本，请参见[Ruby 运行时发布信息](service-source-code-ruby-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

Ruby 运行时的版本语法：`major[.minor[.patch]]`

例如：`3.1.2`

以下示例演示了版本锁定：
+ `3.1`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `3.1.2`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [Ruby 运行时配置](#service-source-code-ruby.config)
+ [Ruby 运行时示例](#service-source-code-ruby.examples)
+ [Ruby 运行时发布信息](service-source-code-ruby-releases.md)

## Ruby 运行时配置
<a name="service-source-code-ruby.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API** — 调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## Ruby 运行时示例
<a name="service-source-code-ruby.examples"></a>

以下示例显示了用于构建和运行 Ruby 服务的 App Runner 配置文件。

### 最小 Ruby 配置文件
<a name="service-source-code-ruby.examples.minimal"></a>

此示例显示了一个可以与 Ruby 托管运行时配合使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: ruby31
build:
  commands:
    build:
      - bundle install
run:
  command: bundle exec rackup --host 0.0.0.0 -p 8080
```

### 扩展的 Ruby 配置文件
<a name="service-source-code-ruby.examples.extended"></a>

此示例显示了在 Ruby 托管运行时中使用所有配置密钥的情况。

**注意**  
这些示例中使用的运行时版本是*3.1.2*。您可以将其替换为要使用的版本。有关支持的最新的 Ruby 运行时版本，请参阅[Ruby 运行时发布信息](service-source-code-ruby-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: ruby31
build:
  commands:
     pre-build:
      - scripts/prebuild.sh
    build:
      - bundle install
    post-build:
      - scripts/postbuild.sh
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 3.1.2
  command: bundle exec rackup --host 0.0.0.0 -p 4567
  network:
    port: 4567
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 完整的 Ruby 应用程序源码
<a name="service-source-code-ruby.examples.end2end"></a>

这些示例显示了可以部署到 Ruby 运行时服务的完整 Ruby 应用程序的源代码。

**Example server.rb**  

```
# server.rb
require 'sinatra'

get '/' do    
  'Hello World!'
end
```

**Example config.ru**  

```
# config.ru

require './server'

run Sinatra::Application
```

**Example Gemfile**  

```
# Gemfile
source 'https://rubygems.org (https://rubygems.org/)'

gem 'sinatra'
gem 'puma'
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: ruby31
build:
  commands:
    build:
      - bundle install
run:
  command: bundle exec rackup --host 0.0.0.0 -p 4567
  network:
    port: 4567
    env: APP_PORT
```

# Ruby 运行时发布信息
<a name="service-source-code-ruby-releases"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **Ruby 3.** 1 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

本主题列出了 App Runner 支持的 Ruby 运行时版本的完整详细信息。


**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-ruby-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。

# 使用 Go 平台
<a name="service-source-code-go1"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **Go 1.18** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

G AWS App Runner o 平台提供托管运行时。每个运行时都可以轻松构建和运行基于 Go 版本的 Web 应用程序的容器。当你使用 Go 运行时时，App Runner 会从托管 Go 运行时镜像开始。此镜像基于 [Amazon Linux Docker 镜像](https://hub.docker.com/_/amazonlinux)，包含某个 Go 版本的运行时包和一些工具。App Runner 使用此托管运行时映像作为基础映像，并添加您的应用程序代码来构建 Docker 映像。然后，它会部署此映像以在容器中运行您的 Web 服务。

 在使用 App Runner 控制台或 [CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)API 操作[创建服务](manage-create.md)时，可以为 App Runner 服务指定运行时。您也可以将运行时指定为源代码的一部分。在包含在代码存储库中的 A [pp Runner 配置文件](config-file.md)中使用`runtime`关键字。托管运行时的命名约定是*<language-name><major-version>*。

有关有效的 Go 运行时名称和版本，请参阅[Go 运行时版本信息](service-source-code-go-releases.md)。

每次部署或服务更新时，App Runner 都会将服务的运行时更新到最新版本。如果您的应用程序需要托管运行时的特定版本，则可以使用 [App Runner 配置文件](config-file.md)中的`runtime-version`关键字进行指定。您可以锁定到任何级别的版本，包括主要版本或次要版本。App Runner 仅对服务的运行时进行较低级别的更新。

Go 运行时的版本语法：`major[.minor[.patch]]`

例如：`1.18.7`

以下示例演示了版本锁定：
+ `1.18`— 锁定主要版本和次要版本。App Runner 仅更新补丁版本。
+ `1.18.7`— 锁定到特定的补丁版本。App Runner 不会更新你的运行时版本。

**Topics**
+ [Go 运行时配置](#service-source-code-go1.config)
+ [Go 运行时示例](#service-source-code-go1.examples)
+ [Go 运行时版本信息](service-source-code-go-releases.md)

## Go 运行时配置
<a name="service-source-code-go1.config"></a>

选择托管运行时时，还必须至少配置生成和运行命令。您可以在[创建](manage-create.md)或[更新](manage-configure.md) App Runner 服务时对其进行配置。您可以使用以下方法之一来执行此操作：
+ **使用 App Runner 控制台**-在创建过程或**配置选项卡的 “配置构**建” 部分中指定命令。
+ **使用 App Runner API**-调用[CreateService](https://docs.aws.amazon.com/apprunner/latest/api/API_CreateService.html)或 [UpdateService](https://docs.aws.amazon.com/apprunner/latest/api/API_UpdateService.html)API 操作。使用[CodeConfigurationValues](https://docs.aws.amazon.com/apprunner/latest/api/API_CodeConfigurationValues.html)数据类型的`BuildCommand`和`StartCommand`成员来指定命令。
+ **使用[配置文件](config-file.md)**-在最多三个构建阶段中指定一个或多个构建命令，并指定一个用于启动应用程序的运行命令。还有其他可选的配置设置。

提供配置文件是可选的。使用控制台或 API 创建 App Runner 服务时，您可以指定 App Runner 是在创建时直接获取配置设置还是从配置文件中获取配置设置。

## Go 运行时示例
<a name="service-source-code-go1.examples"></a>

以下示例显示了用于构建和运行 Go 服务的 App Runner 配置文件。

### 最小 Go 配置文件
<a name="service-source-code-go1.examples.minimal"></a>

此示例显示了可以与 Go 托管运行时一起使用的最小配置文件。有关 App Runner 使用最小配置文件做出的假设，请参阅[配置文件示例](config-file-examples.md#config-file-examples.managed)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: go1
build:
  commands:
    build:
      - go build main.go
run:
  command: ./main
```

### 扩展 Go 配置文件
<a name="service-source-code-go1.examples.extended"></a>

此示例显示了在 Go 托管运行时中使用所有配置密钥的情况。

**注意**  
这些示例中使用的运行时版本是*1.18.7*。您可以将其替换为要使用的版本。有关支持的最新 Go 运行时版本，请参阅[Go 运行时版本信息](service-source-code-go-releases.md)。

**Example apprunner.yaml**  

```
version: 1.0
runtime: go1
build:
  commands:
     pre-build:
      - scripts/prebuild.sh
    build:
      - go build main.go
    post-build:
      - scripts/postbuild.sh
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
run:
  runtime-version: 1.18.7
  command: ./main
  network:
    port: 3000
    env: APP_PORT
  env:
    - name: MY_VAR_EXAMPLE
      value: "example"
```

### 完整的 Go 应用程序源码
<a name="service-source-code-go1.examples.end2end"></a>

这些示例显示了可以部署到 Go 运行时服务的完整 Go 应用程序的源代码。

**Example main.go**  

```
package main
import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "<h1>Welcome to App Runner</h1>")
    })
    fmt.Println("Starting the server on :3000...")
    http.ListenAndServe(":3000", nil)
}
```

**Example apprunner.yaml**  

```
version: 1.0
runtime: go1
build:
  commands:
    build:
      - go build main.go
run:
  command: ./main
  network:
    port: 3000
    env: APP_PORT
```

# Go 运行时版本信息
<a name="service-source-code-go-releases"></a>

**重要**  
App Runner 将于 2025 年 12 月 1 日终止对 **Go 1.18** 的支持。有关建议和更多信息，请参阅[终止对托管运行时版本的支持](service-source-code.md#service-source-code.managed-platforms.eos)。

本主题列出了 App Runner 支持的 Go 运行时版本的完整详细信息。


**支持的运行时版本-原始 App Runner 版本**  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/apprunner/latest/dg/service-source-code-go-releases.html)

**注意**  
App Runner 为最近发布的特定主要运行时提供了修订后的构建流程。因此，您将在本文档的某些部分中看到对*修订后的 App Runner 版本**和原始 App Runner 版本*的引用。有关更多信息，请参阅[托管运行时版本和 App Runner 版本](service-source-code.md#service-source-code.build-detail)。