יצירת אפליקציית PHP בסביבה הגמישה ב-App Engine

מזהה אזור

‫REGION_ID הוא קוד מקוצר ש-Google מקצה על סמך האזור שבוחרים כשיוצרים את האפליקציה. הקוד לא תואם למדינה או למחוז, למרות שחלק ממזהי האזורים עשויים להיראות דומים לקודים נפוצים של מדינות ומחוזות. באפליקציות שנוצרו אחרי פברואר 2020, המחרוזת REGION_ID.r כלולה בכתובות URL של App Engine. באפליקציות קיימות שנוצרו לפני התאריך הזה, מזהה האזור הוא אופציונלי בכתובת ה-URL.

מידע נוסף על מזהי אזורים

במדריך למתחילים הזה נסביר איך ליצור ולפרוס אפליקציה שמציגה הודעה קצרה. כדי להשתמש באפליקציה לדוגמה שבמדריך הזה עם כל גרסה נתמכת של PHP, צריך לציין את גרסת זמן הריצה ואת מערכת ההפעלה בקובץ app.yaml.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות להשלמת המדריך הזה, אתם צריכים לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

דרישות מוקדמות נוספות

  1. מאתחלים את אפליקציית App Engine עם הפרויקט ובוחרים את האזור שלה:

    gcloud app create --project=[YOUR_PROJECT_ID]
    

    כשמופיעה בקשה, בוחרים את האזור שבו רוצים למקם את אפליקציית App Engine.

  2. מתקינים את הכלים הבאים במחשב המקומי:

המיקומים של App Engine

‫App Engine הוא אזורי, כלומר התשתית שמריצה את האפליקציות שלכם ממוקמת באזור ספציפי, ו-Google מנהלת אותה כך שהיא זמינה באופן יתירתי בכל התחומים באזור הזה.

הקריטריונים העיקריים לבחירת האזור שבו האפליקציות שלכם יפעלו הם זמן האחזור, הזמינות או העמידות. בדרך כלל אפשר לבחור את האזור הכי קרוב למשתמשים של האפליקציה, אבל כדאי להביא בחשבון את המיקומים שבהם App Engine זמין ואת המיקומים שלGoogle Cloud המוצרים והשירותים האחרים שבהם האפליקציה משתמשת. שימוש בשירותים בכמה מיקומים יכול להשפיע על זמן האחזור של האפליקציה ועל התמחור שלה.

אחרי שמגדירים את האזור של האפליקציה, אי אפשר לשנות אותו.

אם כבר יצרתם אפליקציית App Engine, תוכלו לראות את האזור שלה באחת מהדרכים הבאות:

במדריך למתחילים הזה אנחנו מניחים שאתם מכירים את שפת התכנות PHP.

הורדת האפליקציה Hello World

יצרנו אפליקציית Hello World ל-App Engine כדי שתוכלו להבין במהירות איך לפרוס אפליקציה ב- Google Cloud.

  1. משכפלים את המאגר של אפליקציית Hello World לדוגמה למכונה המקומית.

    git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
    

    אפשרות נוספת היא להוריד את הקובץ לדוגמה כקובץ ZIP ולחלץ אותו.

  2. עוברים לספרייה שמכילה את הקוד לדוגמה.

       cd php-docs-samples/appengine/flexible/helloworld
    

הרצת Hello World במחשב המקומי

כדי להריץ את האפליקציה Hello World במחשב המקומי:

  1. מתקינים את יחסי התלות.

    composer install
    
  2. מפעילים שרת אינטרנט מקומי.

    php -S localhost:8080 -t web/
    
  3. בדפדפן האינטרנט, מזינים את הכתובת הבאה:

    http://localhost:8080

ההודעה Hello World מאפליקציית הדוגמה מוצגת בדף. בחלון הטרמינל, מקישים על Ctrl+C כדי לצאת משרת האינטרנט.

פריסה והפעלה של Hello World ב-App Engine

כדי לפרוס את האפליקציה בסביבה הגמישה של App Engine:

  1. מריצים את הפקודה הבאה מהספרייה appengine/flexible/helloworld כדי לפרוס את אפליקציית Hello World:

    gcloud app deploy

    מידע על הדגלים האופציונליים

    הגדרות flag נפוצות של הפקודה gcloud

    • כדי לציין מזהה שמזהה באופן ייחודי את הגרסה של האפליקציה, צריך לכלול את הדגל --version. אחרת, המערכת תיצור מזהה בשבילכם. לדוגמה: --version [YOUR_VERSION_ID]
    • כדי לציין מזהה פרויקט חלופי ב- Google Cloud , שונה ממה שהוגדר כברירת מחדל בכלי gcloud, צריך לכלול את הדגל --project. לדוגמה: --project [YOUR_PROJECT_ID]

    דוגמה:

    gcloud app deploy --version pre-prod-5 --project my-sample-app

    במאמר בדיקה ופריסה של האפליקציה תוכלו לקרוא מידע נוסף על פריסת האפליקציה משורת הפקודה. רשימה של כל הדגלים של הפקודה מופיעה במאמר בנושא gcloud app deploy.

  2. מפעילים את הדפדפן כדי לראות את האפליקציה בכתובת https://PROJECT_ID.REGION_ID.r.appspot.com

    ‫
    gcloud app browse
    , כאשר PROJECT_ID מייצג את מזהה הפרויקט ב- Google Cloud.

הפעם, הדף שבו מוצגת ההודעה Hello World מועבר על ידי שרת אינטרנט שפועל במכונה של App Engine.

מזל טוב! פרסתם את האפליקציה הראשונה שלכם ב-App Engine בסביבה הגמישה ב-App Engine!

אם נתקלתם בשגיאות כלשהן במהלך פריסת האפליקציה, כדאי לעיין בטיפים לפתרון בעיות.

בקטעים הבאים מפורט מידע על ניקוי, וגם קישורים לפעולות אפשריות שאפשר לבצע.

הסרת המשאבים

כדי להימנע מחיובים, אתם יכולים למחוק את הפרויקט Google Cloud כדי להפסיק את החיוב על כל המשאבים שנעשה בהם שימוש במסגרת הפרויקט.

  1. נכנסים לדף Manage resources במסוף Google Cloud .

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

המאמרים הבאים

לומדים את כל הפלטפורמה

עכשיו, אחרי שאתם יודעים איך לפתח ולפרוס אפליקציות ב-App Engine, אתם יכולים להמשיך לחקור את Google Cloud. כבר מותקן אצלכם Google Cloud CLI, שכולל את הכלים שמאפשרים לכם ליצור אינטראקציה עם מוצרים כמו Cloud SQL, ‏ Cloud Storage,‏ Firestore ועוד.

מידע על סביבה גמישה ב-App Engine

הנה כמה נושאים שיעזרו לכם להמשיך ללמוד על App Engine:

סקר קוד של Hello World

‫Hello World היא האפליקציה הכי פשוטה של App Engine, כי היא מכילה רק שירות אחד, יש לה רק גרסה אחת וכל הקוד נמצא בתיקיית השורש של האפליקציה. בקטע הזה מפורטים כל קובצי האפליקציה.

index.php

הקובץ index.php מכיל את קוד ה-PHP להפעלת שרת ולהגבה לבקשות. בדוגמה הזו משתמשים ב-Slim, אבל אפשר להשתמש בסביבות פיתוח אחרות לאינטרנט ב-PHP.

הקוד הזה יוצר אובייקט Slim Application חדש, ואז מגדיר שני מסלולים שונים של GET:

require_once __DIR__ . '/../vendor/autoload.php';

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Factory\AppFactory;

// Create App
$app = AppFactory::create();

// Display errors
$app->addErrorMiddleware(true, true, true);

$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello World');
    return $response;
});

$app->get('/goodbye', function (Request $request, Response $response) {
    $response->getBody()->write('Goodbye World');
    return $response;
});

// @codeCoverageIgnoreStart
if (PHP_SAPI != 'cli') {
    $app->run();
}
// @codeCoverageIgnoreEnd

return $app;

app.yaml

הקובץ app.yaml מתאר את ההגדרות הבאות של האפליקציה:

  • הערך env: flex מציין שהאפליקציה משתמשת בסביבה גמישה ב-App Engine.
  • הגדרה של סביבת זמן הריצה שבה האפליקציה משתמשת.

    runtime: php
    env: flex
    
    runtime_config:
      document_root: web
      operating_system: ubuntu22
      runtime_version: 8.4
    
    # This sample incurs costs to run on the App Engine flexible environment.
    # The settings below are to reduce costs during testing and are not appropriate
    # for production use. For more information, see:
    # https://cloud.google.com/appengine/docs/flexible/php/configuring-your-app-with-app-yaml
    manual_scaling:
      instances: 1
    resources:
      cpu: 1
      memory_gb: 0.5
      disk_size_gb: 10
    

    composer.json

    composer.json משמש להצהרה, להתקנה ולעדכון של יחסי תלות של אפליקציות:

    {
      "require": {
        "slim/slim": "^4.0",
        "slim/psr7": "^1.3"
      }
    }