#!/bin/bash +x

disable_selinux() {
	if [ -s /etc/selinux/config ] && grep 'SELINUX=enforcing' /etc/selinux/config; then
		sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
		setenforce 0
	fi
}

system_init(){
	echo 'system inti...'
	disable_selinux
	mkdir /data1/www
	systemctl stop firewalld.service
	systemctl disable firewalld.service
	yum -y upgrade 
	yum install -y yum-plugin-fastestmirror 
	yum install -y gcc gcc-c++ wget lrzsz zip unzip vim
}

download_rpm(){
	echo 'download rpm...'
	rpm -Uvh http://download.icloudstudio.com/rpm/nginx-release-centos-7-0.el7.ngx.noarch.rpm
	rpm -Uvh http://download.icloudstudio.com/rpm/epel-release-latest-7.noarch.rpm
	rpm -Uvh http://download.icloudstudio.com/rpm/remi-release-7.rpm
	rpm -ivh http://download.icloudstudio.com/rpm/mysql-community-release-el7.rpm
	#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
	#rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
	#rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
	#rpm -ivh http://repo.mysql.com/mysql-community-release-el7.rpm
}

yum_setting(){
	echo 'yum setting...'
	mv /etc/yum.repos.d/remi.repo /etc/yum.repos.d/remi.repo_backup
	wget -P /etc/yum.repos.d/ http://download.icloudstudio.com/rpm/remi.repo
	#yum clean all
	#yum -y upgrade
	#yum makecache
}

permission_setting(){
	echo 'permission setting...'
	groupadd www
	useradd -G www -s /sbin/nologin nginx
	useradd -G www web
	
	chown -R mysql:mysql /data1/mysql
	chown -R nginx:www /data1/nginx
	chown -R nginx:www /data1/php
	chown -R nginx:www /data1/php-fpm
	chown -R nginx:www /data1/www
}

install_nginx(){
	echo 'install nginx...'
	mkdir -p /data1/nginx/logs
	mkdir -p /data1/nginx/siteconf
	yum -y install nginx
	mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf_backup
	wget -P /etc/nginx/ http://download.icloudstudio.com/lnmp/config/nginx/nginx.conf
}

install_php(){
	echo 'install php-fpm...'
	mkdir -p /data1/php/session
	mkdir -p /data1/php/wsdlcache
	mkdir -p /data1/php-fpm/logs
	yum -y install php php-opcache php-pecl-apcu php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof php-pdo php-pear php-fpm php-cli php-xml php-bcmath php-process php-gd php-common
	mv /etc/php.ini /etc/php.ini_backup
	mv /etc/php-fpm.conf /etc/php-fpm.conf_backup
	mv /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf_backup
	wget -P /etc/ http://download.icloudstudio.com/lnmp/config/php/php.ini
	wget -P /etc/ http://download.icloudstudio.com/lnmp/config/php/php-fpm.conf
	wget -P /etc/php-fpm.d/ http://download.icloudstudio.com/lnmp/config/php/www.conf
}

install_mysql(){
	mkdir -p /data1/mysql/logs
	mkdir -p /data1/mysql/data
	yum -y install mysql-community-server
	mv /etc/my.cnf /etc/my.cnf_backup
	wget -P /etc/ http://download.icloudstudio.com/lnmp/config/mysql/my.cnf
}

service_activate(){
	systemctl enable php-fpm
	systemctl restart php-fpm
	systemctl enable nginx
	systemctl restart nginx
	systemctl enable mysql
	systemctl restart mysql
}

enable_proxy(){
	cp /etc/profile /etc/profile_backup
	echo 'export http_proxy=http://ecs.icloudstudio.com:9394' >> /etc/profile
	echo 'export https_proxy=http://ecs.icloudstudio.com:9394' >> /etc/profile
	echo 'export ftp_proxy=http://ecs.icloudstudio.com:9394' >> /etc/profile
	source /etc/profile
}

disable_proxy(){
	rm -rf /etc/profile
	mv /etc/profile_backup /etc/profile
	source /etc/profile
}

install_main(){
	system_init
	download_rpm
	yum_setting

	enable_proxy

	install_nginx
	install_php
	install_mysql

	disable_proxy

	permission_setting
	service_activate
}

install_main

echo 'complete...'

