Create an Extremely High-Performance WordPress Site with FrankenPHP + sidekick

Deploy the application

Refer to the official example repository (View on GitHub)

services:
  wordpress:
    image: wpeverywhere/frankenwp:latest-php8.3
    restart: always
    ports:
      - "8100:80" # HTTP
    environment:
      SERVER_NAME: ${SERVER_NAME:-:80}
      WORDPRESS_DB_HOST: ${DB_HOST:-db}
      WORDPRESS_DB_USER: ${DB_USER:-exampleuser}
      WORDPRESS_DB_PASSWORD: ${DB_PASSWORD:-examplepass}
      WORDPRESS_DB_NAME: ${DB_NAME:-exampledb}
      WORDPRESS_DEBUG: ${WP_DEBUG:-true}
      WORDPRESS_TABLE_PREFIX: ${DB_TABLE_PREFIX:-wp_}
      CACHE_LOC: ${CACHE_LOC:-/var/www/html/wp-content/cache}
      TTL: ${TTL:-80000}
      PURGE_PATH: ${PURGE_PATH:-/__cache/purge}
      PURGE_KEY: ${PURGE_KEY:-}
      BYPASS_HOME: ${BYPASS_HOME:-false}
      BYPASS_PATH_PREFIXES: ${BYPASS_PATH_PREFIXES:-/wp-admin,/wp-content,/wp-includes,/wp-json,/feed}
      CACHE_RESPONSE_CODES: ${CACHE_RESPONSE_CODES:-000}
      CADDY_GLOBAL_OPTIONS: |
        email myemail@sample.com
        auto_https disable_redirects
        debug
      WORDPRESS_CONFIG_EXTRA: |
          define('WP_SITEURL', 'http://localhost:8100');
          define('WP_HOME', 'http://localhost:8100');
volumes:
#  - ./wp-content:/var/www/html/wp-content

depends_on:
  - db
tty: true

db:
image: mariadb:latest
restart: always
ports:
- ${LOCAL_DB_PORT:-3311}:3306
environment:
MYSQL_DATABASE: ${DB_NAME:-exampledb}
MYSQL_USER: ${DB_USER:-exampleuser}
MYSQL_PASSWORD: ${DB_PASSWORD:-examplepass}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-examplepass}
volumes:
- dbwp:/var/lib/mysql

phpmyadmin:
image: phpmyadmin/phpmyadmin
restart: always
ports:
- ${LOCAL_PHPMYADMIN_PORT:-8086}:80
environment:
PMA_HOST: db
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-examplepass}
depends_on:
- db

volumes:
dbwp:

Generally speaking, you can run this compose file directly, but this is only an example. Using it directly as a production environment will have security issues. For example, its default MySQL username and password are both weak passwords. What's more, this compose file also maps the MySQL port outside the container. Although the default port is no longer 3306, there is still a significant security risk.

For this reason, we can create a .env file in the current directory for configuration, so that many parameters can be customized during deployment.

BYPASS_PATH_PREFIX=/wp-admin,/wp-includes,/wp-content
DB_NAME=mysitename
DB_USER=mysitename
DB_PASSWORD=soineoinxAsx
DB_PREFIX=mywp_

If everything is ready, you can deploy the application with this command

docker compose up -d

It will automatically pull the image and start the container. At this point, you only need to access server IP:port, for example 123.1.1.1:8100

If you only want to run this one website, you can modify the first section of the compose file like this to save the reverse proxy step

ports:
      - "80:80"   # HTTP

After modifying it, it is also recommended to modify the content in WORDPRESS_CONFIG_EXTRA

Of course, since FrankenPHP uses Caddy as its web server, you can use its automatic HTTPS feature

First, you need to map both ports 80 and 443

ports:
      - "80:80"   # HTTP
      - "443:443"  # HTTPS

And add a new line to the .env file with the following content

SERVER_NAME=你的域名

After that, be sure to modify the content in WORDPRESS_CONFIG_EXTRA

This site is currently using FrankenPHP as its backend. Testing has shown it to be very stable, and performance has indeed improved

Build the image yourself

If you want to further optimize performance, you may need to use Redis, Mencached, etc. In this case, you need to install the corresponding extensions for PHP. However, installing them by exec-ing directly into the container is not convenient for persistence, so it is recommended to build your own Docker image.

Building your own image requires some foundation, so this article will not mention some well-known details.

First, if your goal is to create a new site rather than switch an existing site to a different backend, then you can use the official DockerFile with some modifications. If you are switching an existing site to a different backend, it is recommended to refer to my DockerFile.

First, here is the DockerFile I modified myself. Since I do not need the database and phpmyadmin (they have already been deployed), I commented these out.

ARG WORDPRESS_VERSION=latest
ARG PHP_VERSION=8.3
ARG USER=1000

FROM dunglas/frankenphp:latest-builder-php${PHP_VERSION} AS builder

Copy xcaddy in the builder image

COPY --from=caddy:builder /usr/bin/xcaddy /usr/bin/xcaddy

CGO must be enabled to build FrankenPHP

ENV CGO_ENABLED=1 XCADDY_SETCAP=1 XCADDY_GO_BUILD_FLAGS='-ldflags="-w -s" -trimpath'

COPY ./sidekick/middleware/cache ./cache

RUN xcaddy build
--output /usr/local/bin/frankenphp
--with github.com/dunglas/frankenphp=./
--with github.com/dunglas/frankenphp/caddy=./caddy/
--with github.com/dunglas/caddy-cbrotli
# Add extra Caddy modules here
--with github.com/stephenmiracle/frankenwp/sidekick/middleware/cache=./cache

FROM wordpress:$WORDPRESS_VERSION AS wp
FROM dunglas/frankenphp:latest-php${PHP_VERSION} AS base

LABEL org.opencontainers.image.title=FrankenWP
LABEL org.opencontainers.image.description="Optimized WordPress containers to run everywhere. Built with FrankenPHP & Caddy."
LABEL org.opencontainers.image.url=https://wpeverywhere.com
LABEL org.opencontainers.image.source=https://github.com/StephenMiracle/frankenwp
LABEL org.opencontainers.image.licenses=MIT
LABEL org.opencontainers.image.vendor="Stephen Miracle"

Replace the official binary by the one contained your custom modules

COPY --from=builder /usr/local/bin/frankenphp /usr/local/bin/frankenphp
ENV WP_DEBUG=${DEBUG:+1}
ENV FORCE_HTTPS=0
ENV PHP_INI_SCAN_DIR=$PHP_INI_DIR/conf.d

RUN apt-get update && apt-get install -y --no-install-recommends
ca-certificates
ghostscript
curl
libonig-dev
libxml2-dev
libcurl4-openssl-dev
libssl-dev
libzip-dev
unzip
git
libjpeg-dev
libwebp-dev
libzip-dev
libmemcached-dev
zlib1g-dev

install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions)

RUN install-php-extensions
bcmath
redis
fileinfo
exif
gd
intl
mysqli
zip
curl
# See https://github.com/Imagick/imagick/issues/640#issuecomment-2077206945
imagick/imagick@master
opcache

RUN cp $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini
COPY php.ini $PHP_INI_DIR/conf.d/wp.ini

#COPY --from=wp /usr/src/wordpress /usr/src/wordpress
COPY --from=wp /usr/local/etc/php/conf.d /usr/local/etc/php/conf.d/
COPY --from=wp /usr/local/bin/docker-entrypoint.sh /usr/local/bin/

set recommended PHP.ini settings

see https://secure.php.net/manual/en/opcache.installation.php

RUN set -eux;
{
echo 'opcache.memory_consumption=128';
echo 'opcache.interned_strings_buffer=8';
echo 'opcache.max_accelerated_files=4000';
echo 'opcache.revalidate_freq=2';
} > $PHP_INI_DIR/conf.d/opcache-recommended.ini

https://wordpress.org/support/article/editing-wp-config-php/#configure-error-logging

RUN {
# https://www.php.net/manual/en/errorfunc.constants.php
# https://github.com/docker-library/wordpress/issues/420#issuecomment-517839670
echo 'error_reporting = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING | E_RECOVERABLE_ERROR';
echo 'display_errors = Off';
echo 'display_startup_errors = Off';
echo 'log_errors = On';
echo 'error_log = /dev/stderr';
echo 'log_errors_max_len = 1024';
echo 'ignore_repeated_errors = On';
echo 'ignore_repeated_source = Off';
echo 'html_errors = Off';
} > $PHP_INI_DIR/conf.d/error-logging.ini

WORKDIR /var/www/html

#VOLUME /var/www/html/wp-content

COPY wp-content/mu-plugins /var/www/html/wp-content/mu-plugins
RUN mkdir /var/www/html/wp-content/cache

RUN sed -i
-e 's/[ "$1" = '''php-fpm''' ]/[[ "$1" == frankenphp* ]]/g'
-e 's/php-fpm/frankenphp/g'
/usr/local/bin/docker-entrypoint.sh

Add $_SERVER['ssl'] = true; when env USE_SSL = true is set to the wp-config.php file here: /usr/local/bin/wp-config-docker.php

#RUN sed -i 's/<?php/<?php if (!!getenv("FORCE_HTTPS")) { $_SERVER["HTTPS"] = "on"; } define( "FS_METHOD", "direct" ); set_time_limit(300); /g' /usr/src/wordpress/wp-config-docker.php

Adding WordPress CLI

RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar &&
chmod +x wp-cli.phar &&
mv wp-cli.phar /usr/local/bin/wp

COPY Caddyfile /etc/caddy/Caddyfile

Caddy requires an additional capability to bind to port 80 and 443

RUN useradd -D ${USER} &&
setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/frankenphp

Caddy requires write access to /data/caddy and /config/caddy

RUN chown -R ${USER}:${USER} /data/caddy &&
chown -R ${USER}:${USER} /config/caddy &&
chown -R ${USER}:${USER} /var/www/html &&
#chown -R ${USER}:${USER} /usr/src/wordpress &&
chown -R ${USER}:${USER} /usr/local/bin/docker-entrypoint.sh

USER $USER

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["frankenphp", "run", "--config", "/etc/caddy/Caddyfile"]

If you only want to add a few extensions, you can directly modify the content under RUN install-php-extensions. If you want more customization, you can refer to mine. I will not explain too much here.

Article repost policy

Request authorization before reposting and retain the authorization code, original URL, and go/affiliate links.

Original URL: https://www.geekcorelab.cn/en-US/post/create-a-high-performance-wordpress-site-using-frankenphp