#!/usr/bin/env bash
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y --no-install-recommends \
apache2 \
mariadb-server \
php \
libapache2-mod-php \
php-cli \
php-curl \
php-gd \
php-intl \
php-mbstring \
php-mysql \
php-xml \
php-zip \
unzip \
curl \
ca-certificates
a2enmod rewrite headers expires
echo "ServerName desktop-support.test" >/etc/apache2/conf-available/servername.conf
a2enconf servername
cat >/etc/apache2/sites-available/desktop-support.conf <<'APACHE'
ServerName desktop-support.test
DocumentRoot /vagrant/public
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
ErrorLog ${APACHE_LOG_DIR}/desktop-support-error.log
CustomLog ${APACHE_LOG_DIR}/desktop-support-access.log combined
APACHE
a2dissite 000-default.conf
a2ensite desktop-support.conf
cat >/etc/php/development-overrides.ini <<'PHP'
display_errors = On
display_startup_errors = On
error_reporting = E_ALL
date.timezone = America/New_York
PHP
PHP_VERSION="$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')"
ln -sf /etc/php/development-overrides.ini "/etc/php/${PHP_VERSION}/apache2/conf.d/99-development.ini"
ln -sf /etc/php/development-overrides.ini "/etc/php/${PHP_VERSION}/cli/conf.d/99-development.ini"
systemctl enable --now mariadb apache2
install -d -m 0750 -o root -g www-data /etc/desktop-resume
if [ -f /vagrant/.env ]; then
RESEND_KEY="$(sed -n 's/^RESEND_KEY=//p' /vagrant/.env | head -n 1 | tr -d '\r')"
if [ -n "$RESEND_KEY" ]; then
printf 'RESEND_API_KEY=%s\nRESEND_FROM=%s\n' "$RESEND_KEY" 'Desktop Support Portfolio ' >/etc/desktop-resume/contact.env
chown root:www-data /etc/desktop-resume/contact.env
chmod 0640 /etc/desktop-resume/contact.env
fi
fi
mariadb <<'SQL'
CREATE DATABASE IF NOT EXISTS desktop_support
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS 'desktop_support'@'localhost' IDENTIFIED BY 'development_only';
GRANT ALL PRIVILEGES ON desktop_support.* TO 'desktop_support'@'localhost';
FLUSH PRIVILEGES;
SQL
apache2ctl configtest
systemctl restart apache2
echo "LAMP provisioning complete: http://localhost:8080"