אירוח של WordPress בסביבה הגמישה של App Engine

במדריך הזה נסביר איך לפרוס אתר קטן של WordPress בסביבה הגמישה של App Engine.

מטרות

  • יוצרים מכונה מדור שני של Cloud SQL.
  • מגדירים אתר לדוגמה ב-WordPress.
  • פורסים את אתר WordPress לדוגמה בסביבה הגמישה של App Engine.

עלויות

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

  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 required APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. התקינו את ה-CLI של Google Cloud.

  6. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  7. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    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 required APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  11. התקינו את ה-CLI של Google Cloud.

  12. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  13. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  14. כדי ליצור פרטי כניסה:
    • נכנסים לדף Credentials במסוף Google Cloud .

      פתיחת הדף API Credentials

    • לוחצים על Create credentials ובוחרים באפשרות Service account key.
    • בוחרים באפשרות חשבון שירות > חשבון השירות של App Engine שמוגדר כברירת מחדל.
    • לוחצים על יצירה.
    • שומרים את המפתח שהורד במקום מאובטח.
  15. מתקינים את PHP ואת Composer.
  16. מורידים את Cloud SQL Proxy והופכים אותו לקובץ הפעלה. צריך גם להוסיף את המיקום של קובץ ההפעלה של Cloud SQL Proxy למשתנה הסביבה PATH.
  17. מתקינים לקוח MySQL ומוודאים שהמיקום של קובץ ההפעלה mysql נמצא במשתנה הסביבה PATH.

יצירה והגדרה של מכונה מדור שני ב-Cloud SQL

  1. יצירת מכונה של Cloud SQL מהדור השני:

    gcloud sql instances create tutorial-sql-instance \
        --activation-policy=ALWAYS \
        --tier=db-n1-standard-1 \
        --region=us-central1
    
  2. מגדירים את סיסמת הבסיס למכונה:

    gcloud sql users set-password root --instance tutorial-sql-instance \
        --password [YOUR_SQL_ROOT_PASSWORD] \
        --host %
    

    כאשר [YOUR_SQL_ROOT_PASSWORD] היא סיסמה מאובטחת לבחירתכם.

  3. מורידים ומריצים את Cloud SQL Proxy:

    ./cloud-sql-proxy \
        -dir /tmp/cloudsql \
        -instances=[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance \
        -credential_file=[PATH_TO_YOUR_SERVICE_ACCOUNT_JSON]

    איפה

    • [YOUR_PROJECT_ID] הוא מזהה הפרויקט. Google Cloud

    • [PATH_TO_YOUR_SERVICE_ACCOUNT_JSON] הוא הנתיב לקובץ JSON של חשבון השירות שהורדתם קודם.

    הפלט הבא מציין ששרת ה-proxy מוכן לחיבורים חדשים:

    Listening on 127.0.0.1:3306 for [YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance
    Ready for new connections
    
  4. בחלון טרמינל אחר, יוצרים מסד נתונים חדש ומשתמש:

    mysql -h 127.0.0.1 -u root --password=[YOUR_SQL_ROOT_PASSWORD]
    mysql> create database tutorialdb;
    mysql> create user 'tutorial-user'@'%' identified by '[YOUR_DATABASE_PASSWORD]';
    mysql> grant all on tutorialdb.* to 'tutorial-user'@'%';
    mysql> exit

    where:

    • [YOUR_SQL_ROOT_PASSWORD] היא סיסמת הבסיס למכונה שלכם ב-Cloud SQL.
    • [YOUR_DATABASE_PASSWORD] היא סיסמה מאובטחת לבחירתכם.

הגדרת פרויקט WordPress

  1. משכפלים את המאגר לדוגמה:

    git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
    
  2. עוברים לספרייה שמכילה את הקוד לדוגמה:

    cd php-docs-samples/appengine/flexible/wordpress
    
  3. יחסי תלות של התקנות:

    composer install
    
  4. מריצים את סקריפט העזר:

    php wordpress.php setup -n \
        --dir=./wordpress-project \
        --db_instance=tutorial-sql-instance \
        --db_name=tutorialdb \
        --db_user=tutorial-user \
        --project_id=[YOUR_PROJECT_ID] \
        --db_password=[YOUR_DATABASE_PASSWORD]

    where:

    • [YOUR_PROJECT_ID] הוא מזהה הפרויקט.
    • [YOUR_DATABASE_PASSWORD] היא הסיסמה למסד הנתונים.

    הפרמטר -dir מציין את המיקום של פרויקט WordPress.

  5. הסקריפט המסייע כותב מידע ל-wordpress-project/wordpress/wp-config.php. בודקים את התוכן של wp-config.php כדי לוודא שהשמות, מזהה הפרויקט והסיסמה של מסד הנתונים נכונים.

    if ($onGae) {
        /** Production environment */
        define('DB_HOST', ':/cloudsql/[YOUR_PROJECT_ID]:us-central1:tutorial-sql-instance');
        /** The name of the database for WordPress */
        define('DB_NAME', 'tutorialdb');
        /** MySQL database username */
        define('DB_USER', 'tutorial-user');
        /** MySQL database password */
        define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
    } else {
        /** Local environment */
        define('DB_HOST', '127.0.0.1');
        /** The name of the database for WordPress */
        define('DB_NAME', 'tutorialdb');
        /** MySQL database username */
        define('DB_USER', 'tutorial-user');
        /** MySQL database password */
        define('DB_PASSWORD', '[YOUR_DATABASE_PASSWORD]');
    }

פריסת פרויקט WordPress בסביבה הגמישה של App Engine

  1. עוברים לספריית הפרויקט של WordPress:

    cd wordpress-project
    
  2. פורסים את פרויקט WordPress:

    gcloud app deploy \
        --promote --stop-previous-version app.yaml cron.yaml
    
  3. בדפדפן, מזינים את כתובת ה-URL הבאה:

    https://PROJECT_ID.REGION_ID.r.appspot.com

    מחליפים את מה שכתוב בשדות הבאים:

עדכון של WordPress, פלאגינים ועיצובים

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

  1. עדכון של WordPress עצמה:

    vendor/bin/wp core update --path=wordpress
    
  2. עדכון פלאגינים:

    vendor/bin/wp plugin update --all --path=wordpress
    # Just in case it updates any of the dropins, copy the files:
    cp wordpress/wp-content/plugins/batcache/advanced-cache.php \
        wordpress/wp-content/plugins/memcached/object-cache.php \
        wordpress/wp-content
    
  3. עדכון העיצובים:

    vendor/bin/wp theme update --all --path=wordpress
    
  4. פורסים את הפרויקט שוב:

    gcloud app deploy \
        --promote --stop-previous-version app.yaml cron.yaml
    

הסרת המשאבים

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

מחיקת הפרויקט

הדרך הקלה ביותר לבטל את החיוב היא למחוק את הפרויקט שיצרתם בשביל המדריך הזה.

כדי למחוק את הפרויקט:

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

    כניסה לדף Manage resources

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

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

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

כדי למחוק גרסת אפליקציה:

  1. במסוף Google Cloud , נכנסים לדף Versions של App Engine.

    כניסה לדף Versions

  2. מסמנים את התיבה שלצד גרסת האפליקציה שלא מוגדרת כברירת מחדל ושרוצים למחוק.
  3. כדי למחוק את גרסת האפליקציה, לוחצים על Delete.

מחיקת המכונה של Cloud SQL

כדי למחוק מכונה של Cloud SQL:

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

    כניסה לדף Instances

  2. לוחצים על השם של מכונת ה-SQL שרוצים למחוק.
  3. כדי למחוק את המכונה, לוחצים על Delete ופועלים לפי ההוראות.

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