Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Remote Control WordPress


Published on

Automated deployment woth WP-CLI and Aliases

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Remote Control WordPress

  1. 1. REMOTE CONTROL WORDPRESS Automated Deployment with WP-CLI and Aliases
  2. 2. 2 Agenda. 1. Introduction 2. Local Development with WP-CLI 3. SSH Intro and Configuration 4. WP-CLI and Aliases 5. Database Commands 6. Scripting Multiple Commands 7. Complete Script 8. Questions?
  3. 3. Introduction
  4. 4. Automated Deployment with WP-CLI and Aliases SENIOR SALES ENGINEER WP ENGINE @SPICECADET Edmund Turbin
  5. 5. 5 Understand the problem Define success Think creatively Sometimes solutions are not apparent Can involve 3rd Parties or combinations of technologies How can I overcome a challenge? Sales Engineering = Finding Solutions.
  6. 6. Staging/Dev/Production 6 Deploy DB Changes to Local Development Environment from Production/Staging/Dev server What Challenge are we Overcoming? Local Environment
  7. 7. 7 Developing WordPress locally Create Aliases to access WordPress remotely Connecting to the remotes via SSH Using WP-CLI commands to execute MySQL queries Deploy DB Changes from Local Development to Production/Staging/Dev server What’s Involved?
  8. 8. Local Development with WP-CLI
  9. 9. 9 Local Dev Stack - MAMP/WAMP Virtual Machines Docker - Containers Local Server (running on your laptop) Local dev solution from web host Many ways to work locally Developers generally choose what works for them Local Development Approach
  10. 10. Local Development Virtual Machine Configuration Local Development Tools. 10 Vagrants/VVV
  11. 11. VirtualBox Hypervisor. 11 Virtual Machine Manager (VMM) Creates and runs Virtual Machines Runs on a Host Machine VMs are called Guest Machines
  12. 12. VirtualBox Virtualization. 12 Multiple virtual machines can run at the same time Guest operating systems can be different - Mac, Windows, Linux, etc. VMs can be pre-built with commonly used apps - Apache, Nginx, email server Save state - start/stop, freeze, back up, copy
  13. 13. Host Local machine where virtual instances run Hypervisor Thin layer on host that allocates CPU, memory and resources to a virtual instance Virtual Machine. 13 HOST OS HYPERVISOR GUEST VM 1 APPS OS GUEST VM 2 APPS OS
  14. 14. Vagrant Vagrant. 14 Automation tool for building and managing VMs Uses VirtualBox as a base Uses a provisioning tool for automation Automatically installs and configures software on the VM
  15. 15. WP-CLI. 15 Access commonly functionality in WordPress from the command line. Do something quickly in WordPress without menu- diving Automate repetitive tasks Bundle several tasks together to create repeatable scripts WordPress Command Line Interface
  16. 16. Bourne-Again Shell BASH - Shell Scripting. 16 Unix shell and scripting language Released in 1989 as free software Common on macOS, Linux, Windows 10, etc. Can execute commands from text and from file - shell script Allows control structures and variables
  17. 17. 17 Built on Ubuntu 14.04 LTS (Trusty) base VM Provisions the following software packages VVV - What’s under the hood Tools DatabaseServer
  18. 18. SSH Intro and Configuration
  19. 19. 19 VVV is seen as a separate instance Even though the guest runs on the host machine, it acts like a remote SSH is required to access the command line of the guest machine Secure Shell Why do we need SSH? > SSH
  20. 20. 20 Public Key Authentication SSH Intro > SSHRemote Public Key Client Private Key
  21. 21. 21 Remote connection between two systems Secured by public/private key authentication client has an encrypted private key server has a public key connection checks to make sure keys are valid Replaces password authentication for quicker access Secure Shell SSH Intro > SSH
  22. 22. 22 secure connection over port 22 ssh user_name@hostname add key to list of hosts -i option will allow public/private key authentication to be used SSH Command. > SSH Secure Shell
  23. 23. 23 Host your_site HostName User user_name IdentityFile path_to_private_key Basic SSH Config File.
  24. 24. 24 vagrant ssh-config Host your_site HostName  User vagrant  Port 2200  UserKnownHostsFile /dev/null  StrictHostKeyChecking no  PasswordAuthentication no  IdentityFile /path_to_private_key  IdentitiesOnly yes  LogLevel FATAL  ForwardAgent yes Generate Vagrant SSH.
  25. 25. 25 vagrant ssh-config Host your_site HostName  User vagrant  Port 2200  UserKnownHostsFile /dev/null  StrictHostKeyChecking no  PasswordAuthentication no  IdentityFile /path_to_private_key  IdentitiesOnly yes  LogLevel FATAL  ForwardAgent yes Port Number Can Change - Regenerate.
  26. 26. WP-CLI and Aliases
  27. 27. 27 WP-CLI.
  28. 28. 28 Shortcuts that you register in a .yml file Allow you to run commands against a remote WordPress instance Can be grouped together Bypass remote login, change directories to get to a WordPress install WP-CLI Features which requires some quick config WP-CLI Aliases ALIASES
  29. 29. 29 Lesser known feature of WP-CLI Aliases can be globally set or on a site by site basis What’s the benefit? time saved WP-CLI Features which requires some quick config WP-CLI Aliases ALIASES
  30. 30. 30 @prod: ssh: your_production_site @dev: ssh: your_development_site path: /srv/www/edmund @both: - @prod - @dev wpcli.yml or config.yml ALIASES
  31. 31. 31 /** * * WP-CLI commands that use Aliases * */ wp cli info wp @dev cli info wp @prod cli info Basic Commands with Aliases ALIASES
  32. 32. Database Commands
  33. 33. 33 # Get help and list of commands wp help db Basic DB Commands.
  34. 34. 34 Basic DB Commands.
  35. 35. Scripting Multiple Commands
  36. 36. 36 # check if a plugin is installed wp @both plugin is-installed hello # return value for last command - 0 = true echo $? # install plugin if $? false if [ $? -eq 0 ]; then wp @prod plugin install hello —— activate; fi Simple Logical Plugin Example.
  37. 37. 37 #! /bin/bash # check if a plugin is installed wp @dev plugin is-installed hello # install plugin if $? false if [ $? -eq 0 ]; then wp @dev plugin install hello ——activate; fi We Can Save Commands in a Script.
  38. 38. 38 # create a variable and save a string NAME=edmund # Print the value of the variable echo $NAME edmund We Can Save Values in a Variable.
  39. 39. 39 # create a variable and save a string BLOG_NAME=${wp option get blogname} # Print the value of the variable echo $BLOG_NAME Edmund Turbin’s Blog We Can Save Commands in Variables.
  40. 40. 40 # Save command output as a variable PROD_SITEURL=$(wp @prod option get siteurl); DEV_SITEURL=$(wp @dev option get siteurl); Get URLs from dev and production.
  41. 41. 41 # Get db from remote WP site and download it as a local file wp @prod db export - > prod.sql - option prints to screen > option redirects output to a local file Export the WordPress DB.
  42. 42. 42 # Import the downloaded .sql file wp @dev db import /srv/www/edmund/prod.sql Full path used to point to the .sql file Import the WordPress DB on Dev.
  43. 43. 43 # Search and replace siteurl option wp @dev search-replace $prod_siteurl $dev_siteurl Change production siteurl to dev.
  44. 44. 44 # Delete the file rm prod.sql Cleanup.
  45. 45. 45 # Chane permissions on our file chmod +x # Execute the script ./ Make the script executable and run it.
  46. 46. Complete Script
  47. 47. 47 #!/bin/bash #get siteurl option from prod and dev PROD_SITEURL=$(wp @prod option get siteurl); DEV_SITEURL=$(wp @dev option get siteurl); #Export database from prod and import to dev wp @prod db export - > prod.sql wp @dev db import /srv/www/edmund/prod.sql #Update siteurl wp @dev search-replace $PROD_SITEURL $DEV_SITEURL #Remove .sql file rm prod.sql Complete Script.
  48. 48. Conclusion
  49. 49. 49 VVV for local development WP-CLI & Aliases Scripting can make a list of commands repeatable Conclusion
  50. 50. 50 automation-part-i-remote-control-wordpress-with-wp-cli- aliases/ Blog Post
  51. 51. 51 SSH Documentation: WP Migrate DB - WP-CLI Command Reference: Bash Script Tutorial: Going Further - WP Local Docker: References & Links
  52. 52. EDMUND.TURBIN@WPENGINE.COM @SPICECADET Thank you. Edmund Turbin - Senior Sales Engineer