Compute Engine 上的 PHP 使用入门


本教程介绍如何开始使用 Compute Engine。 请按照本教程向 Compute Engine 部署一个 Hello World PHP Web 应用。如需获取 App Engine 入门方面的帮助,请参阅 App Engine 标准环境

目标

  • 使用 Cloud Shell 下载并部署 Hello World 示例应用。
  • 将 Hello World 示例应用部署到单个 Compute Engine 实例。

费用

在本文档中,您将使用 Google Cloud 的以下收费组件:

您可使用价格计算器根据您的预计使用情况来估算费用。 Google Cloud 新用户可能有资格申请免费试用

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Compute Engine API。

    启用 API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Compute Engine API。

    启用 API

  8. 在 Google Cloud 控制台中,在 Cloud Shell 中打开该应用。

    转到 Cloud Shell

    Cloud Shell 让您可以直接在浏览器中通过命令行访问云端资源。

  9. 如果您同意克隆代码库,请点击确认以下载示例代码并切换到应用目录。

  10. 在 Cloud Shell 中,配置 gcloud CLI 以使用新的 Google Cloud 项目:
    # Configure gcloud for your project
    gcloud config set project YOUR_PROJECT_ID
    

在 Cloud Shell 中运行应用

  1. 使用 composer 安装应用依赖项:

    composer install
    
  2. 运行 PHP 内置 Web 服务器

    php -S localhost:8080
    
  3. 在 Cloud Shell 中,点击 Web 预览,然后选择在端口 8080 上预览。此时浏览器会打开一个新窗口,显示出您运行中的应用。

  4. 要停止本地网络服务器,请按 Control+C

部署到单个实例

此部分逐步介绍了如何在 Compute Engine 上运行应用的单个实例。

单实例部署。

您可以通过 Cloud Shell 将应用部署到单个 Compute Engine 实例虚拟机 (VM),以运行您的应用。

使用启动脚本初始化实例

您需要一种方法来指示实例下载并运行这些代码。 实例可能拥有对其执行启动或重启操作时会运行的启动脚本。

set -e
export HOME=/root

# Install PHP and dependencies from apt
apt-get update
apt-get install -y git nginx php7.2 php7.2-fpm php7.2-mysql php7.2-dev \
    php7.2-mbstring php7.2-zip php-pear pkg-config

# Install Composer
curl -sS https://getcomposer.org/installer | \
    /usr/bin/php -- \
    --install-dir=/usr/local/bin \
    --filename=composer

# Get the application source code
git clone https://github.com/googlecloudplatform/getting-started-php /opt/src
ln -s /opt/src/gce /opt/app

# Run Composer
composer install -d /opt/app --no-ansi --no-progress --no-dev

# Disable the default NGINX configuration
rm /etc/nginx/sites-enabled/default

# Enable our NGINX configuration
cp /opt/app/config/nginx/helloworld.conf /etc/nginx/sites-available/helloworld.conf
ln -s /etc/nginx/sites-available/helloworld.conf /etc/nginx/sites-enabled/helloworld.conf
cp /opt/app/config/nginx/fastcgi_params /etc/nginx/fastcgi_params

# Start NGINX
systemctl restart nginx.service

# Install Fluentd
curl -s "https://storage.googleapis.com/signals-agents/logging/google-fluentd-install.sh" | bash

# Enable our Fluentd configuration
cp /opt/app/config/fluentd/helloworld.conf /etc/google-fluentd/config.d/helloworld.conf

# Start Fluentd
service google-fluentd restart &

启动脚本执行以下任务:

  • 安装 PHP 7 和 PHP 7 FPM。

  • 安装并配置 NGINX。

  • 下载 Composer 并使用其运行应用代码。

  • 安装 Cloud Logging 代理,并将其配置为监控应用日志。这意味着,本教程前面步骤中配置的日志记录的上传方式与使用 App Engine 时一样。

创建和配置 Compute Engine 实例

  1. 创建 Compute Engine 实例:

    MY_INSTANCE_NAME='my-app-instance'
    ZONE=us-central1-f
    
    gcloud compute instances create $MY_INSTANCE_NAME \
        --image-family=ubuntu-1804-lts \
        --image-project=ubuntu-os-cloud \
        --machine-type=g1-small \
        --scopes userinfo-email,cloud-platform \
        --metadata-from-file startup-script=scripts/startup-script.sh \
        --zone $ZONE \
        --tags http-server

    将地区替换为开发地区,例如 us-central1-a。如需详细了解区域和地区,请参阅地理位置和区域

    这会创建一个新实例,允许其访问 Google Cloud 服务并运行您的启动脚本。实例名称为 my-app-instance

  2. 检查实例创建的进度:

    gcloud compute instances get-serial-port-output my-app-instance --zone YOUR_ZONE
    

    YOUR_ZONE 替换为您将实例部署到的地区。

    启动脚本完成后,您会看到以下消息:

    startup-script: INFO Finished running startup scripts.
    
  3. 创建一条防火墙规则以允许流量进入您的实例:

    gcloud compute firewall-rules create default-allow-http-80 \
        --allow tcp:80 \
        --source-ranges 0.0.0.0/0 \
        --target-tags http-server \
        --description "Allow port 80 access to http-server"
    

  4. 获取您的实例的外部 IP 地址:

    gcloud compute instances list
    
  5. 要查看应用的运行情况,请在浏览器中输入以下网址:

    http://YOUR_INSTANCE_IP
    

    YOUR_INSTANCE_IP 替换为实例的外部 IP 地址。

管理和监控实例

您可以使用 Google Cloud 控制台来监控和管理您的实例。

  1. 在 Google Cloud 控制台中,转到虚拟机实例页面。

    转到“虚拟机实例”

  2. 在虚拟机实例列表中,点击要连接的实例所在行中的 SSH
  3. 如需查看 Compute Engine 资源生成的所有日志,请转到日志浏览器页面。转到 Logs Explorer

    Cloud Logging 会自动配置为从各种常用服务(包括 syslog)收集日志。

清理

为避免因本教程中使用的资源导致您的 Google Cloud 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。

删除项目

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

逐个删除资源

gcloud compute instances delete my-app-instance --zone=YOUR_ZONE --delete-disks=all
gcloud compute firewall-rules delete default-allow-http-80

后续步骤