#!/bin/bash
#Slackware Web Server

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

echo -e "\n--- installing packages (Apache, PHP, SQLite MariaDB)"
sed -e 's/^\(BATCH\)=off/\1=on/' -e 's/^\(DEFAULT_ANSWER\)=n/\1=y/' -i /etc/slackpkg/slackpkg.conf
slackpkg install man-pages man-db groff libseccomp
slackpkg install httpd apr-1 apr-util mariadb lz4 lzo sqlite icu4c
slackpkg install php-7 libxml2 libedit libsodium
slackpkg install oniguruma gd-2 libxslt libzip curl libgcrypt libgpg-error fontconfig freetype libXpm libX11 libwebp harfbuzz brotli libxcb graphite2 libXau libXdmcp nghttp2 #PHP: zip, xsl & gd
slackpkg install enchant aspell-0 libiodbc tidy-html5 net-snmp #PHP: aspell, odbc, tidy, snmp
slackpkg install strace patch file-5 git #diff
sed -e 's/^\(BATCH\)=on/\1=off/' -e 's/^\(DEFAULT_ANSWER\)=y/\1=n/' -i /etc/slackpkg/slackpkg.conf


echo "\n--- setup Apache, PHP & MariaDB (for development & remote database access)"
sed -e 's@#\(Include /etc/httpd/mod_php.conf\)@\1@' -e 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' -i /etc/httpd/httpd.conf
cat /etc/php.ini-development > /etc/php.ini
echo "<?php phpinfo(); ?>" > /srv/www/htdocs/index.php

mysql_install_db --user=mysql
sed 's/\(SKIP="--skip-networking"\)/#\1/' -i /etc/rc.d/rc.mysqld

chmod +x /etc/rc.d/rc.httpd
chmod +x /etc/rc.d/rc.mysqld
/etc/rc.d/rc.httpd start
/etc/rc.d/rc.mysqld start

cat > init-root.sql << EOF
create user 'root'@'%' identified by "$ROOTPW";
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges;
EOF
mysql < init-root.sql
rm -f init-root.sql


echo "\n--- setup HTTPS (with Self Signed Certificate + ACME.sh) "
wget -O /usr/local/sbin/acme.sh https://raw.githubusercontent.com/acmesh-official/acme.sh/master/acme.sh
chmod +x /usr/local/sbin/acme.sh

sed -e 's@#\(LoadModule socache_shmcb_module lib64/httpd/modules/mod_socache_shmcb.so\)@\1@' \
	-e 's@#\(LoadModule ssl_module lib64/httpd/modules/mod_ssl.so\)@\1@' \
	-e 's@#\(Include /etc/httpd/extra/httpd-ssl.conf\)@\1@' \
	-i /etc/httpd/httpd.conf

sed 's@^#\(SSLRandomSeed startup file:/dev/urandom 512\)@\1@' -i /etc/httpd/extra/httpd-ssl.conf
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -subj "/CN=www.dev.org/C=FR/ST=France/L=Paris/O=Dev/OU=SSC/" -keyout /etc/httpd/server.key -out /etc/httpd/server.crt
/etc/rc.d/rc.httpd restart


cat > /root/README.md << EOF
- Server is set up with Apache, PHP SQLite & MariaDB (PDO).
	- PHP Developpment mode
	- remote MariaDB access (root password: $ROOTPW)
	- HTTPS enabled (Self Signed Certificate)
- SSH & SFTP:
	- root@$(hostname -I) (SSH key only)
- [acme.sh](https://github.com/acmesh-official/acme.sh) is installed.
EOF
