# Set up Debian node
# Set up collectd for apache2

# Install apache
apt install apache2

apt-get -y install mariadb-client libapache2-mod-php php-xml php php-cgi php-cli php-gd php-curl php-zip php-mysql php-mbstring wget unzip
cd /etc ; git add . ; git commit -a -m 'install php'

a2enmod ssl
a2ensite default-ssl
service apache2 restart

# Open firewall
# Allow web ports
-A INPUT -p tcp --dport  80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Letsencrypt
apt-get install -t stretch-backports letsencrypt python-certbot-apache

certbot --authenticator standalone --installer apache -d matomo.forksand.com --email certbot@forksand.com --pre-hook "service apache2 stop" --post-hook "service apache2 start"

rm /etc/apache2/sites-enabled/default-ssl.conf

service apache2 restart

# Set up MySQL
# On sql1
mysql -u root -p

# In database:
CREATE DATABASE matomo;
CREATE USER 'matomo'@'10.42.1.219' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON matomo.* TO 'matomo'@'10.42.1.219';
FLUSH PRIVILEGES;


# Set up pen on matomo server
apt-get -y install pen
useradd pen
echo "d /var/run/pen 0755 pen pen -" > /etc/tmpfiles.d/pen.conf
systemd-tmpfiles --create

mkdir -p /etc/pen
cat > /etc/pen/galera.cfg <<EOF
server 0 address 10.42.1.171 port 3306
server 2 address 10.42.1.172 port 3306
server 3 address 10.42.1.173 port 3306
server 4 address 10.42.1.174 port 3306
debug 0
EOF

cat > /lib/systemd/system/pen-galera.service <<EOF
[Unit]
Description=Pen load balancer (galera)
After=network.target

[Service]
User=pen
Type=forking
PIDFile=/var/run/pen/galera.pid
ExecStart=/usr/bin/pen -u pen -C /var/run/pen/galera.ctl -F /etc/pen/galera.cfg -p /var/run/pen/galera.pid 127.0.0.1:3306
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl restart pen-galera
systemctl enable pen-galera.service

# Install matomo
wget https://builds.piwik.org/piwik.tar.gz
tar zxf piwik.tar.gz
sudo rm /var/www/html/index.html
sudo mv piwik/* /var/www/html/
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 0755 /var/www/html/tmp
sudo chmod -R 0755 /var/www/html/tmp/assets/
sudo chmod -R 0755 /var/www/html/tmp/cache/
sudo chmod -R 0755 /var/www/html/tmp/logs/
sudo chmod -R 0755 /var/www/html/tmp/tcpdf/
sudo chmod -R 0755 /var/www/html/tmp/templates_c/

# Go here in web browser:
# https://matomo.forksand.com/
#
# Clicky.
# MySQL 127.0.0.1
# Login matomo
# Database name matomo
# Table prefix matomo_
# Adapter PDO\MySQL


##############################################################################
# Copy onto websites:

<!-- Matomo -->
<script type="text/javascript">
  var _paq = _paq || [];
  /* tracker methods like "setCustomDimension" should be called before "trackPageView" */
  _paq.push(['trackPageView']);
  _paq.push(['enableLinkTracking']);
  (function() {
    var u="//matomo.forksand.com/";
    _paq.push(['setTrackerUrl', u+'piwik.php']);
    _paq.push(['setSiteId', '5']);
    var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
    g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
  })();
</script>
<!-- End Matomo Code -->

##############################################################################

##############################################################################
# Post install...
apt install libapache2-mod-geoip php-geoip
