Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…

30Jun/140

Migrate WordPress to Google App Engine

Wordpress on Google App Engine

Why not run WordPress on Google App Engine? You will get performance and stability while only paying for the resources you actually use. Reading the official Google tutorial "Running WordPress in App Engine" it gives you a fare idea what you are in for. But if you want to migrate a currently running site then you have to do some tweaking. So here is a run down on how to do it!

    1. Download all data from current site

      Download all files in the public html directory from you site. You also have to obtain a copy of your database. The way you do this depends on what you current ISP setup. Please note that comments and other content added after this DB download will not be avalible in the new installation. Therefore it’s recommended that you take a fresh backup when you see that everything works as it’s supposed to.

    2. Setup local environment

      To be able to do this you need four components installed on your local machine.
      Python – Even though your developing on PHP python is required for Google App Engine Launcher to work.
      PHP – Download and install the latest version.
      Google App Engine SDK – Includes all you need to develop, test and deploy to Google App Engine.
      MySQL – For database support in the devenviorment.

    3. Setup Google App Engine Launcher

      In the Google App Engine Launcher "File" menu select “Create new application...
      Application name” – Will be the Google App Engine identifier when you upload, don’t worry this can be changed later.
      "Parent Directory" - Where you will store the files localy.
      Runtime” – Select PHP.
      Delete the “main.php” file that is created in the folder.
      Copy all the contents you downloaded from your public html directory.

    4. Setup “app.yaml

      The "app.yaml" contains the configuration information for Google App Engine. The first line contains "application:", you will notice that this is the same as the "Application name" you selected in the previous step. Important to remember here is that this will be part of you web address if you don't use a custom domain e.g. example.com, so in that case you will end up with applicationname.appspot.com.

      Next few rows is nothing you need to worry about: version, runtime, api_version and threadsafe you can leave as they are.

      The handlers: on the other hand is really important to get the WordPress site working on Google App Engine. You should use this:

      handlers:
      - url: /(.*\.(htm|html|css|js))$
      static_files: \1
      upload: .*\.(htm|html|css|js)$
      application_readable: true
      
      - url: /wp-content/(.*\.(ico|jpg|png|gif))$
      static_files: wp-content/\1
      upload: wp-content/.*\.(ico|jpg|png|gif)$
      application_readable: true
      
      - url: /(.*\.(ico|jpg|png|gif))$
      static_files: \1
      upload: .*\.(ico|jpg|png|gif)$
      
      - url: /wp-admin/(.+)
      script: wp-admin/\1
      secure: always
      
      - url: /wp-admin/
      script: wp-admin/index.php
      secure: always
      
      - url: /wp-login.php
      script: wp-login.php
      secure: always
      
      - url: /wp-cron.php
      script: wp-cron.php
      login: admin
      
      - url: /xmlrpc.php
      script: xmlrpc.php
      
      - url: /wp-(.+).php
      script: wp-\1.php
      
      - url: /(.+)?/?
      script: index.php
    5. Setup “cron.yaml

      This file is for the cron jobs. This is the same as on Linux/Unix machines and replaces the timer jobs WordPress have on other platforms. This file should contain:

      cron:
      - description: wordpress cron tasks
      url: /wp-cron.php
      schedule: every 2 hours
    6. Setup local database

      So after installation of the MySQL software start the MySQL Workbench. (There maybe some initial configuration wizards here, I already had it installed when I wrote the tutorial!)
      Double click the "Local instance MySQL..."
      If "Server Status" is "Stopped" then go to the "Server" menu and select "Startup/Shutdown" then click the "Start Server" button.
      When server is "Running" click the "Data Import/Restore" in the left hand "Navigator" menu.
      Select the "Import from Self-Contained File" radio button then browse for the database backup/dump you downloaded in step one.
      Under "Default Schema to be Imported To" click the "New..." button.
      Name the schema something you will remember.
      Click the "Start Import" button.
      When the database is successfully imported hit the little refresh icon next to "Schemas" in the left hand "Navigator" menu, now you should see your new database (or schema as it's called in MySQL).
      Double click on you new database and then on "Tables", find the "wp_options" table then right click and select "Select Rows - Limit 1000".
      Find the row where the field “option_name” is “active_plugins” and delete it! (This is to disable all active plugins before we upload)
      Find the rows where "option_name" is “siteurl” and “home” and set them to “http://localhost:portnumber”. The port number should correspond to the one in the applications "port" column in the Google App Engine Launcher application list.

      NOTE: You can also run this query against the server to delete the "active_plugins" row:

      DELETE FROM wp_options WHERE option_name = 'active_plugins';
    7. Configure “wp-config.php”

      Now you need to setup WordPress to access your database. This information is different between your local environment and the Google App Engine environment. I simply use an if statement to take care of that problem and save me the trouble of changing the wp-config.php file every time I update my site.

      if(isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'],'Google App Engine') !== false) {
          /** Google App Engine Settings */
          define('DB_NAME', 'hackviking_com');
          define('DB_USER', 'root');
          define('DB_PASSWORD', '');
          define('DB_HOST', ':/cloudsql/wp-hackviking-com:db');
      }
      else
      {
          /** Local Settings */
          define('DB_NAME', 'hackviking_com');
          define('DB_USER', 'root');
          define('DB_PASSWORD', 'mylocaldbpassword');
          define('DB_HOST', 'localhost');
      }

      DB_NAME - Name of the database. Locally it's the schema name you selected in step 6 when you imported your database. For the name used on Google App Engine please see step 9.
      DB_USER - Usually it's root locally if you haven't customized your setup. On Google App Engine it's always root.
      DB_PASSWORD - Password for the corresponding DB_USER. On Google App Engine the password for root is always null due to access control being controlled on app level.
      DB_HOST - On local environment it's "localhost". On Google App Engine please see step 9.

    8. Prepare local site for upload

      Run the site and login to the WordPress admin site e.g. "http://localhost:<portnumber>/wp-admin".
      Run all available updates, you will not be able to do this when the site is uploaded. To install plugins and updates you have to do it locally and then reupload. Therefor it’s a good idea to keep your local environment to be able to take care about this later on.
      Delete all WordPress plugins that will not work on Google App Engine, it’s not easy to know before you try but a role of thumb is that caching and minifying plugins won’t work.
      Install WordPress plugins required for Google App Engine.
      Google App Engine for WordPress
      Memcache Object Cache
      Batcache

    9. Setup the "Google Cloud Platform"

      1. Log in and create a new project by clicking "Create Project". If the "Project ID" is available that will automatically be the name of your Google App Engine instance and that name is part of your web address e.g. <projectID>.appspot.com. NOTE: This can not be changed later, if your not happy with your url you have to create a new project!
      2. To be able to run "Cloud SQL" needed for the MySQL implementation in WordPress you have to enable billing for the project. In the projects overview click your new project go to "Billing & Settings" and click "Enable Billing". Fill out all the details for the billing and verify your payment option.
      3. Now you can create the database for your system. In your project view select "Storage" ->"Cloud SQL" -> "New Instance".
      "Instance ID" - The name of your database. The name will be <projectID>:<InstanceID>.
      "Tier" - Start out with "D0 - 128M RAM" and upgrade later if you need.
      "Billing plan" - I prefer the "Per Use" when I start out.
      "Preferred Location" - The logical choice is to select "Follow App Engine App".
      "Assign IP Address" - Select "Assign IP Address", we need this later to upload our database! NOTE: DON'T FORGET TO DELETE THIS WHEN YOUR DONE OR IT WILL COST YOU!
      "Authorized IP Address" - Your public IP address that you will use to access the database later. If you don't know visit whatsmyip.org
      Click "Confirm" and the database will be set up. Go to "Access Control" and use the "Set root password" to set a password for the root account.
      4. Setup file storage for file uploads.
      Before you can set this up you need the "Service Account Name" for your Google App Engine instance. Easiest way to access this is to select "Compute" -> "App Engine" -> "Quota details" which will send you the the Google App Engine dashboard. Then select "Application Settings" under "Administration" and copy the "Service Account Name".
      From the project view go to "Storage" -> "Cloud Storage" -> "Storage Browser" -> "New Bucket" and select a name for it. I like to use the same name as my "Project ID".
      Then tick the box next to your new bucket and click "Bucket Permissions".
      Under "Add another permission" select "User" in the drop down, enter the "Service Account Name" and select "Writer" as permission and then click "Add"

    10. Upload database

      So now you have to get you database online. Start by creating a database backup of your local database.
      If you didn't authorize your IP address in step 9 follow these instructions to do so:
      1. Login to your Google Cloud Consol.
      2. Select your project name.
      3. Click  "Storage" -> “Cloud SQL”.
      4. Select your instance name.
      5. Click “access control”.
      6. Click “request an IPaddress”.
      7. Click “add authorized network” and enter your public IP address. If you don’t know visit whatsmyip.org.
      8. Click “set root password” and set it to something you remember.
      9. Connect with MySQL workbench to the database instance you created in step 9 and import your database backup to a new schema.
      10. Go into the database and set the “siteurl” and “home” to <projectID>.appspot.com.
      11. If the row “active_plugins” exists delete it!
      NOTE: Don’t forget to disable the external access after you’re done! For security reasons and cost. It costs every hour it’s open.

    11. Final WordPress config & deployment

      Now you have to go back to step 7 and put the new configuration into the "wp-config.php" file.
      In step 4 we talked about app.yaml, now you need to update that file! The line saying application: should be updated with you project ID from step 9.
      When this is done hit the "Deploy" button in Google App Engine Launcher to upload you site.

    12. Config site online

      Go to <projectID>.appspot.com/wp-admin and login with your admin account.
      Enable plugins:
      Google App Engine for WordPress
      Memcache
      Batcache
      When this is done go to "Settings" -> "App Engine" under "Upload Settings" you have "Bucket name" fill in the one you setup in step 9. This is for storing uploaded media files.
      Then enable your plugins one by one and test that the site is working. If you encounter issues with this just enable external DB access again and remove the “active_plugins” row from the database.

Trouble shooting

If you encounter issues with WordPress caching still enabled then try the following
Delete “advanced-cache.php
Delete “wp-cache-config.php
Delete “cache” folder from “wp-content” and “wp-includes
In “wp-config.php” set “WP_CACHE” to “false

 

Hope you liked this tutorial, if so please share it with others! Please tell me what you think in the comments below and give my any suggestions/requests you have! Stay tuned for more articles about WordPress and App Engine.

Comments (0) Trackbacks (0)

No comments yet.


Leave a Reply

No trackbacks yet.