

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

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

# 使用 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)。