×
Community Blog Cloud Native WordPress on Alibaba Cloud

Cloud Native WordPress on Alibaba Cloud

This article shares how you can quickly start with WordPress on Alibaba Cloud to experience cloud native features such as high availability and auto-scaling.

In this blog, we'll show you how you can quickly start with WordPress on Alibaba Cloud to experience cloud native features such as high availability, auto-scaling, etc.

You can access all the tutorial resources, including the deployment script (Terraform), related source code, sample data, and instruction guidance, from the GitHub project: https://github.com/alibabacloud-howto/solution-cloud-native-web-hosting

For more tutorials around Alibaba Cloud Database, please refer to: https://github.com/alibabacloud-howto/database

Project URL

https://github.com/alibabacloud-howto/solution-cloud-native-web-hosting

Architecture Overview

1

Deployment

Terraform

Use terraform to provision VPC, SLB, EIP, ESS, ECS, Redis, and PolarDB instances that used in this solution against this .tf file: https://github.com/alibabacloud-howto/solution-cloud-native-web-hosting/blob/main/deployment/terraform/main.tf

For more information about how to use Terraform, please refer to this tutorial: https://www.youtube.com/watch?v=zDDFQ9C9XP8

Run Demo

Step 1: Install Apache HTTP Server and PHP on ECS

  • Logon to ECS via SSH, use the account root/N1cetest, the password has been predefined in Terraform script for this tutorial. If you changed the password, please use the correct password accordingly.
ssh root@<EIP_ECS>

There are 2 ways to get the EIP applied for the ECS in Terraform:

  • Go to ECS web console:

2

  • In the command line, under the same folder of the terraform script file main.tf, open "terraform.tfstate":

3

If you encounter this error when performing SSH to ECS, please go to /Users/xxx/.ssh/known_hosts to edit the file and remove the whole line with the EIP of the target ECS at the very beginning. After that, please SSH to log on again.

4

  • Run the following command to install required utilities on the instance:
yum install -y sysbench unzip zip dstat
- Zip: a utility commonly used for compressing files and folders 
- Unzip: a utility used for decompressing files and folders.  
- Sysbench: a benchmarking tool used for system performance testing. 
- Dstat: a monitoring tool that provides statistics about system performance.  
  • Install Apache HTTP Server

Run the following command to install the Apache HTTP server:

yum -y install httpd
  • Install PHP

List available versions of PHP:

dnf module list php

Most likely php 7.4 is included, so run the following commands to enable PHP 7.4 (Please make sure the PHP version is new to catch up the requirement of the Wordpress, otherwise Wordpress installation would fail possibly):

dnf module reset php
dnf module enable php:7.4
dnf install -y php php-opcache php-gd php-curl php-mysqlnd
dnf install -y php-bcmath php-mbstring php-xmlwriter php-xmlreader php-cli php-ldap php-zip php-fileinfo

Then restart Apache HTTP Server:

service httpd restart

Configure to auto start httpd service when ECS restarting:

chmod +x /etc/rc.d/rc.local
vim /etc/rc.d/rc.local

Add the line at the end and save:

service httpd restart

5

Create a PHP file to verify the PHP is working:

vim /var/www/html/info.php

Then input the following content in this info.php file, then save and exit.

<?php
phpinfo();
?>

Then open the following URL in a Web browser (Note: Replace the placeholder with the Elastic IP address of the ECS instance that you obtained previously):

http://<ECS_EIP>/info.php

If the following page appears, PHP is installed successfully.

6

Step 2: Install and Configure WordPress on ECS

Create a folder, download the WordPress package to this folder, and extract the package. To do this, run the following commands in sequence:

mkdir -p /opt/WP  
cd /opt/WP 
wget https://wordpress.org/latest.tar.gz 
tar -xzvf latest.tar.gz 

Run the following commands in sequence to configure WordPress to access ApsaraDB for PolarDB:

cd /opt/WP/wordpress/ 
cp wp-config-sample.php wp-config.php 
vim wp-config.php 

Complete the database configurations as follows:

Setting Value & description
DB_NAME The name of the ApsaraDB for PolarDB database that you created. In this tutorial, we use "wpdb", which is predefined in resource "alicloud_polardb_database" within Terraform script https://github.com/alibabacloud-howto/solution-cloud-native-web-hosting/blob/main/deployment/terraform/main.tf.
DB_USER The user name of the database account you created. In this lab, we use "test_polardb" as predefined within Terraform script.
DB_PASSWORD The password of the database account you created within Terraform script. In this lab, we use "N1cetest" as predefined within Terraform script.
DB_HOST The VPC-facing endpoint of the ApsaraDB for PolarDB cluster that you obtained previously. Do not include the port number. Please use the Cluster endpoint of PolarDB.

Endpoint on PolarDB web console:

7
8

Run the following commands in sequence to copy the wordpress folder to the /var/www/html/ path:

cd /var/www/html 
cp -rf /opt/WP/wordpress/* /var/www/html/ 

Open the following URL in a Web browser to initialize WordPress:

http://<ECS_EIP>

Note: Replace the placeholder with the Elastic IP address of the ECS instance that you obtained previously.

Then complete the settings and click "Install WordPress".

9

Then the following page shows, which means the installation is success.

10

Step 3: Configure Redis Caching

Run the following commands in sequence to download the Redis object cache plugin and unzip the plugin package:

cd /opt/WP
wget https://downloads.wordpress.org/plugin/redis-cache.2.0.18.zip 
unzip redis-cache.2.0.18.zip 

Run the following commands in sequence to copy the redis-cache folder to the /var/www/html/wp-content/plugins/ path and configure WordPress to access ApsaraDB for Redis:

cp -rf redis-cache /var/www/html/wp-content/plugins/ 
vim /var/www/html/wp-config.php

Complete the settings as follows:

Setting Value & Description
WP_REDIS_HOST The internal endpoint of the ApsaraDB for Redis instance that you obtained previously. Such as r-xxxxx.redis.singapore.rds.aliyuncs.com
WP_REDIS_PORT The port number.
WP_REDIS_PASSWORD The password for connecting to the instance.
// Redis settings
define( 'WP_REDIS_HOST', '<Redis URL>' );
define( 'WP_REDIS_CLIENT', 'predis' );
define( 'WP_REDIS_PORT', '6379' );
define( 'WP_REDIS_DATABASE', '0');
define( 'WP_REDIS_PASSWORD', 'test_redis:N1cetest' );

11

Please MAKE SURE this Redis setting block is set at the first settings block of the wp-config.php file as shown in the image above.

Run the following command to copy the object-cache configuration file to the /var/www/html/wp-content/ path:

cp /var/www/html/wp-content/plugins/redis-cache/includes/object-cache.php /var/www/html/wp-content/ 

Log on to WordPress to enable Redis object cache.

12

In the left-side navigation pane, click Plugins. Find the Redis Object Cache plugin and click Activate.

After the plugin is activated, click Settings.

13

Verify that the plugin status is Connected. Click Flush Cache to synchronize cache data to the ApsaraDB for Redis instance.

14

Now, your cloud native Wordpress has been setup successfully. You can visit it via SLB EIP:

http://<SLB_EIP>/

15

Step 4 (Optional): Make Custom ECS Image for Auto Scaling

Follow these steps to create an image from the ECS instance:

  • Log on to the ECS console. Click "Images" in the left-side navigation pane, then Click "Create Now".

16

  • Select "Instance", select the target ECS that with WordPress installed previously, enter a name and a description for the image, and then click "Create". In this lab, the image name is wp_image.

17

  • Wait until the image creation progress becomes 100%.

18

Step 5 (Optional): Setup Auto Scaling (ESS) for ECS Auto Scaling

Follow these steps to enable Alibaba Cloud Auto Scaling:

  • Log on to the Auto Scaling console. Click "Create Scaling Group".

19

  • Select "Create from Scratch" and click "Start Creation".

20

  • In the "Create Scaling Group" section, complete the settings as follows:
Setting Value & Description
Scaling Group Name wp_auto_scaling
Instance Configuration Source Create from Scratch
Instance Removing Policy For "Filter First", select "Earliest Instance Created Using Scaling Configuration". For "Then Remove from Results", select "Most Recent Created Instance".
Minimum Number of Instances 2
Maximum Number of Instances 5
Default Cooldown Time (Seconds) 300
Network Type VPC
Multi-zone Scaling Policy Balanced Distribution Policy
Instance Reclaim Mode Release Mode
VPC Select the VPC created before by Terraform
Select VSwitch Select all the VSwitches created before by Terraform. There are 2 VSW.
Associate SLB Instance Select the SLB created before by Terraform.

21
22
23

  • Click "OK" to finish the creation of the scaling group.

24

  • Click "Add Scaling Configuration".

25

  • Complete the settings as following:
Setting Value & description
Billing Method Pay-As-You-Go
Instance Type ecs.g5.xlarge and ecs.c5.xlarge, or you can select other ECS class to the optional list for auto scaling
Image Click "Custom Image" and select the "wp_image" image that you created previously.
Storage Select "Ultra Disk" and "40 GiB" for the system disk. Click "Add Disk" and select "Ultra Disk" and "100 GiB" for the data disk.
Security Group Select the security group that you created previously.

26
27

  • In the "Logon Credentials" section, select “Inherit Password from Image”. In the "Instance Name" field, enter a name for the instance. In this lab, we use WP. Then click "Preview".

28

  • In the "Scaling Configuration Name" field, enter a name for the scaling configuration. In this lab, we use wp_as_group. Then click "Create".

29

  • Wait until the success message appears. Click "Enable Configuration" and "OK" to enable the scaling configuration.

30
31
32

  • Click the "Instances", "Manually Added" tab and then click "Add Existing Instances".

33

  • Add the ECS that you installed WordPress previously.

34

  • Follow the following steps to create 2 scaling rules (ADD and DROP).

35

Setting Value & description
Name ADD1
Rule Type Simple Scaling Rule
Operation Add 1 Instances

36

Setting Value & description
Name DROP1
Rule Type Simple Scaling Rule
Operation Remove 1 Instances

37

  • Follow the following steps to create 2 event triggered taskes mapping to the scaling rules respectively (CPU busy to ADD, CPU idle to DROP).

38

Setting Value & description
Task Name cpu_busy
Resource Monitored Select the "wp_auto_scaling" scaling group you created previously.
Monitoring Type System Monitoring
Monitoring Metric (ECS) CPU Utilization
Condition Average >= Threshold 70%
Triggered Rule Select the "ADD1" rule you created previously.

39

Setting Value & description
Task Name cpu_idle
Resource Monitored Select the "wp_auto_scaling" scaling group you created previously.
Monitoring Type System Monitoring
Monitoring Metric (ECS) CPU Utilization
Condition Average <= Threshold 50%
Triggered Rule Select the "DROP1" rule you created previously.

40

  • Verify that the statuses of new scaling tasks are both Normal.

41

  • After for a while (since the default setting is "triggered after 3 times for each 1 minute reference period", so please be patient to wait at least 3 minutes), verify that the statuses of SLB and the backend servers are Normal. 1 new ECS server were scaled out by auto scaling service and attached to SLB instance for load balancing.

42

Step 6 (Optional): Simulate Fluctuating Traffic to Trigger Auto Scaling

  • Modify the auto scaling event-triggered task "cpu_busy" with the condition to "Maximum >= 70%"

43
44

  • Log on to the ECS with EIP bound before, and run the sysbench to simulate the high CPU workload
sysbench cpu --cpu-max-prime=4000000 --threads=4 --time=1000 run
  • After for a while (since the default setting is "triggered after 3 times for each 1 minute reference period", so please be patient to wait at least 3 minutes), verify the auto scaling event triggered with a new instance added

45

  • After for a while, verify the SLB instance is attached with 3 ECS instances.

46

  • Then you can "ctrl + C" to stop the sysbench workload, and verify the ECS count backs to 2 following the "cpu_idle" auto scaling rule after the rule event is triggered.
0 0 0
Share on

ApsaraDB

148 posts | 11 followers

You may also like

Comments