#!/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 https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/rpm/nginx-release-centos-7-0.el7.ngx.noarch.rpm
	#rpm -Uvh https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/rpm/epel-release-latest-7.noarch.rpm
	rpm -ivh https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/rpm/epel-release-7-5.noarch.rpm
	rpm -Uvh https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/rpm/webtatic-release.rpm
	rpm -Uvh https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/rpm/remi-release-7.rpm
	rpm -ivh https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/rpm/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/ https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/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/ https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/lnmp70/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 php70w php70w-opcache php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel php70w-intl
	#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/ https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/lnmp70/config/php/php.ini
	#wget -P /etc/ https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/lnmp70/config/php/php-fpm.conf
	#wget -P /etc/php-fpm.d/ https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/lnmp70/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/ https://francis-oss.oss-cn-shenzhen.aliyuncs.com/download/lnmp70/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...'

