Implementing a Modern WordPress Workflow on Alibaba Cloud — Part 3

$ sudo rm -rf /var/www/staging.another-example-domain.com

Part 1. Create a WordPress Staging Site Using WP-CLI

WP-CLI is the command line interface for WordPress, and it is an incredibly powerful tool for WordPress developers. With it you can update plugins, configure installs, manipulate your WordPress database, manage user and options, and so much more, all without using a browser.

Step 1. Install WP-CLI

If you have already installed WP-CLI, skip this step and move onto Step 2. You can check to see if WP-CLI is installed on your server with the following command:

$ wp --info
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp
$ wp --info

Step 2. Create Your Staging Site’s Root Directory

Change directory to your ‘/var/www/' directory:

$ cd /var/www
root /var/www/staging.another-example-domain.com;
$ sudo cp -rf another-example-domain.com staging.another-example-domain.com
$ ls

Step 3. Create Your Staging Site’s WordPress Configuration File

First change directory to your staging site’s root directory:

$ cd /var/www/staging.another-example-domain.com
$ sudo rm wp-config.php
$ sudo chown -R new_user:www-data /var/www/staging.another-example-domain.com
$ cd /var/www
$ ls -l
$ wp config create --dbname=staging --dbuser=new_user --dbpass=new_users_password

Step 4. Import Your Production Site’s Database into Your Staging Site’s Database

Change directory to your production sites root directory:

$ cd /var/www/another-example-domain.com
$ wp db export ~/production-db.sql
$ cd /var/www/staging.another-example-domain.com
$ wp db reset --yes
$ wp db import ~/production-db.sql
$ wp db query "show tables;"

Step 5. Use WP-CLI to Search and Replace Domains in Your Staging Database

At this point, our staging database still contains all the database entries for the original production site domain. We need to search for all references to the production domain and replace them with the staging site domain.

$ wp search-replace another-example-domain.com staging.another-example-domain.com --dry-run --network
$ wp search-replace another-example-domain.com staging.another-example-domain.com --network

Step 6. Rename Your Staging Site Using WP-CLI Option Command

Your site will now be up and running, but before we visit it, let’s change the sites name so that we can easily identify it as the staging site for when we are committing changes later in this series.

$ wp option update blogname "Staging Site WPCLI"

Step 7. Change Staging Site Directory Ownership to Allow WordPress Admin Updates

Currently the staging site directory belongs to the superuser. This means the web server user ‘www-data' won't be able to write to the directories and make change and we won't be able to install or delete plugins or themes from the WordPress admin.

$ sudo chown -R www-data:www-data /var/www/staging.another-example-domain.com
$ cd /var/www
$ ls -l

Step 8. Add Password Protection for Access to Our Staging Site

Our staging site can now be publicly accessed at the following url:
https://staging.another-example-domain.com

$ sudo apt-get update
$ sudo apt-get install apache2-utils
$ sudo htpasswd -c /etc/nginx/.htpasswd new_user
$ sudo nano /etc/nginx/sites-available/staging.another-example-domain.com
location / {
……
auth_basic "Staging Site - Access Restricted!";
auth_basic_user_file /etc/nginx/.htpasswd;
}
$ sudo nginx -t
$ sudo systemctl reload nginx
$ sudo nano /etc/nginx/sites-available/staging.another-example-domain.com
location / {
……
# auth_basic "Staging Site - Access Restricted!";
# auth_basic_user_file /etc/nginx/.htpasswd;
}
$ sudo nginx -t
$ sudo systemctl reload nginx

Part 2. Create a Local WordPress Development Site Using WP-CLI

Step 1. Install a Local Development Environment

To develop locally, you will need to configure a LEMP stack on your local machine. Luckily, there are many great applications for this in the WordPress and wider PHP development ecosystem. You can choose from several applications:

  1. Local by Flywheel
  2. Desktop Server
  3. MAMP
  1. VVV
  2. Valet
  3. Chassis

Step 2. Create a Fresh WordPress Site Using Local by Flywheel

Local by Flywheel makes it very easy to create a site. Click the large + button in the bottom left corner of the application to start the WordPress site creation wizard:

Step 3. Download Your Production Site Directory and Database to Your Local Computer

To download a copy of the production sites files and directories to our local machine, we will use an SFTP client. There are many FTP/SFTP clients, but may I recommend FileZilla as it is a good cross-platform, free and open source client.

Step 4. Replace Local Files with Production Files

On your local machine, delete the entire contents of you Local Sites ‘public' directory. This is the directory containing all the WordPress files and directories.

Step 5. Open an SSH Connection to Your Local Environment

Local creates each of its sites on a LEMP stack within their own Docker environment. This means we can SSH into the environment and run server tools from the terminal, including WP-CLI. Open an SSH connection to your Local Sites Environment like this:

Step 6. Use WP-CLI to Create a New WordPress Configuration File

Use the following command in the local terminal window to create your ‘wp-config.php' file, you will need to use the database name, user, and password from your Local environment:

# wp config create --dbname=local --dbuser=root --dbpass=root

Step 7. Import Your Production Database into Your Local Database Using WP-CLI

Change directory into your Local site’s public folder, and list the contents:

# cd /app/public
# ls
$ wp db reset --yes
$ wp db import production-db.sql
$ wp db query "show tables;"

Step 8. Use WP-CLI to Search and Replace Domains in Your Local Database

At this point, our local database still contains all the database entries for the original production site domain. As before with the staging site, we need to search for all references to the production domain and replace them with the local site domain.

$ wp search-replace another-example-domain.com another-example-domain.local --dry-run --network
$ wp search-replace another-example-domain.com another-example-domain.local --network

Step 9. Rename Your Local Site Using WP-CLI Options Commands

Your site will now be up and running, but before we visit it, let’s change the sites name so that we can easily identify it as the Local site for when we are committing changes later in this series.

$ wp option update blogname "Local Site WPCLI"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com