gcloud config set project myProject
Run WordPress on App Engine standard environment
Author(s): @bshaffer , Published: 2019-01-31
Google Cloud Community tutorials submitted from the community do not represent official Google Cloud product documentation.
WordPress is an open source web framework for PHP developers that encourages the use of the model-view-controller (MVC) pattern.
This tutorial illustrates how to use a simple command-line tool for downloading and configuring WordPress on App Engine standard environment for PHP 7.2.
You can check out PHP on Google Cloud to get an overview of PHP and learn ways to run PHP apps on Google Cloud.
Prerequisites
Create a project in the Cloud Console.
Enable billing for your project.
Install the Cloud SDK.
Enable the Cloud SQL API.
Install Composer.
Note: Enable Cloud SQL API - Go to API and services then dashboard
Enable APIS and Services
Search for Cloud SQL
If not enabled then enable
From the shell
Create a new Cloud SQL for MySQL Second Generation instance with the following command:
$ gcloud sql instances create wordpress3 \
--activation-policy=ALWAYS \
--tier=db-n1-standard-1 \
--region=us-central1
john@cloudshell:~ (graphite-ally-282801)$ gcloud sql instances create wordpress2 \> --activation-policy=ALWAYS \> --tier=db-n1-standard-1 \> --region=us-central1Creating Cloud SQL instance...done.Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/graphite-ally-282801/instances/wordpress2].NAME DATABASE_VERSION LOCATION TIER PRIMARY_ADDRESS PRIVATE_ADDRESS STATUSwordpress2 MYSQL_5_7 us-central1-a db-n1-standard-1 34.69.150.154 - RUNNABLE
Note: you can choose db-f1-micro or db-g1-small instead of db-n1-standard-1 for the Cloud SQL machine type, especially for development or testing purposes. However, those machine types are not recommended for production use and are not eligible for Cloud SQL SLA coverage. See the Cloud SQL SLA for more details.
Create the database you want your WordPress site to use:
$ gcloud sql databases create wordpress3 --instance wordpress3
john@cloudshell:~ (graphite-ally-282801)$ gcloud sql databases create wordpress2 --instance wordpress2 Creating Cloud SQL database...done.Created database [wordpress2].instance: wordpress2name: wordpress2project: graphite-ally-282801john@cloudshell:~ (graphite-ally-282801)$
Change the root password for your instance:
$ gcloud sql users set-password root \
--host=% \
--instance wordpress3 \
--password=uconnstamford
john@cloudshell:~ (graphite-ally-282801)$ gcloud sql users set-password root \> --host=% \> --instance wordpress \> --password=uconnstamfordUpdating Cloud SQL user...done.john@cloudshell:~ (graphite-ally-282801)$
Create or update a WordPress project for App Engine
The wp-gae command provides a convenient way for you to either create a new WordPress project or add the required configuration to an existing one.
Setup
Download the google/cloud-tools package:
$ composer require google/cloud-tools
john@cloudshell:~ (graphite-ally-282801)$ composer require google/cloud-toolsUsing version ^0.11.0 for google/cloud-tools./composer.json has been createdLoading composer repositories with package informationUpdating dependencies (including require-dev)Package operations: 22 installs, 0 updates, 0 removals - Installing symfony/polyfill-ctype (v1.18.1): Downloading (100%) - Installing symfony/polyfill-mbstring (v1.18.1): Downloading (100%) - Installing twig/twig (v2.13.1): Downloading (100%) - Installing symfony/polyfill-php80 (v1.18.1): Downloading (100%) - Installing symfony/process (v5.1.5): Downloading (100%) - Installing symfony/filesystem (v5.1.5): Downloading (100%) - Installing symfony/polyfill-intl-normalizer (v1.18.1): Downloading (100%) - Installing symfony/polyfill-intl-grapheme (v1.18.1): Downloading (100%) - Installing symfony/string (v5.1.5): Downloading (100%) - Installing psr/container (1.0.0): Downloading (100%) - Installing symfony/service-contracts (v2.2.0): Downloading (100%) - Installing symfony/polyfill-php73 (v1.18.1): Downloading (100%) - Installing symfony/console (v5.1.5): Downloading (100%) - Installing symfony/dom-crawler (v5.1.5): Downloading (100%) - Installing symfony/browser-kit (v5.1.5): Downloading (100%) - Installing psr/http-message (1.0.1): Downloading (100%) - Installing psr/http-client (1.0.1): Downloading (100%) - Installing ralouphie/getallheaders (3.0.3): Downloading (100%) - Installing guzzlehttp/psr7 (1.6.1): Downloading (100%) - Installing guzzlehttp/promises (v1.3.1): Downloading (100%) - Installing guzzlehttp/guzzle (7.0.1): Downloading (100%) - Installing google/cloud-tools (v0.11.0): Downloading (100%) symfony/polyfill-intl-normalizer suggests installing ext-intl (For best performance)symfony/polyfill-intl-grapheme suggests installing ext-intl (For best performance)symfony/service-contracts suggests installing symfony/service-implementationsymfony/console suggests installing symfony/event-dispatchersymfony/console suggests installing symfony/locksymfony/console suggests installing psr/log (For using the console logger)symfony/dom-crawler suggests installing symfony/css-selectorguzzlehttp/psr7 suggests installing zendframework/zend-httphandlerrunner (Emit PSR-7 responses)guzzlehttp/guzzle suggests installing ext-curl (Required for CURL handler support)guzzlehttp/guzzle suggests installing ext-intl (Required for Internationalized Domain Name (IDN) support)guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)Writing lock fileGenerating autoload files14 packages you are using are looking for funding.Use the `composer fund` command to find out more!john@cloudshell:~ (graphite-ally-282801)$
Note: If you receive an error about extensions, install phar and zip PHP extensions and retry:
$ sudo apt-get install php7.2-zip php7.2-curl
john@cloudshell:~ (graphite-ally-282801)$ sudo apt-get install php7.2-zip********************************************************************************You are running apt-get inside of Cloud Shell. Note that your Cloud Shellmachine is ephemeral and no system-wide change will persist beyond session end.To suppress this warning, create an empty ~/.cloudshell/no-apt-get-warning file.The command will automatically proceed in 5 seconds or on any key.Visit https://cloud.google.com/shell/help for more information.********************************************************************************Reading package lists... DoneBuilding dependency tree Reading state information... DoneThe following additional packages will be installed: libzip4The following NEW packages will be installed: libzip4 php7.2-zip0 upgraded, 2 newly installed, 0 to remove and 7 not upgraded.Need to get 72.5 kB of archives.After this operation, 233 kB of additional disk space will be used.Do you want to continue? [Y/n] YGet:1 https://packages.sury.org/php buster/main amd64 libzip4 amd64 1.5.1-4+0~20190318173200.9+buster~1.gbp333132 [50.8 kB]Get:2 https://packages.sury.org/php buster/main amd64 php7.2-zip amd64 7.2.33-1+0~20200807.47+debian10~1.
gbpcb3068 [21.6 kB]Fetched 72.5 kB in 0s (487 kB/s) debconf: delaying package configuration, since apt-utils is not installedSelecting previously unselected package libzip4:amd64.(Reading database ... 129614 files and directories currently installed.)Preparing to unpack .../libzip4_1.5.1-4+0~20190318173200.9+buster~1.gbp333132_amd64.deb ...Unpacking libzip4:amd64 (1.5.1-4+0~20190318173200.9+buster~1.gbp333132) ...Selecting previously unselected package php7.2-zip.Preparing to unpack .../php7.2-zip_7.2.33-1+0~20200807.47+debian10~1.gbpcb3068_amd64.deb ...Unpacking php7.2-zip (7.2.33-1+0~20200807.47+debian10~1.gbpcb3068) ...Setting up libzip4:amd64 (1.5.1-4+0~20190318173200.9+buster~1.gbp333132) ...Setting up php7.2-zip (7.2.33-1+0~20200807.47+debian10~1.gbpcb3068) ...debconf: unable to initialize frontend: Dialogdebconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76.)debconf: falling back to frontend: ReadlineCreating config file /etc/php/7.2/mods-available/zip.ini with new versionProcessing triggers for libc-bin (2.28-10) ...Processing triggers for php7.2-cli (7.2.33-1+0~20200807.47+debian10~1.gbpcb3068) ...Processing triggers for php7.2-cgi (7.2.33-1+0~20200807.47+debian10~1.gbpcb3068) ...john@cloudshell:~ (graphite-ally-282801)$
2. Now you can run the wp-gae command which is included in that package:
$ php vendor/bin/wp-gae
john@cloudshell:~ (graphite-ally-282801)$ php vendor/bin/wp-gaeWordpress HelperUsage: command [options] [arguments]Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debugAvailable commands: create Create a new WordPress site for Google Cloud help Displays help for a command list Lists commands update Update an existing WordPress site for Google Cloudjohn@cloudshell:~ (graphite-ally-282801)$
Note: You can also install google/cloud-tools globally, which will allow you to execute the command wp-gae anywhere.
The wp-gae command will ask you several question in order to set up your Cloud SQL database connection, and then write the required configuration to your wp-config.php configuration file. It also copies the following files into your project directory to allow WordPress to run on App Engine:
app.yaml: The App Engine configuration file that specifies the runtime and static asset handlers.
cron.yaml: The App Engine configuration file that ensures wp-cron.php is run every 15 minutes.
php.ini: For setting PHP configuration in App Engine specific to WordPress.
gae-app.php: The Front Controller, which is required for all App Engine applications.
Create a new WordPress project
To download WordPress and set it up for GCP, run the create command:
sudo apt-get install php7.2-zip
Note: to get project ID click 3 circles on top left hand side of screen
Then cut and paste the ID from right side of screen
Create a directory for your wordpress files
$ mkdir wordpress-uconn
$ php vendor/bin/wp-gae create
Note: if you get an error then $ sudo apt-get install php7.2-zip
The command asks you several questions. After you answer them, you'll have a new WordPress project. By default, it will create my-wordpress-project in the current directory.
Please enter a directory path for the new project (defaults to my-wordpress-project):wordpress-uconnWe will use the directory: wordpress-uconn. If the directory exists, we will overwrite the contents. Do you want to continue? (Y/n)YDownloading the WordPress archive...Re-using a directory /home/john/wordpress-uconn.Downloaded the WordPress archive.Re-using a directory /home/john/wordpress-uconn.Downloading the GCS plugin...Downloaded GCS plugin.Please select the region of your Cloud SQL instance (defaults to us-central1) [0 ] northamerica-northeast1 [1 ] us-central [2 ] us-central1 [3 ] us-east1 [4 ] us-east4 [5 ] us-west1 [6 ] us-west2 [7 ] southamerica-east1 [8 ] europe-north1 [9 ] europe-west1 [10] europe-west2 [11] europe-west3 [12] europe-west4 [13] asia-east1 [14] asia-northeast1 [15] asia-south1 [16] asia-southeast1 [17] australia-southeast1 >
> 2Using db_region us-central1Please enter project_id (required): graphite-ally-282801Please enter db_instance (required): wordpress2Please enter db_name (required): wordpress2Please enter db_user (defaults to 'root'):Please enter db_password (required):
Do you want to use the same db user and password for local run? (Y/n)Y
Deploy to Google Cloud
cd into the root of your WordPress project:
$ cd wordpress-uconn
john@cloudshell:~ (graphite-ally-282801)$ pwd/home/johnjohn@cloudshell:~ (graphite-ally-282801)$ cd wordpress-uconnjohn@cloudshell:~/wordpress-uconn (graphite-ally-282801)$
Run the following command to deploy your project to App Engine:
Note: app engine must be enabled
$ gcloud app deploy app.yaml cron.yaml
You are creating an app for project [graphite-ally-282801].WARNING: Creating an App Engine application for a project is irreversible and the regioncannot be changed. More information about regions is at<https://cloud.google.com/appengine/docs/locations>.Please choose the region where you want your App Engine applicationlocated: [1] asia-east2 (supports standard and flexible) [2] asia-northeast1 (supports standard and flexible) [3] asia-northeast2 (supports standard and flexible) [4] asia-northeast3 (supports standard and flexible) [5] asia-south1 (supports standard and flexible) [6] asia-southeast2 (supports standard and flexible) [7] australia-southeast1 (supports standard and flexible) [8] europe-west (supports standard and flexible) [9] europe-west2 (supports standard and flexible) [10] europe-west3 (supports standard and flexible) [11] europe-west6 (supports standard and flexible) [12] northamerica-northeast1 (supports standard and flexible) [13] southamerica-east1 (supports standard and flexible) [14] us-central (supports standard and flexible) [15] us-east1 (supports standard and flexible) [16] us-east4 (supports standard and flexible) [17] us-west2 (supports standard and flexible) [18] us-west3 (supports standard and flexible) [19] us-west4 (supports standard and flexible) [20] cancelPlease enter your numeric choice: 14
Please enter your numeric choice: 14Creating App Engine application in project [graphite-ally-282801] and region [us-central]....done.Services to deploy:descriptor: [/home/john/wordpress-uconn/app.yaml]source: [/home/john/wordpress-uconn]target project: [graphite-ally-282801]target service: [default]target version: [20200913t003907]target url: [http://graphite-ally-282801.uc.r.appspot.com]Configurations to update:descriptor: [/home/john/wordpress-uconn/cron.yaml]type: [cron jobs]target project: [graphite-ally-282801]Do you want to continue (Y/n)? YBeginning deployment of service [default]...Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
I got an error
Beginning deployment of service [default]...Created .gcloudignore file. See `gcloud topic gcloudignore` for details.╔════════════════════════════════════════════════════════════╗╠═ Uploading 2533 files to Google Cloud Storage ═╣╚════════════════════════════════════════════════════════════╝File upload done.Updating service [default]...failed.ERROR: (gcloud.app.deploy) Error Response: [9] Cloud build dce3270a-9a97-465a-961b-85d299bb50d2 status: FAILUREBuild error details: Access to bucket staging.graphite-ally-282801.appspot.com denied. You must grant Storage Object Viewer permission to 252985529066@cloudbuild.gserviceaccount.com.Full build logs: https://console.cloud.google.com/cloud-build/builds/dce3270a-9a97-465a-961b-85d299bb50d2?project=252985529066
So I added the permissions to the storage bucket
Now you can access your site, and continue the installation step! The URL is https://YOUR_PROJECT_ID.appspot.com/.
NOTE: If you receive any error in your application, such as "Error estabilishing a database connection", set the WP_DEBUG constant to true in wp-config.php and redeploy:
define('WP_DEBUG', true);
Enable the Google Cloud Storage plugin
To use the Google Cloud Storage plugin for media uploads, follow these steps:
Configure the App Engine default Cloud Storage bucket for later use. The default App Engine bucket is named YOUR_PROJECT_ID.appspot.com. Change the default Access Control List (ACL) of that bucket as follows:
$ gsutil defacl ch -u AllUsers:R gs://YOUR_PROJECT_ID.appspot.com
Go to the Dashboard at https://YOUR_PROJECT_ID.appspot.com/wp-admin. On the Plugins page, activate the Google Cloud Storage plugin.
In the plugins Settings page, set your Bucket name to the bucket you configured in Step 1.
After activating the plugin, try uploading a media object in a new post and confirm the image is uploaded to the Cloud Storage bucket by visiting the Cloud Console Storage page.
Local development
To access this MySQL instance, use Cloud SQL Proxy. Download it to your local computer and make it executable.
To install in Cloud Shell:
$ wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
$ chmod +x cloud_sql_proxy
Go to the the Credentials section of your project in the Console. Click Create credentials and then click Service account key. For the Service account, select App Engine app default service account. Then click Create to create and download the JSON service account key to your local machine. Save it to a safe place.
Run the proxy by the following command:
$ ./cloud_sql_proxy \
-instances=YOUR_PROJECT_ID:us-central1:wordpress=tcp:3306 \
-credential_file=/path/to/YOUR_SERVICE_ACCOUNT_JSON_FILE.json &
If running within Cloud Shell:
$ ./cloud_sql_proxy -instances <YOUR_PROJECT_ID>:us-central1:wordpress=tcp:3306 &
Note: See Connecting to Cloud SQL from External Applications for more options when running the Cloud SQL proxy.
Now you can access the Cloud SQL instance with the MySQL client in a separate command-line tab.
$ mysql --host=127.0.0.1 -u root -p
mysql> use database wordpress;
mysql> show tables;
mysql> exit
Various workflows
Install and update WordPress, plugins, and themes
Because the wp-content directory on the server is read-only, you have to perform all code updates locally. Run WordPress locally and update the plugins and themes in the local Dashboard, deploy the code to production, then activate them in the production Dashboard. You can also use the wp-cli utility as follows. Be sure to keep the Cloud SQL proxy running.
# Install the wp-cli utility
$ composer require wp-cli/wp-cli-bundle
# Now you can run the "wp" command to update Wordpress itself
$ vendor/bin/wp core update --path=wordpress
# You can also update all the plugins and themes
$ vendor/bin/wp plugin update --all
$ vendor/bin/wp theme update --all
The following error may occur:
Failed opening required 'google/appengine/api/urlfetch_service_pb.php'
If you get this error, you can set a WP_CLI_PHP_ARGS environment variable to add include_path PHP configuration for wp-cli:
$ export WP_CLI_PHP_ARGS='-d include_path=vendor/google/appengine-php-sdk'
Then try the update commands again.
After everything is up to date, deploy the app again:
$ gcloud app deploy app.yaml cron.yaml
Note: This will deploy a new version of the app and set it as the default while keeping the previous versions available under versioned hostnames. Visit the App Engine, Versions area to see previous versions.
Alternately, you may deploy the new version and stop previous ones so they stop incurring charges:
$ gcloud app deploy app.yaml cron.yaml --promote --stop-previous-versions
Remove plugins and themes
To remove plugins and themes, first deactivate them in the production Dashboard, and then remove them completely locally. The next deployment will remove those files from the production environment.
create 3 new pages
No comments:
Post a Comment