#!/bin/bash
#Nifty: Install LAMP with PHP from Remi repository.

###
curl -s https://nifty.sh/lib/universal/niftylib.sh > /tmp/lib.nifty.sh
. /tmp/lib.nifty.sh
rm -f /tmp/lib.nifty.sh

# Requirements
require_root
require_64bit
require_os alma8 alma9 alma10

NUMCPU=`cat /proc/cpuinfo  |grep "cpu MHz" |wc -l`

ce "Enabling app stream modules"
dnf -y module enable httpd

ce "Updating the system packages..."
dnf update -y 

ce "Install dnf tools"
dnf install -y dnf-utils dnf-plugins-core

ce "Installing EPEL"
dnf install epel-release yum-utils -y

ce "Installing remi"
OS_RELEASE="$(detect_os)"
OS_VERSION="$(get_os_version)"
rpm -Uvh "http://rpms.remirepo.net/enterprise/remi-release-${OS_VERSION/.*/}.rpm"

mapfile -t PHP_MODULES < <(dnf module list php | awk '/^php.+remi-/ { print $1 ":" $2 }')
if is_interactive; then
  echo
  PHP_MODULE="$(prompt_select_num_value "Which PHP module do you want to install?" "${PHP_MODULES[@]}")"
else
  PHP_MODULE="${PHP_MODULES[-1]}"
fi

ce "Enabling module $PHP_MODULE"
dnf module switch-to -y "$PHP_MODULE"

ce "Installing required packages"
dnf install -y php php-cli php-pdo php-opcache php-xml php-gd php-mcrypt php-mbstring php-soap php-mysqlnd php-pecl-zip tar vim httpd mod_ssl wget

curl vim.dk >> /etc/vimrc

ce "Doing php.ini customization"
sed -i '/^;/d' /etc/php.ini
sed -i '/^$/d' /etc/php.ini
sed -i 's/memory_limit = 128M/memory_limit = 512M/g' /etc/php.ini
sed -i 's/max_execution_time = 30/max_execution_time = 600/g' /etc/php.ini
sed -i 's/max_input_time = 60/max_input_time = 600/g' /etc/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 256M/g' /etc/php.ini
sed -i '3idate.timezone = "Europe/Copenhagen"' /etc/php.ini
sed -i 's/expose_php = On/expose_php = Off/g' /etc/php.ini

ce "Setting up Apache"
sed -i 's/\/var\/www\/html/\/var\/www\/default/g' /etc/httpd/conf/httpd.conf
sed -i 's/^\s*DirectoryIndex.*/DirectoryIndex index.html index.html.var index.php/g' /etc/httpd/conf/httpd.conf
rm -rf /var/www/html
mkdir /var/www/default

mkdir /etc/httpd/vhosts.d

cat <<EOF > /etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"

Listen 80

Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost
ServerTokens Prod
ServerSignature off

<Directory />
    AllowOverride none
    Require all denied
</Directory>


DocumentRoot "/var/www/default"

<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>

<Directory "/var/www/default">
    Options Indexes FollowSymLinks

    AllowOverride None

    Require all granted
</Directory>

<IfModule dir_module>
DirectoryIndex index.html index.html.var index.php
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"

LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>


    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>


    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz



    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on

Protocols h2 h2c http/1.1
FileETag None

IncludeOptional conf.d/*.conf
IncludeOptional vhosts.d/*.conf
EOF

ce "Writing default apache config"
cat <<EOF > /etc/httpd/vhosts.d/00-default.conf
<VirtualHost *:80>
	ServerName	`hostname`
	
	DocumentRoot	/var/www/default
	
	<Directory /var/www/default>
                Options +Indexes +FollowSymLinks +MultiViews
                AllowOverride All
                Require all granted
                IndexOptions FancyIndexing IconsAreLinks
	</Directory>

	CustomLog /var/log/httpd/access_log combined
	ErrorLog /var/log/httpd/error_log
</VirtualHost>
EOF

ce "Enabling and starting Apache & PHP-FPM"
systemctl enable --now httpd php-fpm

cd /var/www/default/
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
tar zxvf phpMyAdmin*.tar.gz
rm -rf *tar.gz
mv phpMyAdmin* phpmyadmin
cd phpmyadmin
mv config.sample.inc.php config.inc.php
sed -i "s/'cookie'/'http'/g" config.inc.php

SIP=`hostname -I |sed 's/ /\n/g' |grep -v 127.0.0.1 |head -n1`

ce "Installing Percona Server"
curl https://nifty.sh/install/universal/percona_server_80.sh |bash

echo -en "\n\n\n"
ce "All done!"
ce "phpMyAdmin at http://$SIP/phpmyadmin"

# Write newdomain script
wget -O /usr/sbin/newdomain https://nifty.sh/misc/centos7/apache_newdomain.php > /dev/null
chmod +x /usr/sbin/newdomain
ce "Installed newdomain script"

