Files
cloud-apache-container/scripts/entrypoint.sh
T
shadowdaoandClaude Sonnet 5.5 0eea05815a DEV mode: fix MariaDB/memcached install on AlmaLinux 10 and fail visibly
microdnf install MariaDB-server MariaDB-client memcached died in depsolve (MariaDB-client obsoletes the image's AppStream mariadb; mariadb-client-utils requires it), after which the entrypoint carried on: no mysqld, a mysql_creds file for a database that did not exist, and a container that reported healthy. Now (scripts/dev-mode.sh, used by cac and cac-fpm):
- dnf install --allowerasing MariaDB-server MariaDB-client from mariadb.org 11.4, falling back to AlmaLinux mariadb-server; memcached from AppStream; --setopt=skip_if_unavailable so one dead repo does not block the rest; install skipped when already present (docker start)
- initialise/chown the data dir, start mysqld, wait for mysqladmin ping (60 s) instead of sleep 10
- any failure is logged loudly (FATAL banner) and the entrypoint exits non-zero. The HEALTHCHECK only probes Apache, so 'unhealthy' could not express this; a non-zero exit is unmistakable in docker ps/logs. PROD never runs this code.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
2026-11-23 06:03:24 -08:00

117 lines
4.4 KiB
Bash

#!/usr/bin/env bash
# PHPVER does not select anything at runtime: the PHP version is the image tag
# (cac:php83, cac:php85, ...). Remember what the caller asked for so DEV mode
# can warn on a mismatch (dev-mode.sh); the default below is unchanged.
PHPVER_REQUESTED="$PHPVER"
if [ -z "$PHPVER" ]; then
PHPVER="83";
fi
if [ -z "$environment" ]; then
environment="PROD"
fi
adduser -u $uid $user
# Root never follows customer symlinks under /home/$user: see safe-paths.sh.
source /scripts/safe-paths.sh
secure_home
user_mkdir /home/$user/public_html /home/$user/logs/apache /home/$user/logs/php-fpm
mv /var/log/httpd /var/log/httpd.bak
ln -s /home/$user/logs/apache /var/log/httpd
ln -s /home/$user/logs/php-fpm /var/log/php-fpm
rm -f /etc/httpd/conf.d/userdir.conf
docker_network=$(ip addr show |grep eth0 |grep inet |awk -F " " {'print $2'})
echo "RemoteIPInternalProxy $docker_network" >> /etc/httpd/conf.d/remoteip.conf
# /scripts/install-php$PHPVER.sh
source /scripts/detect-memory.sh
echo "Container memory: ${CONTAINER_MEMORY_MB}MB | PHP-FPM pm=${PHP_FPM_PM} max_children=${PHP_FPM_MAX_CHILDREN} | Apache workers=${APACHE_MAX_REQUEST_WORKERS}"
/scripts/create-vhost.sh
/scripts/create-php-config.sh
/scripts/create-apache-mpm-config.sh
if [ -f /etc/httpd/conf.d/ssl.conf ]; then
mv /etc/httpd/conf.d/ssl.conf /etc/httpd/conf.d/ssl.conf.bak
fi
# DEV only: PHP overrides + optional Xdebug must be in place before php-fpm starts.
if [[ $environment == 'DEV' ]]; then
source /scripts/dev-mode.sh
dev_check_phpver "$PHPVER_REQUESTED"
fi
# httpd and php-fpm open their logs under /home/$user/logs as root: last
# fail-closed check immediately before they start (safe-paths.sh).
require_safe_logs /home/$user/logs/apache /home/$user/logs/php-fpm
/usr/sbin/httpd -k start
/usr/sbin/php-fpm -y /etc/php-fpm.conf
# GNU chown -R defaults to -P: symlinks are lchown()ed, never followed.
chown -R $user:$user /home/$user
# Do NOT `chmod -R 755` the docroot here. /home/$user is the customer's
# bind-mounted site directory; a recursive 755 on every start made every file
# (wp-config.php included) world-readable AND executable, and reverted any
# tighter permissions the customer had set. Apache (shared-httpd, or the
# bundled httpd) runs as `apache`, not the customer, so it only needs to
# traverse directories to serve static files. Normalise that and nothing else:
# open the docroot itself, and add o+x to directories that lack it. Regular
# files are never touched, so execute is never added and wp-config.php is
# never loosened. Runs as the customer (safe-paths.sh) so a symlink can only
# reach what the customer already owns.
open_dir_traversal
if [[ $environment == 'DEV' ]]; then
echo "Starting Dev Deployment"
user_mkdir /home/$user/_db_backups
dev_install_db_and_cache "$user"
# A DEV container that could not set up its database/cache/debugger must not
# look healthy (HEALTHCHECK only probes Apache): log loudly and exit non-zero.
dev_report_failures
fi
if [[ $environment == 'PROD' ]]; then
sed -r -i 's/;session.save_path="localhost:11211/session.save_path="memcache:11211/' /etc/php.d/50-memcached.ini
fi
# Set up + load the user crontab, created and read as the customer
# (safe-paths.sh). Root reading/writing/chowning it followed symlinks.
load_user_crontab
/usr/sbin/crond
# Graceful shutdown. bash is PID 1 here, and PID 1 only receives signals it
# has a handler for, so without this trap `docker stop`'s SIGTERM was dropped:
# docker waited the full 10s and SIGKILLed apache and php-fpm mid-request
# (exit 137). Stop apache first (graceful-stop: finish in-flight requests,
# accept no new ones), then php-fpm (QUIT: workers finish their current
# request), then crond. Timeouts sum to < 10s so this completes inside
# docker's default stop timeout.
source /scripts/stop-services.sh
TAIL_PID=""
shutdown() {
trap - TERM INT
echo "entrypoint: received stop signal, shutting down"
stop_service httpd /etc/httpd/run/httpd.pid WINCH 4 '^/usr/sbin/httpd'
stop_service php-fpm /run/php-fpm/php-fpm.pid QUIT 3 'php-fpm: master'
stop_service crond /run/crond.pid TERM 1 '^/usr/sbin/crond'
[ -n "$TAIL_PID" ] && kill "$TAIL_PID" 2>/dev/null
exit 0
}
trap shutdown TERM INT
# Stream apache logs to docker logs. Backgrounded + `wait` (instead of a
# foreground tail) so bash can run the trap as soon as a signal arrives.
tail -f /var/log/httpd/* &
TAIL_PID=$!
wait "$TAIL_PID"
exit 0