diff --git a/docker-compose.yml b/docker-compose.yml index 19f57dc2c..57b7a6103 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,6 +6,7 @@ # - PORT (the port to expose wordpress on, defaults to 3000) services: + # basic apache service wordpress: image: "php:${PHP_VERSION:-8.1}-apache" volumes: @@ -15,171 +16,53 @@ services: - "./docker/php-${PHP_VERSION:-8.1}/conf:/usr/local/etc/php/conf.d" - .:/var/www/html/matomo-for-wordpress - /var/www/html/matomo-for-wordpress/app/tmp + - ./scripts/local-dev-entrypoint.sh:/usr/src/entrypoint.sh ports: - "${PORT:-3000}:80" environment: WP_DB_HOST: "${BACKEND:-mariadb}" - entrypoint: - - bash - - -c - - | - set -e - - cd /var/www/html - - LATEST_WORDPRESS_VERSION=6.3.1 # can't use the github API, too easy to get rate limited - - # install PHP extensions needed - for extension in mysqli pdo pdo_mysql; do - if [ ! -f /usr/local/lib/php/extensions/*/$$extension.so ]; then - docker-php-ext-install $$extension - fi - done - - # install wordpress if not present - WORDPRESS_VERSION=$${WORDPRESS_VERSION:-$$LATEST_WORDPRESS_VERSION} - if [ ! -d "/var/www/html/$$WORDPRESS_VERSION" ]; then - WORDPRESS_URL="https://wordpress.org/wordpress-$$WORDPRESS_VERSION.tar.gz" - echo "installing wordpress $$WORDPRESS_VERSION from $$WORDPRESS_URL..." - - curl -O "$$WORDPRESS_URL" - tar -xvf "wordpress-$$WORDPRESS_VERSION.tar.gz" - mv wordpress "$$WORDPRESS_VERSION" - - echo "wordpress installed!" - else - echo "wordpress $$WORDPRESS_VERSION already installed." - fi - - echo "waiting for database..." - sleep 5 # wait for database - echo "done." - - # create database if it does not already exist - WP_DB_NAME=$$(echo "wp_matomo_$$WORDPRESS_VERSION" | sed 's/\./_/g') - php -r "\$$pdo = new PDO('mysql:host=$$WP_DB_HOST', 'root', 'pass'); - \$$pdo->exec('CREATE DATABASE IF NOT EXISTS \`$$WP_DB_NAME\`');\ - \$$pdo->exec('GRANT ALL PRIVILEGES ON $$WP_DB_NAME.* TO \'root\'@\'%\' IDENTIFIED BY \'pass\'');" - - # setup wordpress config if not done so - if [ ! -f "/var/www/html/$$WORDPRESS_VERSION/wp-config.php" ]; then - cat > "/var/www/html/$$WORDPRESS_VERSION/wp-config.php" < "/var/www/html/index.php" < - - - - -

Available Wordpress Installs

-
-
    - -
  • - -
-
- - - EOF - fi - - # download WP_PLUGINS plugins if not present - if [ ! -f "/var/www/html/wp-cli.phar" ]; then - curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /var/www/html/wp-cli.phar - fi - chmod +x /var/www/html/wp-cli.phar - - for PLUGIN_VERSION in $WP_PLUGINS - do - PLUGIN_VERSION_ARRAY=($${PLUGIN_VERSION//:/ }) - PLUGIN=$${PLUGIN_VERSION_ARRAY[0]} - VERSION=$${PLUGIN_VERSION_ARRAY[1]} - - if [ "$$PLUGIN" = "matomo" ]; then - echo "skipping matomo plugin install" - continue - fi - - if [[ ! -z "$$VERSION" ]]; then - VERSION_ARGUMENT="--version=$$VERSION" - fi - - echo "installing plugin $$PLUGIN $$VERSION_ARGUMENT" - /var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$$WORDPRESS_VERSION plugin install --activate $$VERSION_ARGUMENT $$PLUGIN || true - done - - # make sure the files can be edited outside of docker (for easier debugging) - # TODO: file permissions becoming a pain, shouldn't have to deal with this for dev env. this works for now though. - find "/var/www/html/$$WORDPRESS_VERSION" -path "/var/www/html/$$WORDPRESS_VERSION/wp-content/plugins/matomo" -prune -o -exec chown "${UID:-1000}:${GID:-1000}" {} + - find "/var/www/html/$$WORDPRESS_VERSION" -path "/var/www/html/$$WORDPRESS_VERSION/wp-content/plugins/matomo" -prune -o -exec chmod 0777 {} + - chmod -R 0777 "/var/www/html/$$WORDPRESS_VERSION/wp-content/plugins/matomo/app/tmp" "/var/www/html/index.php" "/usr/local/etc/php/conf.d" + entrypoint: /usr/src/entrypoint.sh + depends_on: + - "${BACKEND:-mariadb}" + deploy: + restart_policy: + condition: on-failure - # ordinarily the command is passed to the entrypoint as an argument, but this doesn't seem to work when overriding the entrypoint - # through docker-compose - apache2-foreground + # nginx service + fpm: + image: "php:${PHP_VERSION:-8.1}-fpm" + volumes: + - ./docker/wordpress:/var/www/html + - "./docker/php-${PHP_VERSION:-8.1}/php:/usr/src/php" + - "./docker/php-${PHP_VERSION:-8.1}/extensions:/usr/local/lib/php/extensions" + - "./docker/php-${PHP_VERSION:-8.1}/conf:/usr/local/etc/php/conf.d" + - .:/var/www/html/matomo-for-wordpress + - /var/www/html/matomo-for-wordpress/app/tmp + - ./scripts/local-dev-entrypoint.sh:/usr/src/entrypoint.sh + environment: + WP_DB_HOST: "${BACKEND:-mariadb}" + entrypoint: /usr/src/entrypoint.sh depends_on: - "${BACKEND:-mariadb}" deploy: restart_policy: condition: on-failure + nginx: + image: nginx + volumes: + - ./scripts/local-dev-nginx.conf:/etc/nginx/conf.d/default.conf + - ./docker/wordpress:/var/www/html + - .:/var/www/html/matomo-for-wordpress + - /var/www/html/matomo-for-wordpress/app/tmp + depends_on: + - fpm + ports: + - "${PORT:-3000}:80" + deploy: + restart_policy: + condition: on-failure + + # service for running wp-cli wp: image: "php:${PHP_VERSION:-8.1}-apache" volumes: @@ -197,6 +80,8 @@ services: entrypoint: "/var/www/html/wp-cli.phar --path=/var/www/html/${WORDPRESS_VERSION:-6.3.1}" depends_on: - "${BACKEND:-mariadb}" + + # database service definitions mariadb: image: mariadb environment: diff --git a/scripts/local-dev-entrypoint.sh b/scripts/local-dev-entrypoint.sh new file mode 100755 index 000000000..36dc1d0db --- /dev/null +++ b/scripts/local-dev-entrypoint.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash + +set -e + +cd /var/www/html + +LATEST_WORDPRESS_VERSION=6.3.1 # can't use the github API, too easy to get rate limited + +# install PHP extensions needed +for extension in mysqli pdo pdo_mysql; do + if [ ! -f /usr/local/lib/php/extensions/*/$extension.so ]; then + docker-php-ext-install $extension + fi +done + +# install wordpress if not present +WORDPRESS_VERSION=${WORDPRESS_VERSION:-$LATEST_WORDPRESS_VERSION} +if [ ! -d "/var/www/html/$WORDPRESS_VERSION" ]; then + WORDPRESS_URL="https://wordpress.org/wordpress-$WORDPRESS_VERSION.tar.gz" + echo "installing wordpress $WORDPRESS_VERSION from $WORDPRESS_URL..." + + curl -O "$WORDPRESS_URL" + tar -xvf "wordpress-$WORDPRESS_VERSION.tar.gz" + mv wordpress "$WORDPRESS_VERSION" + + echo "wordpress installed!" +else + echo "wordpress $WORDPRESS_VERSION already installed." +fi + +echo "waiting for database..." +sleep 5 # wait for database +echo "done." + +# create database if it does not already exist +WP_DB_NAME=$(echo "wp_matomo_$WORDPRESS_VERSION" | sed 's/\./_/g') +php -r "\$pdo = new PDO('mysql:host=$WP_DB_HOST', 'root', 'pass'); +\$pdo->exec('CREATE DATABASE IF NOT EXISTS \`$WP_DB_NAME\`');\ +\$pdo->exec('GRANT ALL PRIVILEGES ON $WP_DB_NAME.* TO \'root\'@\'%\' IDENTIFIED BY \'pass\'');" + +# setup wordpress config if not done so +if [ ! -f "/var/www/html/$WORDPRESS_VERSION/wp-config.php" ]; then + cat > "/var/www/html/$WORDPRESS_VERSION/wp-config.php" < "/var/www/html/index.php" < + + + + +

Available Wordpress Installs

+
+
    + +
  • + +
+
+ + +EOF +fi + +# download WP_PLUGINS plugins if not present +if [ ! -f "/var/www/html/wp-cli.phar" ]; then + curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /var/www/html/wp-cli.phar +fi +chmod +x /var/www/html/wp-cli.phar + +for PLUGIN_VERSION in $WP_PLUGINS +do + PLUGIN_VERSION_ARRAY=(${PLUGIN_VERSION//:/ }) + PLUGIN=${PLUGIN_VERSION_ARRAY[0]} + VERSION=${PLUGIN_VERSION_ARRAY[1]} + + if [ "$PLUGIN" = "matomo" ]; then + echo "skipping matomo plugin install" + continue + fi + + if [[ ! -z "$VERSION" ]]; then + VERSION_ARGUMENT="--version=$VERSION" + fi + + echo "installing plugin $PLUGIN $VERSION_ARGUMENT" + /var/www/html/wp-cli.phar --allow-root --path=/var/www/html/$WORDPRESS_VERSION plugin install --activate $VERSION_ARGUMENT $PLUGIN || true +done + +# make sure the files can be edited outside of docker (for easier debugging) +# TODO: file permissions becoming a pain, shouldn't have to deal with this for dev env. this works for now though. +find "/var/www/html/$WORDPRESS_VERSION" -path "/var/www/html/$WORDPRESS_VERSION" -prune -o -exec chown "${UID:-1000}:${GID:-1000}" {} + +find "/var/www/html/$WORDPRESS_VERSION" -path "/var/www/html/$WORDPRESS_VERSION" -prune -o -exec chmod 0777 {} + +chmod -R 0777 "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/matomo/app/tmp" "/var/www/html/index.php" "/usr/local/etc/php/conf.d" + +echo "$@" + +if ! which apache2-foreground &> /dev/null; then + php-fpm "$@" +else + apache2-foreground "$@" +fi diff --git a/scripts/local-dev-nginx.conf b/scripts/local-dev-nginx.conf new file mode 100644 index 000000000..9b28e23da --- /dev/null +++ b/scripts/local-dev-nginx.conf @@ -0,0 +1,26 @@ +server { + listen 80; + server_name localhost; + root /var/www/html; + + index index.php; + + location / { + try_files $uri $uri/ /index.php?$args; + } + + location ~ [^/]\.php(/|$) { + fastcgi_split_path_info ^(.+?\.php)(/.*)$; + if (!-f $document_root$fastcgi_script_name) { + return 404; + } + + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; + + fastcgi_pass fpm:9000; + fastcgi_index index.php; + } +}