#!/bin/sh
set -eu
cd /var/www/html
PORT="${PORT:-80}"
# Whole-line patterns that take any port, not only the image's 80: a restart that keeps the
# container's filesystem (docker restart, Railway's ON_FAILURE retries) finds the port an
# earlier start wrote and sets it again, where a bare "Listen 80" would turn 8080 into 808080.
sed -i "s/^Listen [0-9][0-9]*$/Listen ${PORT}/" /etc/apache2/ports.conf
sed -i "s/^$//" /etc/apache2/sites-available/000-default.conf
a2dismod mpm_event mpm_worker >/dev/null 2>&1 || true
a2enmod mpm_prefork rewrite >/dev/null 2>&1 || true
mkdir -p cache cache_public logs tmp storage
chmod a+rwX inc cache cache_public logs tmp storage
if [ -f plugins/ffmpeg/ffmpeg.exe ]; then
chmod 0755 plugins/ffmpeg/ffmpeg.exe # installer check only; UNA_FFMPEG_PATH is what runs
fi
parse_mysql_url() {
_url="$1"
_url="${_url#mysql://}"
_url="${_url#mariadb://}"
DB_USER="${_url%%:*}"
_url="${_url#*:}"
DB_PASSWORD="${_url%%@*}"
_url="${_url#*@}"
_hostport="${_url%%/*}"
DB_NAME="${_url#*/}"
DB_NAME="${DB_NAME%%\?*}"
case "$_hostport" in
*:*)
DB_HOST="${_hostport%:*}"
DB_PORT="${_hostport##*:}"
;;
*)
DB_HOST="$_hostport"
DB_PORT="3306"
;;
esac
}
DB_URL="${DATABASE_URL:-${MYSQL_URL:-${MARIADB_PRIVATE_URL:-${MARIADB_URL:-}}}}"
if [ -n "$DB_URL" ]; then
parse_mysql_url "$DB_URL"
else
DB_HOST="${MYSQLHOST:-${MARIADB_HOST:-${MARIADBHOST:-mariadb.railway.internal}}}"
DB_PORT="${MYSQLPORT:-${MARIADB_PORT:-${MARIADBPORT:-3306}}}"
DB_NAME="${MYSQLDATABASE:-${MARIADB_DATABASE:-una}}"
DB_USER="${MYSQLUSER:-${MARIADB_USER:-una}}"
DB_PASSWORD="${MYSQLPASSWORD:-${MARIADB_PASSWORD:-una}}"
fi
export DB_HOST DB_PORT DB_NAME DB_USER DB_PASSWORD
export UNA_DB_HOST="$DB_HOST"
export UNA_DB_PORT="$DB_PORT"
export UNA_DB_NAME="$DB_NAME"
export UNA_DB_USER="$DB_USER"
export UNA_DB_PWD="$DB_PASSWORD"
export UNA_ROOT_DIR=/var/www/html/
export UNA_AUTO_HOSTNAME=1
export UNA_SKIP_INSTALL_FOLDER_CHECK=1
export UNA_SKIP_REDIRECT=1
db_status() {
php -r '
$m = @new mysqli(
getenv("DB_HOST"),
getenv("DB_USER"),
getenv("DB_PASSWORD"),
getenv("DB_NAME"),
(int)(getenv("DB_PORT") ?: 3306)
);
if ($m->connect_error) {
fwrite(STDERR, $m->connect_error . "\n");
exit(1);
}
$r = $m->query("SHOW TABLES LIKE \"sys_options\"");
exit($r && $r->num_rows ? 0 : 2);
'
}
echo "Waiting for MariaDB at ${DB_HOST}:${DB_PORT}..."
i=0
rc=1
set +e
while [ "$i" -lt 60 ]; do
db_status
rc=$?
if [ "$rc" -eq 0 ] || [ "$rc" -eq 2 ]; then
break
fi
i=$((i + 1))
sleep 2
done
set -e
if [ "$rc" -eq 1 ]; then
echo "MariaDB is not reachable" >&2
exit 1
fi
if [ "$rc" -eq 2 ]; then
echo "Installing UNA with cmd.php..."
php install/cmd.php \
-m bx_posts,bx_timeline,bx_contact,bx_developer,bx_antispam,bx_convos,bx_organizations,bx_notifications,bx_ether,bx_accounts,bx_albums,bx_facebook,bx_tricon,bx_oauth,bx_payment,bx_market,bx_groups,bx_intercom,bx_googlecon,bx_linkedin,bx_twitter,bx_mailchimp,bx_forum,bx_acl,bx_events,bx_files,bx_glossary,bx_googletagman,bx_photos,bx_polls,bx_profiler,bx_quoteofday,bx_smtp,bx_shopify,bx_snipcart,bx_se_migration,bx_spaces,bx_stripe_connect,bx_videos,bx_charts,bx_dolcon,bx_fontawesome,bx_channels,bx_attendant,bx_massmailer,bx_directory,bx_plyr,bx_ads,bx_lucid,bx_credits,bx_wiki,bx_tasks,bx_courses,bx_classes,bx_artificer,bx_donations,bx_feedback,bx_help_tours,bx_stream,bx_attendant,bx_analytics,bx_reputation,bx_ru \
--db_host="$DB_HOST" \
--db_port="$DB_PORT" \
--db_name="$DB_NAME" \
--db_user="$DB_USER" \
--db_password="$DB_PASSWORD" \
--server_http_host="${RAILWAY_PUBLIC_DOMAIN:-localhost}" \
--server_php_self=/install/index.php \
--server_doc_root=/var/www/html/ \
--site_title="${UNA_SITE_TITLE:-UNA}" \
--admin_username="${UNA_ADMIN_USERNAME:-admin}" \
--admin_password="${UNA_ADMIN_PASSWORD:-unauna}"
else
echo "UNA already installed; writing header from environment."
if [ ! -f inc/header.inc.php ]; then
cp install/patterns/header.inc.php inc/header.inc.php
chmod 0666 inc/header.inc.php
fi
fi
# API key, origins and client URL for the NEO front end (idempotent, every start).
php /usr/local/lib/una-railway/configure.php
if [ "${UNA_DEMO_SEED:-0}" = "1" ]; then
echo "Seeding demo content..."
# A failed seed must not keep the site down; the log says what went wrong.
php /usr/local/lib/una-railway/seed.php || echo "Demo seed failed (exit $?)" >&2
fi
chown -R www-data:www-data inc cache cache_public logs tmp storage
# UNA's scheduler. periodic/cron.php expects to be started every minute, like the crontab line
# in UNA's install instructions (and scripts/docker-compose/Cron.Dockerfile). The image has no
# cron daemon, so a background loop starts a run at the top of each minute. As under cron, runs
# may overlap, so a long video transcode does not hold up the email and push queues. It starts
# after install and seeding, so it never sees a half-installed site.
# - It runs as www-data, so cache files it writes stay writable by Apache. setpriv keeps the
# environment (UNA_DB_*, UNA_ROOT_DIR...), which su would reset.
# - A CLI run has no request to take a host from, so UNA_HTTP_HOST and HTTPS give it the public
# URL. They are set for the scheduler only: Apache keeps taking the host from each request.
run_cron() {
env UNA_HTTP_HOST="${RAILWAY_PUBLIC_DOMAIN:-localhost}" HTTPS=on \
setpriv --reuid=www-data --regid=www-data --init-groups \
php /var/www/html/periodic/cron.php || echo "UNA cron failed (exit $?)" >&2
}
while true; do
sleep $((60 - $(date +%s) % 60))
jobs >/dev/null # dash keeps a record of every finished run until it is listed
run_cron &
done &
exec "$@"