最近对bash shell比较感兴趣,刚好也在看zabbix相关的东西,就整了一个自动安装的脚本,安装完就可以直接配置zabbix,比较省事。以后想要再次安装,上传相关软件到服务器,刷一下脚本即可,非常方便。
本方案采用:MySQL + libgd + Apache + php + zabbix ,经本人实验,该自动安装脚本可用。
#环境介绍
系统版本:centos7.1
相关软件:apr-1.5.2、apr-util-1.5.4 、httpd-2.4.7、libgd-2.1.0、mysql-5.6.26、pcre-8.34、php- 5.6.14、zabbix-2.4.6
以下为bash shell脚本:
#!/bin/bash #定义全局变量 #因为采用通配符的方式,模式匹配apr的时候会匹配到apr-util,所以需要做个特殊处理,因此定义了两个数组,一个为需要安装的软件名称,一个是特殊处理的软件名称。 install_list=("libgd" "mysql" "httpd" "apr" "apr-util" "php" "zabbix" "pcre") special_list=("apr") #定义全局路径 storage_path=/usr/local/src unzip_path=$storage_path/unzip_soft mysql_prefix=/usr/local/mysql libgd_prefix=/usr/local/libgd httpd_prefix=/usr/local/apache php_prefix=/usr/local/php zabbix_prefix=/usr/local/zabbix #检查函数运行情况 function check_execute { if [ $? -ne 0 ] then echo "An error occurred: Function[$1:$2]" echo "Now quit execution ..." echo "Bye!" exit 400 else echo "Running [$1:$2],it's ok!" echo "Continue execution of the script ..." sleep 1 fi } #检查需要特殊处理的软件 function check_special { for special in ${special_list[*]} do if [ "$1" = "$special" ] then echo 100 else echo 200 fi done } #检查所需压缩包是否存在 function check_exist { if [ "$1" = "" ] then echo "Not found archive: $2" echo "Please upload the archive [$2 archive] into dirctory: $storage_path" exit 500 else echo "Found $2: " echo "$1" fi } #检查所需压缩包是否存在 function check_archive { echo "Start looking for the required archive ..." if [ "$(ls $storage_path | gawk '/.*tar*/')" = "" ] then echo "Did not find any archive, please upload relevant archive!" exit 300 fi new_list=(`echo "$@"`) for list in ${new_list[*]} do special=$(check_special "$list") if [ $special -eq 100 ] then found=$(ls $storage_path/*tar* | gawk "/$list/" | gawk '!/util/' | gawk 'BEGIN{FS="/"}{print $NF}') check_exist "$found" "$list" else found=$(ls $storage_path/*tar* | gawk "/$list/" | gawk 'BEGIN{FS="/"}{print $NF}') check_exist "$found" "$list" fi done } #如果一个软件有多个版本,选择使用最高版本 function check_version { if [ "$1" = "" ] then echo "Not found archive: $2" echo "Please upload the archive [$2 archive] into dirctory: $storage_path" exit 500 else tmp="$1" echo $tmp | gawk 'BEGIN{FS=" "}{print $NF}' fi } #解压软件 function unzip_archive { echo "Start decompression ..." rm -rf $unzip_path if [ ! -d $unzip_path ] then mkdir -p $unzip_path fi new_list=(`echo "$@"`) for list in ${new_list[*]} do special=$(check_special "$list") if [ $special -eq 100 ] then found=$(ls $storage_path/*tar* | gawk "/$list/" | gawk '!/util/' | gawk 'BEGIN{FS="/"}{print $NF}') archive=$(check_version "$found") tar -xvf $storage_path/$archive -C $unzip_path else found=$(ls $storage_path/*tar* | gawk "/$list/" | gawk 'BEGIN{FS="/"}{print $NF}') archive=$(check_version "$found") tar -xvf $storage_path/$archive -C $unzip_path fi done } #通过yum的方式安装所需要的环境 function init_env { yum -y install gcc gcc-c++ cmake automake bison bison-devel ncurses ncurses-devel zlib zlib-devel \ libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel fontconfig fontconfig-devel \ libvpx libvpx-devel libXpm libXpm-devel libcurl libcurl-devel openssl openssl-devel \ pcre net-snmp net-snmp-devel libtiff libtiff-devel libxml2 libxml2-devel mysql-devel gettext gettext-devel } #MySQL安装选项 function mysql_install_script { cmake \ -DCMAKE_INSTALL_PREFIX=$mysql_prefix -DMYSQL_DATADIR=$mysql_prefix/data \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 \ -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all \ -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_SSL=yes -DENABLED_LOCAL_INFILE=1 } #配置MySQL function mysql_config { useradd mysql -s /sbin/nologon -M chown -R mysql.mysql $mysql_prefix cp $mysql_prefix/support-files/mysql.server /etc/init.d/mysqld echo "[mysqld] basedir=$mysql_prefix datadir=$mysql_prefix/data port=3306 socket=/tmp/mysql.sock lower_case_table_names=1 character_set_server=utf8 collation_server=utf8_general_ci" > /etc/my.cnf chown mysql.mysql /etc/my.cnf cd $mysql_prefix/scripts ./mysql_install_db --user=mysql --basedir=$mysql_prefix --datadir=$mysql_prefix/data chkconfig mysqld on service mysqld start $mysql_prefix/bin/mysqladmin -u root password "123456" } #调用相关函数,开始安装MySQL function install_mysql { mysql_path=$(ls $unzip_path | gawk '/mysql/') cd $unzip_path/$mysql_path mysql_install_script check_execute "mysql_install_script" "mysql configure" make&&make install check_execute "mysql_install_script" "mysql make&&make install" mysql_config check_execute "mysql_config" "configure mysql" } #libgd安装选项 function libgd_install_script { ./configure \ --prefix=$libgd_prefix --with-zlib -with-png --with-freetype \ --with-fontconfig --with-jpeg --with-vpx --with-xpm --with-tiff } #libgd安装 function install_libgd { libgd_path=$(ls $unzip_path | gawk '/libgd/') cd $unzip_path/$libgd_path libgd_install_script check_execute "libgd_install_script" "libgd configure" make&&make install check_execute "install_libgd" "libgd make&&make install" } #Apache安装选项 function httpd_install_script { httpd_path=$(ls $unzip_path | gawk '/httpd/') apr_path=$(ls $unzip_path | gawk '/apr/' | gawk '!/util/') apr_util_path=$(ls $unzip_path | gawk '/apr-util/') cp -r $unzip_path/$apr_path $unzip_path/$httpd_path/srclib/apr cp -r $unzip_path/$apr_util_path $unzip_path/$httpd_path/srclib/apr-util ./configure \ --prefix=$httpd_prefix \ --with-included-apr=$unzip_path/$httpd_path/srclib/apr \ --with-included-apr-util=$unzip_path/$httpd_path/srclib/apr-util \ --with-mysql=$mysql_prefix --with-pcre } #Apache配置 function httpd_config { sed -in 's/#ServerName www.example.com:80/ServerName localhost:80/' $httpd_prefix/conf/httpd.conf sed -in 's/DirectoryIndex index.html/DirectoryIndex index.php index.html/' $httpd_prefix/conf/httpd.conf sed -in '/AddType application\/x-gzip \.gz \.tgz/a\AddType application/x-httpd-php \.php' $httpd_prefix/conf/httpd.conf sed -in '/AddType application\/x-httpd-php \.php/a\AddType application/x-httpd-php-source \.phps' $httpd_prefix/conf/httpd.conf cp $httpd_prefix/bin/apachectl /etc/init.d/httpd sed -in '/#\!\/bin\/sh/a\#chkconfig:2345 80 80' /etc/init.d/httpd sed -in '/#chkconfig:2345 80 80/a\#description:auto start httpd' /etc/init.d/httpd service httpd start chkconfig httpd on } #安装Apache function install_httpd { httpd_path=$(ls $unzip_path | gawk '/httpd/') cd $unzip_path/$httpd_path httpd_install_script check_execute "httpd_install_script" "httpd configure" make&&make install check_execute "install_httpd" "httpd make&&make install" httpd_config check_execute "httpd_config" "httpd config" } #php安装选项 function php_install_script { ./configure \ --prefix=$php_prefix --enable-zip --with-zlib --with-pcre-dir=/usr --enable-mysqlnd \ --with-zlib --with-libxml-dir=/usr \ --with-openssl --with-pdo-mysql=$mysql_prefix --with-mysqli=$mysql_prefix/bin/mysql_config \ --with-mysql=$mysql_prefix --enable-mbstring --with-freetype-dir=/usr --with-xpm-dir \ --with-zlib --with-png-dir=/usr --with-jpeg-dir=/usr --with-vpx-dir=/usr \ --with-gd=/usr/local/libgd --with-curl --enable-fpm --with-gettext=/usr \ --with-apxs2=$httpd_prefix/bin/apxs --enable-sockets --enable-bcmath } #php配置 function php_config { cp $unzip_path/$php_path/php.ini-development $php_prefix/lib/php.ini sed -in 's/post_max_size = 8M/post_max_size = 16M/' $php_prefix/lib/php.ini sed -in 's/max_execution_time = 30/max_execution_time = 300/' $php_prefix/lib/php.ini sed -in 's/max_input_time = 60/max_input_time = 300/' $php_prefix/lib/php.ini sed -in 's/;date.timezone =/date.timezone = PRC/' $php_prefix/lib/php.ini sed -in 's/;always_populate_raw_post_data = -1/always_populate_raw_post_data = -1/' $php_prefix/lib/php.ini } #安装php function install_php { php_path=$(ls $unzip_path | gawk '/php/') cd $unzip_path/$php_path php_install_script check_execute "php_install_script" "php configure" make&&make install check_execute "install_php" "php make&&make install" php_config check_execute "php_config" "php config" } #zabbix安装选项 function zabbix_install_script { ./configure \ --prefix=$zabbix_prefix --with-mysql --with-libcurl --with-net-snmp \ --enable-server --enable-agent --enable-proxy } #zabbix安装之前做的一些配置 function zabbix_preconfig { zabbix_path=$(ls $unzip_path | gawk '/zabbix/') useradd zabbix -s /sbin/nologon -M $mysql_prefix/bin/mysql -u root -p123456 -e "create user 'zabbix'@'localhost' identified by '123456';" $mysql_prefix/bin/mysql -u root -p123456 -e "flush privileges;" $mysql_prefix/bin/mysql -u root -p123456 -e "create database zabbix;" $mysql_prefix/bin/mysql -u root -p123456 -e "grant all privileges on zabbix.* to zabbix@'%' identified by '123456';" $mysql_prefix/bin/mysql -u root -p123456 -e "flush privileges;" $mysql_prefix/bin/mysql -u root -p123456 -e "use zabbix;" $mysql_prefix/bin/mysql -u root -p123456 zabbix -e "source $unzip_path/$zabbix_path/database/mysql/schema.sql" $mysql_prefix/bin/mysql -u root -p123456 zabbix -e "source $unzip_path/$zabbix_path/database/mysql/images.sql" $mysql_prefix/bin/mysql -u root -p123456 zabbix -e "source $unzip_path/$zabbix_path/database/mysql/data.sql" } #zabbix配置 function zabbix_config { cp -r $unzip_path/$zabbix_path/frontends/php $httpd_prefix/htdocs/zabbix cp $unzip_path/$zabbix_path/misc/init.d/tru64/zabbix_server /etc/init.d/zabbix_server cp $unzip_path/$zabbix_path/misc/init.d/tru64/zabbix_agentd /etc/init.d/zabbix_agentd sed -in 's/DAEMON=\/usr\/local\/sbin\/zabbix_server/DAEMON=\/usr\/local\/zabbix\/sbin\/zabbix_server/' /etc/init.d/zabbix_server sed -in 's/DAEMON=\/usr\/local\/sbin\/zabbix_agentd/DAEMON=\/usr\/local\/zabbix\/sbin\/zabbix_agentd/' /etc/init.d/zabbix_agentd sed -in '/#\!\/bin\/sh/a\#chkconfig:2345 81 81' /etc/init.d/zabbix_server sed -in '/#chkconfig:2345 81 81/a\#description:auto start zabbix_server' /etc/init.d/zabbix_server sed -in '/#\!\/bin\/sh/a\#chkconfig:2345 82 82' /etc/init.d/zabbix_agentd sed -in '/#chkconfig:2345 82 82/a\#description:auto start zabbix_agentd' /etc/init.d/zabbix_agentd sed -in 's/# DBPassword=/DBPassword=123456/' $zabbix_prefix/etc/zabbix_server.conf sed -in 's/# DBSocket=\/tmp\/mysql.sock/DBSocket=\/tmp\/mysql.sock/' $zabbix_prefix/etc/zabbix_server.conf chmod +x /etc/init.d/zabbix_* service zabbix_server start service zabbix_agentd start chkconfig zabbix_server on chkconfig zabbix_agentd on } #安装zabbix function install_zabbix { zabbix_path=$(ls $unzip_path | gawk '/zabbix/') cd $unzip_path/$zabbix_path zabbix_preconfig check_execute "zabbix_preconfig" "zabbix preconfig" zabbix_install_script check_execute "zabbix_install_script" "zabbix configure" make&&make install check_execute "install_zabbix" "zabbix make&&make install" zabbix_config check_execute "zabbix_config" "zabbix config" } function close_selinux_firewalld { service firewalld stop chkconfig firewalld off sed -in 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config } #调用相关函数,检查压缩包,解压,安装相关软件 function run_install { check_archive `echo ${install_list[*]}` check_execute "check_archive" "Check Archive" unzip_archive `echo ${install_list[*]}` check_execute "unzip_archive" "Unzip Archive" init_env check_execute "init_env" "yum install" install_mysql check_execute "mysql_install" "mysql install" install_libgd check_execute "libgd_install" "lingd install" install_httpd check_execute "httpd_install" "httpd install" install_php check_execute "php_install" "php install" install_zabbix check_execute "zabbix_install" "zabbix install" close_selinux_firewalld check_execute "zclose_selinux_firewalld" echo "--------------------------------------" echo "Congratulation!" echo "All software installation is complete!" echo "Done." echo "--------------------------------------" } run_install echo "Manually reboot the system,open a browser, enter ip/zabbix see the effects."
完毕!