#!/bin/bash # Describe: This is a one - button installation service script # 提示:使用此腳本時,儘可能先選擇第一項配置Yum源! red_col="\e[1;31m" reset_col="\e[0m" LOG_DIR=/usr/local/src . /etc/init.d/functions MMM=`rpm -qa | grep mariadb` #node 1. function caidan(){ cat <<YanKaI +------------------------------------------------+ | 1. 配置Yum源 | | _o0o_ 2. 安裝Nginx | | 08880 3. 安裝Apache | | 88"."88 4. 安裝MySQL | | (|-_-|) 5. 安裝PHP | | 0\=/0 6. 部署LNMP環境 | | __/ \__ 7. 安裝zabbix監控 | | ‘\ ///‘ 8. 退出此管理程序 | | / Linux一鍵 \ 9. 關閉計算機 | | || Server || ====================== | | \ //// 一鍵安裝服務 | | ||| i i i ||| by YanKaI | | ___ ___ ====================== | |___‘. /--.--\ .‘___ | +------------------------------------------------+ YanKaI } caidan #node 2. function panduan (){ read -p "請您輸入{1-9}任意選項:" NUM expr $NUM + 1 &> /dev/null if [ "$?" -ne 0 ];then action "請您輸入數值" /bin/false elif [[ "$NUM" == 0 ]];then action "請您輸入比0大的數值" /bin/false fi } #node 3.Yum function Yum_check(){ echo -en "${red_col}正在配置Yum源,請稍等....\n${reset_col}" if [ -f /etc/yum.repos.d/CentOS-Base.repo ] then mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.back && \ wget http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null && \ mv Centos-7.repo /etc/yum.repos.d/CentOS-Base.repo && yum clean all &>/dev/null && yum makecache &>/dev/null fi if [ "$?" -eq 0 ];then action "Yum源配置成功!!!" /bin/true else action "Yum源配置失敗,請您檢查網絡" /bin/false exit 1 fi } #node 4.Nginx function Nginx_server(){ echo -en "${red_col}開始安裝Nginx服務,請稍後.....\n${reset_col}" yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y &>/dev/null && useradd -M -s /sbin/nologin nginx && \ cd $LOG_DIR && wget http://nginx.org/download/nginx-1.12.2.tar.gz &>/dev/null && tar zxf nginx-1.12.2.tar.gz && \ cd $LOG_DIR/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx &>/dev/null && make &>/dev/null && make install &>/dev/null if [ -f /usr/local/nginx/sbin/nginx ] then ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin fi if [ "$?" -eq 0 ] then action "Nginx安裝成功" /bin/true && /usr/local/nginx/sbin/nginx else action "Nginx安裝失敗,請檢查" /bin/false exit 1 fi } #node 2. Apache function Apache_server(){ echo -en "${red_col}開始安裝Apache服務,請稍後.....\n${reset_col}" yum install httpd -y &>/dev/null if [ "$?" -eq 0 ] then action "Apache安裝成功!" /bin/true else action "Apache安裝失敗,請檢查環境" /bin/false exit 1 fi } #node 3. MySQL function Mysql_server(){ echo -en "${red_col}開始安裝Mysql數據庫,請稍後.....\n${reset_col}" if [ -n $MMM ] then rpm -e mariadb-libs --nodeps 2&>/dev/null else action "mariadb卸載失敗,請重試..." /bin/false exit 1 fi cd $LOG_DIR && { wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz &>/dev/null && tar zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz && \ mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql && \ cd /usr/local/mysql/ && mkdir data && mkdir log && echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile && \ source /etc/profile && groupadd mysql && useradd -r -g mysql -s /bin/false mysql } cat << EOF > /etc/my.cnf [client] socket=/usr/local/mysql/mysql.sock [mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data pid-file=/usr/local/mysql/data/mysqld.pid socket=/usr/local/mysql/mysql.sock log_error=/usr/local/mysql/log/mysql.err EOF if [ -f /etc/my.cnf ] then chmod 750 data/ && chown -R mysql . && chgrp -R mysql . && bin/mysqld --initialize --user=mysql && cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && service mysqld start &> /dev/null else echo "MySQL安裝失敗!!!" exit 1 fi ln -s /usr/local/mysql/bin/mysql /usr/local/sbin ln -s /usr/local/mysql/bin/mysqladmin /usr/local/sbin #oo=`cat /usr/local/mysql/log/mysql.err | grep root@localhost | awk -F ":" '{print $4}' | cut -d " " -f 2` oo=`cat /usr/local/mysql/log/mysql.err | grep password | awk '{print $11}'` mysqladmin -uroot -p${oo} password pwd123 &>/dev/null if [ "$?" -eq 0 ] then action "MySQL數據庫安裝成功,密碼爲:pwd123" /bin/true else action "MySQL數據庫安裝失敗密碼初始化失敗!" /bin/false fi } #node 4. PHP function php_server(){ echo -en "${red_col}開始安裝依賴包,請稍後....\n${reset_col}" yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel &> /dev/null if [ "$?" -eq 0 ];then action "依賴包安裝完成" /bin/true else action "依賴包安裝失敗請檢查環境!!!" /bin/false exit 1 fi yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel libjpeg-devel libpng libpng-devel freetype-devel libmcrypt libmcrypt-devel &>/dev/null cd $LOG_DIR && { echo -en "${red_col}開始安裝PHP請稍後,過程稍微有點長...\n${reset_col}" wget http://cn2.php.net/distributions/php-5.6.27.tar.gz &> /dev/null && \ tar zxf php-5.6.27.tar.gz && cd php-5.6.27/ && \ ./configure --prefix=/usr/local/php5.6 --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/usr/local/mysql/mysql.sock --with-gd --with-iconv --with-libxml-dir=/usr --with-mhash --with-mcrypt-dir --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-zlib --with-freetype-dir --with-png-dir --with-jpeg-dir --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-curl &>/dev/null } if [ "$?" -eq 0 ] then make &>/dev/null && make install &>/dev/null else action "PHP安裝失敗,請檢查環境..." /bin/false exit 1 fi echo -en "${red_col}正在配置PHP,請稍等....\n${reset_col}" cd /usr/local/src/php-5.6.27/ && { cp php.ini-production /etc/php.ini && cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on } if [ "$?" -eq 0 ];then cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf && echo -en "${red_col}複製主配置成功${reset_col}" else action "配置PHP環境出錯,請檢查..." /bin/false exit 1 fi if [ ! -d /usr/local/php5.6/etc/php-fpm.conf ] then sed -i 's#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/listen = 127.0.0.1:9000/listen = 127.0.0.1:9000/g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/pm.max_children = 5/pm.max_children = 300/g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/pm.start_servers = 2/pm.start_servers = 20/g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 20/g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 100/g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/user = nobody/user = nginx/g' /usr/local/php5.6/etc/php-fpm.conf sed -i 's/group = nobody/group = nginx/g' /usr/local/php5.6/etc/php-fpm.conf else action "配置PHP環境出錯,請檢查..." /bin/false exit 1 fi if [ $? -eq 0 ] then systemctl start php-fpm && action "PHP啓動成功" /bin/true else echo "啓動PHP失敗" exit 1 fi } #node 5. LNMP_server function LNMP(){ Nginx_server Mysql_server php_server echo -en "${red_col}開始安裝LNMP環境,請稍後..能夠喝杯茶~\n${reset_col}" if [ "$?" -eq 0 ];then echo -en "${red_col}LNMP安裝完成,正在配置Nginx解析PHP請稍後....\n${reset_col}" else action "LNMP安裝失敗,請您檢查環境..." /bin/false exit 1 fi if [ -f /usr/local/nginx/conf/nginx.conf ] then sed -i 's/index index.html index.htm;/index index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf sed -i 's/# root html;/ root html;/g' /usr/local/nginx/conf/nginx.conf sed -i 's/# fastcgi_pass 127.0.0.1:9000;/ fastcgi_pass 127.0.0.1:9000;/g' /usr/local/nginx/conf/nginx.conf sed -i 's/# fastcgi_index index.php;/ fastcgi_index index.php;/g' /usr/local/nginx/conf/nginx.conf sed -i 's*# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;* fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;*g' /usr/local/nginx/conf/nginx.conf sed -i 's/# include fastcgi_params;/ include fastcgi.conf;/g' /usr/local/nginx/conf/nginx.conf sed -i '71d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;70a}' /usr/local/nginx/conf/nginx.conf sed -i '65d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;64alocation ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf else action "Nginx解析php失敗,請您檢查環境..." /bin/false exit 1 fi touch /usr/local/nginx/html/2018.php cat << EOF >/usr/local/nginx/html/2018.php <?php phpinfo(); ?> EOF /usr/local/nginx/sbin/nginx -s reload if [ "$?" -eq 0 ] then action "LNMP環境正式搭建成功,請您訪問http://IP/2018.php" /bin/true else action "LNMP搭建失敗啦,哈哈哈請您檢查一下環境吧..." exit 1 fi } function zabbix_server(){ if [ -f /usr/local/nginx/sbin/nginx -a -f /etc/my.cnf -a -f /etc/php.ini ];then echo -en "${red_col}開始安裝zabbix請稍後!!!\n${reset_col}" else action "提示:在安裝zabbix以前請先安裝LNMP環境!" /bin/false exit 1 fi groupadd zabbix && useradd -g zabbix zabbix sed -i 's/;date.timezone =/date.timezone = PRC/g' /etc/php.ini sed -i 's/expose_php = On/expose_php = Off/g' /etc/php.ini sed -i 's/short_open_tag = Off/short_open_tag = On/g' /etc/php.ini sed -i 's/post_max_size = 8M/post_max_size = 16M/g' /etc/php.ini sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /etc/php.ini sed -i 's/max_input_time = 60/max_input_time = 300/g' /etc/php.ini sed -i 's/;always_populate_raw_post_data = -1/always_populate_raw_post_data = -1/g' /etc/php.ini sed -i 's/;mbstring.func_overload = 0/mbstring.func_overload = 0/g' /etc/php.ini if [ "$?" -eq 0 ];then echo -en "${red_col}編輯/etc/php.ini文件成功!\n${reset_col}" else action "編輯/etc/php.ini文件失敗!" /bin/false exit 1 fi yum install -y net-snmp net-snmp-devel curl-devel java-1.8.0-openjdk java-1.8.0-openjdk-devel OpenIPMI-devel libssh2-devel &>/dev/null if [ "$?" -eq 0 ] then action "zabbix依賴包安裝成功!!!" /bin/true else action "zabbix依賴包安裝失敗" /bin/false exit 1 fi cd $LOG_DIR && { wget http://www.fping.org/dist/fping-3.10.tar.gz &>/dev/null && tar zxf fping-3.10.tar.gz &>/dev/null && \ cd fping-3.10/ && ./configure &>/dev/null && make &>/dev/null && make install &>/dev/null } if [ "$?" -eq 0 ] then chown root:zabbix /usr/local/sbin/fping && chmod 4710 /usr/local/sbin/fping else action "fping安裝失敗" /bin/false exit 1 fi cd $LOG_DIR && { wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.1/zabbix-3.2.1.tar.gz &>/dev/null && \ tar zxf zabbix-3.2.1.tar.gz && cd zabbix-3.2.1/ && \ ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --enable-java --with-mysql=/usr/local/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-openipmi &>/dev/null && make &>/dev/null && make install &>/dev/null } if [ "$?" -eq 0 ] then action "zabbix安裝成功,正在配置請稍後...." /bin/true else action "zabbix安裝失敗,請您檢查環境...." /bin/false exit 1 fi ln -s /usr/local/zabbix/bin/* /usr/local/bin/ ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/ if [ "$?" -eq 0 ]; then /usr/local/sbin/mysql -uroot -ppwd123 -e 'create database zabbix character set utf8;' &>/dev/null /usr/local/sbin/mysql -uroot -ppwd123 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'" &>/dev/null else action "zabbix建立軟鏈接失敗!" /bin/false exit 1 fi if [ "$?" -eq 0 ] then action "zabbix數據庫建立成功!" /bin/true else action "zabbix數據庫建立失敗!" /bin/false exit 1 fi cd /usr/local/src/zabbix-3.2.1/database/mysql && { /usr/local/sbin/mysql -uzabbix -pzabbix -hlocalhost zabbix < schema.sql &>/dev/null /usr/local/sbin/mysql -uzabbix -pzabbix -hlocalhost zabbix < images.sql &>/dev/null /usr/local/sbin/mysql -uzabbix -pzabbix -hlocalhost zabbix < data.sql &>/dev/null } if [ "$?" -ne 0 ] then action "zabbix數據庫導入失敗!" /bin/false exit 1 else action "zabbix數據庫導入成功!" /bin/true fi sed -i 's$LogFile=/tmp/zabbix_server.log$LogFile=/usr/local/zabbix/logs/zabbix_server.log$g' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's$# PidFile=/tmp/zabbix_server.pid$PidFile=/usr/local/zabbix/logs/zabbix_server.pid$g' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's/# DBHost=localhost/DBHost=localhost/g' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's/# DBPassword=/DBPassword=zabbix/g' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's/# DBPort=3306/DBPort=3306/g' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's$# FpingLocation=/usr/sbin/fping$FpingLocation=/usr/sbin/fping$g' /usr/local/zabbix/etc/zabbix_server.conf sed -i 's$# DBSocket=/tmp/mysql.sock$DBSocket=/usr/local/mysql/mysql.sock$g' /usr/local/zabbix/etc/zabbix_server.conf mkdir -p /usr/local/zabbix/logs && chown -R zabbix:zabbix /usr/local/zabbix/ cat <<EOF >/etc/ld.so.conf include ld.so.conf.d/*.conf /usr/local/mysql/lib /usr/local/lib EOF /usr/local/nginx/sbin/nginx -s reload && /etc/init.d/php-fpm restart &>/dev/null if [ "$?" -eq 0 ];then action "zabbix主配修改爲功,正在進行相關的配置~" /bin/true else action "zabbix發生了嚴重的錯誤!!!" /bin/false exit 1 fi cd /usr/local/src/zabbix-3.2.1/ && { cp misc/init.d/fedora/core/zabbix_server /etc/rc.d/init.d/zabbix_server cp misc/init.d/fedora/core/zabbix_agentd /etc/rc.d/init.d/zabbix_agentd chmod +x /etc/rc.d/init.d/zabbix_server chmod +x /etc/rc.d/init.d/zabbix_agentd chkconfig --add zabbix_server chkconfig --add zabbix_agentd chkconfig zabbix_server on chkconfig zabbix_agentd on } if [ "$?" -eq 0 ];then action "複製zabbix啓動項成功" /bin/true else action "複製zabbix啓動項失敗!!" /bin/false exit 1 fi sed -i 's$BASEDIR=/usr/local$BASEDIR=/usr/local/zabbix/$g' /etc/rc.d/init.d/zabbix_server sed -i 's*PIDFILE=/tmp/$BINARY_NAME.pid*PIDFILE=/usr/local/zabbix/logs/$BINARY_NAME.pid*g' /etc/rc.d/init.d/zabbix_server sed -i 's$BASEDIR=/usr/local$BASEDIR=/usr/local/zabbix/$g' /etc/rc.d/init.d/zabbix_agentd sed -i 's*PIDFILE=/tmp/$BINARY_NAME.pid*PIDFILE=/usr/local/zabbix/logs/$BINARY_NAME.pid*g' /etc/rc.d/init.d/zabbix_agentd ldconfig && systemctl daemon-reload /etc/init.d/zabbix_agentd start &>/dev/null /etc/init.d/zabbix_server start &>/dev/null if [ "$?" -eq 0 ];then action "zabbix監控啓動成功,正在進行配置頁面,請稍後!!!" /bin/true else action "zabbix監控啓動失敗,請檢查環境!!!" /bin/false exit 1 fi mkdir /usr/local/nginx/html/zabbix/ if [ -d /usr/local/nginx/html/zabbix/ ];then cp -r /usr/local/src/zabbix-3.2.1/frontends/php/* /usr/local/nginx/html/zabbix/ else action "zabbix目錄建立失敗" /bin/false exit 1 fi chown -R nginx:nginx /usr/local/nginx/html/zabbix if [ $? -eq 0 ];then echo "zabbix部署成功,請您訪問http://IP/zabbix" else echo "zabbix部署失敗,請檢查環境!!!" exit 1 fi } function TC (){ echo -e "${red_col}此管理程序已經成功退出!\n${reset_col}" exit 0 } function GJ(){ echo -e "${red_col}即將關機,請稍後....\n${reset_col}" shutdown -h now } function zong(){ while : do panduan case $NUM in 1) Yum_check ;; 2) Nginx_server ;; 3) Apache_server ;; 4) Mysql_server ;; 5) php_server ;; 6) LNMP ;; 7) zabbix_server ;; 8) TC ;; 9) GJ ;; esac done }