#!/bin/bash
# lty 20180116
# fastdfs install script

if [ `id -u` -ne 0 ];then
    echo "非root用户,脚本退出"
    exit 0
fi
if [ ! -f "/root/soft/fastdfs_install.sh" ];then
    echo "请将安装脚本方在/root/soft目录后重新执行安装。"
fi

install_fdfs()
{
cd /root/soft
wget https://codeload.github.com/happyfish100/libfastcommon/zip/master -O libfastcommon-master.zip
if [ ! -f "libfastcommon-master.zip" ];then
    echo "下载libfastcommon-master.zip出错" && exit 1 
fi
unzip libfastcommon-master.zip
cd libfastcommon-master/
sh ./make.sh
sh ./make.sh install
cd ..
wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Server%20Source%20Code/FastDFS%20Server%20with%20PHP%20Extension%20Source%20Code%20V5.08/FastDFS_v5.08.tar.gz
if [ ! -f "FastDFS_v5.08.tar.gz" ];then
    echo "下载FastDFS_v5.08.tar.gz出错" && exit 1
fi
tar xf FastDFS_v5.08.tar.gz
cd FastDFS
sh ./make.sh
sh ./make.sh install
cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp tracker.conf.sample tracker.conf
cp client.conf.sample client.conf

mkdir -p /data/fastdfs
sed -i "s@/home/yuqing/@/data/@g" ./*f
}

install_nginx()
{
cd /root/soft
wget http://nchc.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
if [ ! -f "fastdfs-nginx-module_v1.16.tar.gz" ];then
    echo "下载fastdfs-nginx-module_v1.16.tar.gz出错"
fi 
tar xf fastdfs-nginx-module_v1.16.tar.gz 
cd fastdfs-nginx-module/src/
sed -i "s@/usr/local/@/usr/@g" config
# 或手动编辑vim config,/usr/local改成/usr,执行:%s+/usr/local/+/usr/+g命令进行批量替换并保存退出
\cp mod_fastdfs.conf /etc/fdfs/

# 开始配置fastdfs
IPLINE=`ip addr | grep -E 'inet 172|inet 192|inet 10' | awk -F'[/]' '{print$1}' | awk '{print$2}' |wc -l`
if [ $IPLINE -gt 1 ];then
echo "本机有多个内网IP,请手动配置/etc/fdfs/mod_fastdfs.conf的tracker_server项"
elif [ $IPLINE -eq 1 ];then
IP=`ip addr | grep -E 'inet 172|inet 192|inet 10' | awk -F'[/]' '{print$1}' | awk '{print$2}'`
sed -i "s@tracker:22122@$IP:22122@" /etc/fdfs/mod_fastdfs.conf
sed -i "s@192.168.0.197@$IP@g" /etc/fdfs/*conf
sed -i "s@192.168.209.121@$IP@g" /etc/fdfs/*conf
else
echo "未检测到本机内网IP,请手动配置/etc/fdfs/mod_fastdfs.conf的tracker_server项"
fi
sed -i "s@url_have_group_name = false@url_have_group_name = true@" /etc/fdfs/mod_fastdfs.conf
sed -i "s@home/yuqing@data@g" /etc/fdfs/*conf

#需要检查是否有系统安装的nginx
NGINX_PROC=`ps -ef | grep nginx | grep -v grep`
if [ $NGINX_PROC ];then
echo "存在nginx进程,将跳过nginx安装。请手动配置nginx.conf"
return
fi

cd /root/soft
yum install -y pcre-devel zlib-devel
wget http://nginx.org/download/nginx-1.12.2.tar.gz
if [ ! -f "nginx-1.12.2.tar.gz" ];then
    echo "下载nginx-1.12.2.tar.gz出错" && exit 1
fi
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/apps/nginx/ --add-module=../fastdfs-nginx-module/src/
make
make install
cd /apps/nginx/conf
cat > /apps/nginx/conf/nginx.conf <<EOF
    user  root;    
    worker_processes  1;
    events {
       worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        client_max_body_size 300M;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
            location ~/group[0-9]/ {
                ngx_fastdfs_module;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            #root   html;
            }
        }
    }
EOF

\cp /apps/nginx/conf/mime.types /etc/fdfs/
\cp /root/soft/FastDFS/conf/http.conf /etc/fdfs/
}

# 启动
start_service()
{
fdfs_storaged /etc/fdfs/storage.conf
fdfs_trackerd /etc/fdfs/tracker.conf
ln -s /data/fastdfs/data/ /data/fastdfs/data/M00
/apps/nginx/sbin/nginx
echo "fdfs、nginx安装完成,启动成功"
ps -ef | grep -E "nginx|fdfs"| grep -v grep
}

install_fdfs || exit 1
install_nginx|| exit 1
start_service|| exit 1
exit 0