#!/bin/bash
#Debian 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

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

echo -e "\n--- configuring development web server"
PHP_VER=7.4
sed 's/display_errors = Off/display_errors = On/' -i /etc/php/$PHP_VER/apache2/php.ini
#~ cat >> /etc/php/$PHP_VER/apache2/conf.d/20-xdebug.ini << EOF
#~ xdebug.mode = debug
#~ xdebug.discover_client_host = 1
#~ xdebug.start_with_request = yes
#~ EOF
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

#cf: https://github.com/xdebug/vscode-php-debug, https://xdebug.org/docs/step_debug
