Skip to content

Instantly share code, notes, and snippets.

@1000heads-luke
Last active January 28, 2026 03:19
Show Gist options
  • Select an option

  • Save 1000heads-luke/227bad0692486bb91e5db9f36bdf260b to your computer and use it in GitHub Desktop.

Select an option

Save 1000heads-luke/227bad0692486bb91e5db9f36bdf260b to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
# Interactive AL2023 LAMP installer (Apache + PHP + optional MariaDB + optional phpMyAdmin)
# Based on AWS AL2023 guide:
# https://docs.aws.amazon.com/linux/al2023/ug/ec2-lamp-amazon-linux-2023.html
log() { echo -e "\n==> $*\n"; }
warn() { echo -e "\n[WARN] $*\n" >&2; }
need_sudo() {
if ! command -v sudo >/dev/null 2>&1; then
echo "sudo is required but not found." >&2
exit 1
fi
if ! sudo -n true >/dev/null 2>&1; then
echo "This script needs sudo privileges. Run as a sudo-capable user (e.g. ec2-user)." >&2
exit 1
fi
}
is_installed_rpm() {
local pkg="$1"
rpm -q "$pkg" >/dev/null 2>&1
}
ensure_enabled_and_started() {
local svc="$1"
if systemctl list-unit-files | grep -q "^${svc}\.service"; then
sudo systemctl enable "$svc" >/dev/null 2>&1 || true
sudo systemctl start "$svc" >/dev/null 2>&1 || true
else
warn "Service '$svc' not found (skipping enable/start)."
fi
}
prompt_yn() {
local prompt="$1"
local default_no="${2:-1}" # 1 => default No, 0 => default Yes
local ans=""
while true; do
if [[ "$default_no" -eq 1 ]]; then
read -r -p "$prompt [y/N]: " ans
ans="${ans:-N}"
else
read -r -p "$prompt [Y/n]: " ans
ans="${ans:-Y}"
fi
case "${ans,,}" in
y|yes) return 0 ;;
n|no) return 1 ;;
*) echo "Please answer y or n." ;;
esac
done
}
install_apache_php() {
log "Updating packages (dnf upgrade -y)"
sudo dnf upgrade -y
log "Installing Apache + PHP packages"
# Per AWS doc: httpd wget php-fpm php-mysqli php-json php php-devel
sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel
log "Enabling + starting Apache (httpd) and PHP-FPM (php-fpm)"
ensure_enabled_and_started httpd
ensure_enabled_and_started php-fpm
}
setup_web_permissions() {
local user
user="$(id -un)"
log "Configuring /var/www permissions (AWS tutorial style)"
if id -nG "$user" | tr ' ' '\n' | grep -qx apache; then
echo "User '$user' is already in the apache group."
else
sudo usermod -a -G apache "$user"
echo "NOTE: Log out and log back in for group membership to take effect."
fi
sudo chown -R "$user:apache" /var/www
sudo chmod 2775 /var/www
sudo find /var/www -type d -exec chmod 2775 {} \;
sudo find /var/www -type f -exec chmod 0664 {} \;
}
install_or_enable_mariadb() {
if is_installed_rpm mariadb105-server; then
log "MariaDB package already installed."
else
log "Installing MariaDB server package (mariadb105-server)"
sudo dnf install -y mariadb105-server
fi
log "Enabling + starting MariaDB (mariadb)"
ensure_enabled_and_started mariadb
if prompt_yn "Run mysql_secure_installation to harden MariaDB now?" 1; then
log "Running mysql_secure_installation (interactive)"
sudo mysql_secure_installation
else
log "Skipping MariaDB hardening for now."
fi
}
install_phpmyadmin() {
log "Installing phpMyAdmin dependencies (php-mbstring php-xml)"
sudo dnf install -y php-mbstring php-xml
log "Restarting Apache and PHP-FPM"
sudo systemctl restart httpd || true
sudo systemctl restart php-fpm || true
log "Downloading and installing phpMyAdmin into /var/www/html/phpMyAdmin"
cd /var/www/html
local tarball="phpMyAdmin-latest-all-languages.tar.gz"
wget -q "https://www.phpmyadmin.net/downloads/${tarball}" -O "${tarball}"
sudo mkdir -p phpMyAdmin
sudo tar -xzf "${tarball}" -C phpMyAdmin --strip-components 1
rm -f "${tarball}"
log "phpMyAdmin installed."
echo "Path: /var/www/html/phpMyAdmin"
echo "Tip: copy phpMyAdmin/config.sample.inc.php to phpMyAdmin/config.inc.php and configure secrets/blowfish."
}
main() {
need_sudo
echo "Interactive LAMP setup for Amazon Linux 2023"
echo " - Apache + PHP will be installed/enabled/started"
echo " - MariaDB is optional (prompted)"
echo " - phpMyAdmin is optional (default No)"
echo
install_apache_php
setup_web_permissions
if prompt_yn "Install/configure MariaDB (mariadb105-server)?" 1; then
install_or_enable_mariadb
else
log "Skipping MariaDB installation."
fi
if prompt_yn "Install phpMyAdmin?" 1; then
install_phpmyadmin
else
log "Skipping phpMyAdmin installation."
fi
log "Done. Service status summary:"
echo "Apache (httpd): $(sudo systemctl is-enabled httpd 2>/dev/null || echo 'unknown') / $(sudo systemctl is-active httpd 2>/dev/null || echo 'unknown')"
echo "PHP-FPM (php-fpm):$(sudo systemctl is-enabled php-fpm 2>/dev/null || echo 'unknown') / $(sudo systemctl is-active php-fpm 2>/dev/null || echo 'unknown')"
if systemctl list-unit-files | grep -q "^mariadb\.service"; then
echo "MariaDB (mariadb):$(sudo systemctl is-enabled mariadb 2>/dev/null || echo 'unknown') / $(sudo systemctl is-active mariadb 2>/dev/null || echo 'unknown')"
fi
echo
echo "NOTE: If you were just added to the 'apache' group, log out + log back in for it to apply."
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment