Skip to content

Commit

Permalink
allow using nginx in local dev docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
diosmosis committed Oct 4, 2023
1 parent 28fc4a3 commit aa79792
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 155 deletions.
195 changes: 40 additions & 155 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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" <<EOF
<?php
define('DB_NAME', '$$WP_DB_NAME');
define('DB_USER', 'root');
define('DB_PASSWORD', 'pass');
define('DB_HOST', getenv('WP_DB_HOST'));
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define( 'WP_DEBUG', false );
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );
define( 'FS_CHMOD_DIR', ( 0777 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
define( 'FS_METHOD', 'direct' );
define( 'MATOMO_ANALYTICS_FILE', __DIR__ . '/wp-content/plugins/matomo/matomo.php' );
\$$table_prefix = 'wp_';
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
EOF
echo "setup wp-config.php!"
fi
# link matomo for wordpress volume as wordpress plugin
if [ ! -d "/var/www/html/$$WORDPRESS_VERSION/wp-content/plugins/matomo" ]; then
ln -s /var/www/html/matomo-for-wordpress "/var/www/html/$$WORDPRESS_VERSION/wp-content/plugins/matomo"
fi
# add index.php file listing available installs to root /var/www/html
if [ ! -f "/var/www/html/index.php" ]; then
cat > "/var/www/html/index.php" <<EOF
<html lang="en">
<head>
<style>
body { padding: 0; margin: 0; font-family: sans-serif; background-color: #f8f8f8; height: calc(100%); display: flex; flex-direction: column; }
header { background-color: lightskyblue; padding: 10px 0; }
h1 { text-align: center; margin: 0; color: #fafafa; }
.content { width: 800px; margin: 20px auto; background-color: white; padding: 20px; flex: 1; overflow-y: auto; }
</style>
</head>
<body>
<header><h1>Available Wordpress Installs</h1></header>
<div class="content">
<ul>
<?php
foreach (scandir(__DIR__) as \$$folder) {
if (preg_match('/^\d+\.\d+\.\d+\$$/', \$$folder) && is_dir(\$$folder)) {
?>
<li><a href="<?php echo \$$folder; ?>/"><?php echo \$$folder; ?></a></li>
<?php
}
}
?>
</ul>
</div>
</body>
</html>
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:
Expand All @@ -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:
Expand Down
158 changes: 158 additions & 0 deletions scripts/local-dev-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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" <<EOF
<?php
define('DB_NAME', '$WP_DB_NAME');
define('DB_USER', 'root');
define('DB_PASSWORD', 'pass');
define('DB_HOST', getenv('WP_DB_HOST'));
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define( 'WP_DEBUG', false );
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );
define( 'FS_CHMOD_DIR', ( 0777 & ~ umask() ) );
define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
define( 'FS_METHOD', 'direct' );
define( 'MATOMO_ANALYTICS_FILE', __DIR__ . '/wp-content/plugins/matomo/matomo.php' );
\$table_prefix = 'wp_';
/* That's all, stop editing! Happy publishing. */
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ . '/' );
}
/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
EOF

echo "setup wp-config.php!"
fi

# link matomo for wordpress volume as wordpress plugin
if [ ! -d "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/matomo" ]; then
ln -s /var/www/html/matomo-for-wordpress "/var/www/html/$WORDPRESS_VERSION/wp-content/plugins/matomo"
fi

# add index.php file listing available installs to root /var/www/html
if [ ! -f "/var/www/html/index.php" ]; then
cat > "/var/www/html/index.php" <<EOF
<html lang="en">
<head>
<style>
body { padding: 0; margin: 0; font-family: sans-serif; background-color: #f8f8f8; height: calc(100%); display: flex; flex-direction: column; }
header { background-color: lightskyblue; padding: 10px 0; }
h1 { text-align: center; margin: 0; color: #fafafa; }
.content { width: 800px; margin: 20px auto; background-color: white; padding: 20px; flex: 1; overflow-y: auto; }
</style>
</head>
<body>
<header><h1>Available Wordpress Installs</h1></header>
<div class="content">
<ul>
<?php
foreach (scandir(__DIR__) as \$folder) {
if (preg_match('/^\d+\.\d+\.\d+\$/', \$folder) && is_dir(\$folder)) {
?>
<li><a href="<?php echo \$folder; ?>/"><?php echo \$folder; ?></a></li>
<?php
}
}
?>
</ul>
</div>
</body>
</html>
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
Loading

0 comments on commit aa79792

Please sign in to comment.