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

###
function ce()
{
	echo $(tput setaf 4)$@$(tput sgr0)
}

NUMCPU=`cat /proc/cpuinfo  |grep "cpu MHz" |wc -l`
ce "Updating the system packages..."
yum update -y 

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

ce "Installing remi"
rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm

ce "Enabling PHP 8.0 repo"
yum-config-manager --enable remi-php80

ce "Installing required packages"
yum -y install php php-cli php-pdo php-opcache php-xml php-gd php-mbstring php-mcrypt php-soap php-mysqlnd php-bcmath php-pecl-zip vim httpd 


ce "Enabling httpd on startup"
systemctl enable httpd

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
echo "IncludeOptional vhosts.d/*.conf" >> /etc/httpd/conf/httpd.conf

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 "Starting Apache"
systemctl start httpd


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/centos7/percona_server_57.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"












