Skip to content

Share wp-content in WordPress

Run WordPress on an autoscaling group behind a load balancer, with uploads, plugins and themes in wp-content on a shared file system.

Updated View as Markdown

WordPress keeps uploads, plugins and themes in wp-content. With more than one server, every server needs the same wp-content. This guide puts it on a Files access point, runs WordPress on an autoscaling group whose members mount it at boot, and puts an HTTP load balancer in front.

You need:

  • a VPC with a subnet for the servers and one for the load balancer, as Load balancers describes;
  • a MySQL-family database for WordPress, reachable from the servers’ subnet.

The servers run linux/ezgh-ubuntu:24.04, where TLS mounts need extra setup, so they mount without TLS. WordPress core is installed on each server; only wp-content is shared.

  1. Create the file system and a mount target in the servers’ subnet, as in Get started. Note the mount target’s hostname.

  2. Create an access point for wp-content. Apache and PHP run as www-data, user and group 33 on Ubuntu. The access point makes every request act as 33:33, so files written from any server have the same owner, and creates the folder:

    curl -X POST -H "Authorization: Bearer $EZGH_API_KEY" \
      -H "Content-Type: application/json" -H "Idempotency-Key: $(uuidgen)" \
      -d '{"fileSystem": "fsy_4k2m9x0a7qpe", "name": "wp-content",
           "rootDirectory": {"path": "/wp-content", "creationInfo": {"ownerUid": 33, "ownerGid": 33, "permissions": "755"}},
           "posixUser": {"uid": 33, "gid": 33}}' \
      "https://files-api.ezghcloud.com/v1/organizations/$ORG_ID/projects/$PROJECT_ID/access-points"
  3. Create an HTTP target group and a load balancer with an http listener on port 80, or an https one, forwarding to the group, as in Route HTTP traffic. The servers’ private interfaces use the VPC’s default firewall, which already admits the load balancer and reaches the mount target.

  4. Let the servers add themselves to the target group. Each server registers its private interface when it boots, with its instance bot. Give the bot lb.targetGroups.registerTargets on the target group, and nothing else:

    register-wordpress.jsonjson
    {
      "statements": [
        {
          "effect": "allow",
          "actions": ["lb.targetGroups.registerTargets"],
          "resources": ["ezgh:us-west-1:org_k3f9a0x2m7qp:prj_g7h8i9j0k1l2:tgp_7t3n1v8c2wzd"]
        }
      ]
    }
    ezgh iam policies create register-wordpress --document @register-wordpress.json
    ezgh iam bots create wordpress-servers
    ezgh iam policies attach register-wordpress --bot wordpress-servers
  5. Write the user data. It installs Apache, PHP and WordPress, mounts the access point at wp-content, turns on OPcache, and registers the server. Replace the values in <…>. wp-config.php takes your database’s endpoint and credentials; fetch the password at boot as in Read a secret on an instance rather than writing it into the user data.

    user-data.yamlyaml
    #cloud-config
    packages:
      - apache2
      - libapache2-mod-php
      - php-mysql
      - php-gd
      - php-curl
      - php-xml
      - php-mbstring
      - php-zip
      - php-intl
    write_files:
      - path: /etc/php/8.3/apache2/conf.d/90-opcache.ini
        content: |
          opcache.enable=1
          opcache.memory_consumption=256
          opcache.max_accelerated_files=20000
          opcache.validate_timestamps=1
          opcache.revalidate_freq=60
      - path: /usr/local/bin/register-target
        permissions: "0755"
        content: |
          #!/bin/sh
          set -eu
          ORG=<organization ID>
          PROJECT=<project ID>
          TARGET_GROUP=<tgp_…>
          MD=http://[fd65:7a67:6800::254]/ezgh/v1
          ENI=$(curl -fsS -H "EZGH-Metadata: true" "$MD/instance" |
            python3 -c 'import json, sys; print(next(i["id"] for i in json.load(sys.stdin)["interfaces"] if i["kind"] == "private"))')
          TOKEN=$(curl -fsS -X PUT -H "EZGH-Metadata: true" -H "EZGH-Metadata-Token-TTL-Seconds: 60" "$MD/token")
          KEY=$(curl -fsS -H "EZGH-Metadata: true" -H "EZGH-Metadata-Token: $TOKEN" "$MD/credentials" |
            python3 -c 'import json, sys; print(json.load(sys.stdin)["apiKey"])')
          curl -fsS -X POST -H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
            -H "Idempotency-Key: $(cat /proc/sys/kernel/random/uuid)" \
            -d "{\"networkInterface\": \"$ENI\"}" \
            "https://lb-api.ezghcloud.com/v1/organizations/$ORG/projects/$PROJECT/target-groups/$TARGET_GROUP/targets"
    runcmd:
      - |
        set -e
        cd /tmp
        curl -fsSLO https://wordpress.org/wordpress-<version>.tar.gz
        curl -fsSL https://wordpress.org/wordpress-<version>.tar.gz.sha1 | sed 's/$/  wordpress-<version>.tar.gz/' | sha1sum -c -
        rm -rf /var/www/html
        tar -xzf wordpress-<version>.tar.gz
        mv wordpress /var/www/html
        mv /var/www/html/wp-content /var/www/html/wp-content.dist
        mkdir /var/www/html/wp-content
        chown -R www-data:www-data /var/www/html
        echo "<hostname>:/<fap_…> /var/www/html/wp-content nfs4 nfsvers=4.1,hard,noresvport,rsize=1048576,wsize=1048576,timeo=600,retrans=2,_netdev 0 0" >> /etc/fstab
        mount /var/www/html/wp-content
        # The first server seeds wp-content; later ones find it there.
        [ -e /var/www/html/wp-content/index.php ] || cp -rn /var/www/html/wp-content.dist/. /var/www/html/wp-content/
        # Write /var/www/html/wp-config.php here, with your database and the salts from https://api.wordpress.org/secret-key/1.1/salt/.
        systemctl restart apache2
        /usr/local/bin/register-target

    Pin one WordPress <version> so that every server runs the same core. To upgrade, change it and roll out a new launch spec.

  6. Create the group, with a private interface in the servers’ subnet and the bot. The primary interface is public, for package and WordPress downloads:

    ezgh compute autoscaling-groups create wordpress \
      --instance-type <type> --image linux/ezgh-ubuntu:24.04 --ssh-key laptop \
      --subnet sbn_2m9x0a7qpe4k --bot wordpress-servers --ipv4 \
      --user-data user-data.yaml --min 2 --max 10 --cpu-target 60

Then open the load balancer’s address and finish WordPress’s installation once; every server shares its uploads, plugins and themes from then on.

OPcache and shared files

PHP reads every file it runs. With opcache.revalidate_freq=60, each server checks a PHP file for changes at most once a minute instead of on every request, which keeps pages fast on a shared file system. A plugin or theme update shows on every server within a minute. If you deploy code yourself, you can instead set opcache.validate_timestamps=0 and reset OPcache (restart Apache) on every server after each deploy.

Keep hard in the mount options: if the file system is briefly unreachable, PHP waits and carries on, where soft would fail the request.

Scale-in

A server the group removes stops, and its target becomes unused: it takes no traffic. Deregister unused targets when you like with ezgh lb targets deregister.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close