Wordpress

In this example, we'll deploy Wordpress tool with its MariaDB database.

1. Create shared variables

Go to "Environment variables" menu and create this variable.

  • MY_WORDPRESS_DB_PASSWORD (don't forget to check the "hide value" box)

2. Create Wordpress and MariaDB services

services:
  - id: mywp
    name: myWP
    dockerConfiguration:
      image: wordpress
      imageVersion: latest
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 80
        healthCheck:
          healthCheckEnabled: true
        loadBalancerRules:
          - publicPort: 443
    cpuLimit: 512 # MHz
    memoryLimitMiB: 512
    links:
      - toServiceId: mydb
        toServicePort: 3306
        variableHost: MY_DB_HOST
        variablePort: MY_DB_PORT
        variableAddress: MY_DB_ADDRESS
    sharedEnvironmentVariables:
      - MY_WORDPRESS_DB_PASSWORD
    environmentVariables:
      - key: WORDPRESS_DB_HOST
        value: "%MY_DB_ADDRESS%"
      - key: WORDPRESS_DB_USER
        value: "wp"
      - key: WORDPRESS_DB_PASSWORD
        value: "%MY_WORDPRESS_DB_PASSWORD%"
      - key: WORDPRESS_DB_NAME
        value: "wp"
    volumes:
      - name: wpcontent
        path: /var/www/html/wp-content
  - id: mydb
    name: myDB
    type: database
    dockerConfiguration:
      image: mariadb
      imageVersion: "11.0"
    countMin: 1
    countMax: 1
    ports:
      - listeningPort: 3306
    cpuLimit: 512 # MHz
    memoryLimitMiB: 512
    sharedEnvironmentVariables:
      - MY_WORDPRESS_DB_PASSWORD
    environmentVariables:
      - key: MYSQL_RANDOM_ROOT_PASSWORD
        value: "yes"
      - key: MYSQL_USER
        value: "wp"
      - key: MYSQL_PASSWORD
        value: "%MY_WORDPRESS_DB_PASSWORD%"
      - key: MYSQL_DATABASE
        value: "wp"
In this example, we create both services with a single import, but you can import them in several steps.

Here is the section allowing communication between services:

links:
  - toServiceId: mydb
    toServicePort: 3306
    variableHost: MY_DB_HOST
    variablePort: MY_DB_PORT
    variableAddress: MY_DB_ADDRESS

This block instructs LayerOps that our WordPress service should be able to communicate with another service with id "mydb" on port 3306. In addition to creating the link, this block is used to inject environment variables into our wordpress service:

  • MY_DB_HOST: will contains the host to access service mydb (example: 127.0.0.1)
  • MY_DB_PORT: will contains the port to access service mydb (example: 9000)
  • MY_DB_ADDRESS: will contains the address to access service mydb (example: 127.0.0.1:9000)

Then, to indicate which address our Wordpress should use to communicate with the database, we add the WORDPRESS_DB_HOST environment variable.

environmentVariables:
  - key: WORDPRESS_DB_HOST
    value: "%MY_DB_ADDRESS%"
Check WORDPRESS_DB_HOST and other environment variables documentation

3. Enable backup

3.1 Enable database backup

To enable database backup, you need to add the following block to your YAML file:

databaseBackups:
  - s3BucketSecretUuid: <s3-bucket-secret-uuid>
    compressionFormat: zstd
    retentionDays: 7
    cronExpression: 0 * * * * # every hour at x:00
    alertThresholdMinutes: 60
    backupName: wpdb
    databaseName: wp
    databaseUser: wp
    databasePassword: "%MY_WORDPRESS_DB_PASSWORD%"
    isDatabasePasswordSensitive: false
    backupImage: mysql:9.1
You can configure multiple backup destinations by adding several backup configurations. This is useful for implementing redundant backups or writing to multiple destinations (Y-writing pattern). Simply add multiple entries in the databaseBackups array.

Here is the full example:

  services:
    - id: mywp
      name: myWP
      dockerConfiguration:
        image: wordpress
        imageVersion: latest
      countMin: 1
      countMax: 1
      ports:
        - listeningPort: 80
          healthCheck:
            healthCheckEnabled: true
          loadBalancerRules:
            - publicPort: 443
      cpuLimit: 512 # MHz
      memoryLimitMiB: 512
      links:
        - toServiceId: mydb
          toServicePort: 3306
          variableHost: MY_DB_HOST
          variablePort: MY_DB_PORT
          variableAddress: MY_DB_ADDRESS
      sharedEnvironmentVariables:
        - MY_WORDPRESS_DB_PASSWORD
      environmentVariables:
        - key: WORDPRESS_DB_HOST
          value: "%MY_DB_ADDRESS%"
        - key: WORDPRESS_DB_USER
          value: "wp"
        - key: WORDPRESS_DB_PASSWORD
          value: "%MY_WORDPRESS_DB_PASSWORD%"
        - key: WORDPRESS_DB_NAME
          value: "wp"
      volumes:
        - name: wpcontent
          path: /var/www/html/wp-content
    - id: mydb
      name: myDB
      type: database
      dockerConfiguration:
        image: mariadb
        imageVersion: "11.0"
      countMin: 1
      countMax: 1
      ports:
        - listeningPort: 3306
      cpuLimit: 512 # MHz
      memoryLimitMiB: 512
      sharedEnvironmentVariables:
        - MY_WORDPRESS_DB_PASSWORD
      environmentVariables:
        - key: MYSQL_RANDOM_ROOT_PASSWORD
          value: "yes"
        - key: MYSQL_USER
          value: "wp"
        - key: MYSQL_PASSWORD
          value: "%MY_WORDPRESS_DB_PASSWORD%"
        - key: MYSQL_DATABASE
          value: "wp"
      databaseBackups:
        - s3BucketSecretUuid: <s3-bucket-secret-uuid>
          compressionFormat: zstd
          retentionDays: 7
          cronExpression: 0 * * * * # every hour at x:00
          alertThresholdMinutes: 60
          backupName: wpdb
          databaseName: wp
          databaseUser: wp
          databasePassword: "%MY_WORDPRESS_DB_PASSWORD%"
          isDatabasePasswordSensitive: false
          backupImage: mysql:9.1

3.2 Enable wordpress volume backup

In addition to database backups, LayerOps allows you to backup volumes containing your application data. For WordPress, this is particularly useful to backup your uploads, themes, and plugins stored in the wp-content directory.

To enable volume backup, you need to add a backup configuration directly in the volume definition:

volumes:
  - name: wpcontent
    path: /var/www/html/wp-content
    backup:
      s3BucketSecretUuid: <s3-bucket-secret-uuid>
      compressionFormat: zstd
      retentionDays: 7
      cronExpression: 0 0 * * * # every day at midnight
      alertThresholdMinutes: 1440 # 24 hours

This configuration will:

  • Backup the wp-content directory
  • Store the backup in the specified S3 bucket
  • Compress the backup using zstd
  • Keep backups for 7 days
  • Create a backup every day at midnight
  • Alert if no backup has been created in the last 24 hours