今天给大家带来的是一个实例脚本…. 效果如下:
1、安装过程效果如下:
2、第八选项,第九选项效果如下:
3、当用户输入0,或者输入字母等情况,效果如下:
整体的脚本内容如下:
#!/bin/bash # Date: 4:42 2018-2-10 # Mail: ywyankerp@163.com # Founder: <YanKai> # 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 } zong
运行此脚本时,请调整成能够上网的状态