#!/bin/bash
#Ubuntu Web Server

ROOTPW="P@ssw0rd"
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
ln -sf bash /bin/sh

echo -e "\n--- disabling password authentication & ubuntu user"
sed 's/#\(PasswordAuthentication\) yes/\1 no/' -i /etc/ssh/sshd_config
systemctl restart sshd
userdel -r ubuntu

echo -e "\n--- installing packages"
apt update && apt-upgrade -y
apt install -y mariadb-server php php-mysql libapache2-mod-php php-sqlite3

echo -e "\n--- configuring development web server"
sed 's/display_errors = Off/display_errors = On/' -i /etc/php/8.1/apache2/php.ini
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
systemctl restart apache2

echo -e "\n--- enabling HTTPS"
a2enmod ssl
a2ensite default-ssl
systemctl restart apache2

echo -e "\n--- enabling remote database access"
sed 's/^bind-address.*/bind-address = 0.0.0.0/' -i /etc/mysql/mariadb.conf.d/50-server.cnf
systemctl restart mariadb
cat > init-root.sql << EOF
create user 'root'@'%' identified by "$ROOTPW";
grant all privileges on *.* to 'root'@'%' with grant option;
EOF
mysql < init-root.sql
rm -f init-root.sql
