#!/bin/sh

if [ `rpm -qa | grep httpd | wc -l` = 0 ] ; then
	yum -y install httpd mariadb mariadb-server php php-common php-devel php-fpm php-gd php-mbstring php-mysql
fi

if [ `cat /etc/my.cnf | grep "set names utf8" | wc -l` = 0 ] ; then
	echo "[mysqld]" >> /etc/my.cnf
	echo "init_connect='set names utf8'" >> /etc/my.cnf
	echo "character-set-server=utf8" >> /etc/my.cnf
	echo "max_connections=1000" >> /etc/my.cnf
	echo "log_slow_queries = On" >> /etc/my.cnf
	echo "long_query_time = 5" >> /etc/my.cnf
	echo "join_buffer_size = 16MB" >> /etc/my.cnf
	echo "key_buffer_size = 8MB" >> /etc/my.cnf
	echo "read_rnd_buffer_size = 32MB" >> /etc/my.cnf
	echo "sort_buffer_size = 32MB" >> /etc/my.cnf
	echo "query_cache_limit = 256KB" >> /etc/my.cnf
	echo "query_cache_min_res_unit = 4096" >> /etc/my.cnf
	echo "query_cache_size = 16MB" >> /etc/my.cnf
	echo "thread_cache_size = 8" >> /etc/my.cnf
	echo "table_open_cache = 16384" >> /etc/my.cnf
	echo "max_heap_table_size = 256MB" >> /etc/my.cnf
	echo "tmp_table_size = 256MB" >> /etc/my.cnf
	echo "[Service]" >> /etc/systemd/system/multi-user.target.wants/mariadb.service
	echo "LimitNOFILE=32768" >> /etc/systemd/system/multi-user.target.wants/mariadb.service
fi

if [ `cat /etc/php.ini | grep "short_open_tag = On" | wc -l` = 0 ] ; then
	sed -i 's/^short_open_tag\ =\ Off/short_open_tag\ =\ On/g' /etc/php.ini
	sed -i 's/^allow_url_fopen\ =\ On/allow_url_fopen\ =\ Off/g' /etc/php.ini
	sed -i 's/^post_max_size\ =\ 8M/post_max_size\ =\ 32M/g' /etc/php.ini
	sed -i 's/^upload_max_filesize\ =\ 2M/upload_max_filesize\ =\ 5M/g' /etc/php.ini
	sed -i 's/^;date.timezone\ =/date.timezone\ =\ Asia\/Seoul/g' /etc/php.ini
	sed -i 's/^session.gc_maxlifetime\ =\ 1440/session.gc_maxlifetime\ =\ 43200/g' /etc/php.ini
fi
if [ `cat /etc/httpd/conf.d/php.conf | grep "^AddType application/x-httpd-php" | wc -l` = 0 ] ; then
	sed -i 's/^AddType\ text\/html\ .php/AddType\ text\/html\ .php\nAddType\ application\/x-httpd-php\ .htm\ .html\ .php\ .php3\ .php4\ inc/g' /etc/httpd/conf.d/php.conf
fi

ystemctl enable httpd.service
systemctl enable mariadb.service
systemctl start httpd.service
systemctl start mariadb.service
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
mysql_secure_installation
