Docker Run FrankenPHP, WordPress and MySQL

1 min read

Setup FrankenPHP

What’s FrankenPHP

FrankenPHP is a modern application server for PHP built on top of the Caddy web server.

FrankenPHP gives superpowers to your PHP apps thanks to its stunning features: Early Hints, worker mode, real-time capabilities, automatic HTTPS, HTTP/2, and HTTP/3 support.


Install Docker in Ubuntu

apt install -y ca-certificates curl
install -m 0755 -d /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

apt update && apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Setup FrankenPHP to run WordPress CSM and MySQL

We’ll setup WordPress container with local volume.

Create working directory

mkdir wordpress/{caddy,custom,db,web}

Caddyfile config

cd wordpress
mkdir caddy/{data,config,log}

vim caddy/Caddyfile

{
  frankenphp
  # https://caddyserver.com/docs/caddyfile/directives#sorting-algorithm
  order php_server before file_server
}

blackonsole.org {
  root * /web/blackonsole_org/public_html
  encode zstd br gzip
  php_server

	@static {
		file
		path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.avif *.webp
	}
	header @static Cache-Control max-age=5184000

  log {
    output file /var/log/caddy/access.log {
      roll_size 1gb
      roll_keep 3
      roll_keep_for 720h
    }
  }

}

Setup Dockerfile to custom FrankenPHP

# custom php ini
tee wordpress/custom/web.ini >/dev/null <<EOF
file_uploads = On
memory_limit = 512M
upload_max_filesize = 512M
post_max_size = 521M
max_execution_time = 600
expose_php = Off
EOF

# custom Dockerfile
vim wordpress/custom/Dockerfile

# content of Dockerfile
FROM dunglas/frankenphp:latest-php8.2

# install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions)
RUN install-php-extensions \
    bcmath \
    exif \
    gd \
    intl \
    mysqli \
    zip \
    imagick \
    opcache

# Enable PHP production settings
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY web.ini $PHP_INI_DIR/conf.d/

USER www-data

CMD ["frankenphp", "run", "--config", "/etc/caddy/Caddyfile"]

Setup docker-compose.yml

# create docker-compose.yml file
cd wordpress
vim docker-compose.yml

# content of docker-compose.yml
---
services:
  fatwakata_com:
    container_name: blackonsole_org
    build: ./custom
    restart: always
    ports:
      - 443:443
    volumes:
      - ./web/blackonsole_org/public_html:/web/blackonsole_org/public_html
      - ./caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./caddy/log:/var/log/caddy

  db:
    container_name: db
    image: mysql
    restart: always
    environment:
      MYSQL_DATABASE: db_name
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_passwd
      MYSQL_ROOT_PASSWORD: root_passwd
    volumes:
      - ./db:/var/lib/mysql

Download WordPress

mkdir web/domain_name
wget https://wordpress.org/latest.zip
unzip latest.zip
rm -fv latest.zip
mv wordpress web/blackonsole_org/public_html

Change permission to web user

cd wordpress
chown -R www-data:www-data caddy web

Run FrankenPHP with WordPress Container

cd wordpress
docker compose up -d

Installing WordPress

Open your browser and installing WordPress via browser.

References

  • https://github.com/dunglas/frankenphp-wordpress
  • https://frankenphp.dev/

Leave a Reply

Your email address will not be published. Required fields are marked *