安装zabbix监控首先的先安装LNMP环境,在这里我采用事先准备好的脚本进行安装LNMP环境 脚本内容如下:

  1 #!/bin/bash
  2 # DATE:Wed Jan 17 1# hw226234@126.com  3 #this is a lnmp-install
  4  
  5 # The Nginx service is being installed
  6 ROOT_ID=0
  7 ML=/usr/local/src
  8 NGINX_ML=/usr/local/src/nginx-1.12.2
  9 PML=/usr/local/src/php-5.6.27
 10 QD=`netstat -anpt | grep 80 | awk '{print $7}' | cut -d "/" -f2 | cut -d ":" -f1`
 11 ADDR=`ip a | grep eth0$ | awk '{print $2}' | cut -d "/" -f1`
 12 MMM=`netstat -anput | grep mysqld | awk '{print $4}' | cut -d ":" -f4`
 13 MAR=`rpm -qa | grep mariadb`
 14 cd $ML && {
 15 wget http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
 16 mv Centos-7.repo /etc/yum.repos.d/Centos-Base.repo
 17 yum clean all &> /dev/null
 18 yum makecache &> /dev/null
 19 }
 20  
 21 if [ $? -eq 0 ]
 22     then
 23           echo "Yum源更新完毕,开始安装!!!"
 24     else
 25         echo "Yum源error"
 26         exit 1
 27 fi
 28 echo "Yum源配置成功"
 29  
 30 if [ "$UID" -eq "$ROOT_ID" ]
 31         then
 32                echo "root用户运行中" && systemctl stop firewalld && yum install -y gcc gcc-c++  pcre-devel zlib-devel openssl-devel &> /dev/null
 33         else
 34                echo "请使用Root用户运行"
 35                exit 1
 36 fi
 37 echo "依赖包安装完成!"
 38 cd $ML || {
 39         echo "进入目录失败!!!"
 40         exit 1
 41 }
 42  
 43 wget http://nginx.org/download/nginx-1.12.2.tar.gz &> /dev/null && {
 44 tar zxf nginx-1.12.2.tar.gz && groupadd www && useradd -g www www -s /sbin/nologin
 45 }
 46 echo "Nginx包下载成功"
 47 cd $NGINX_ML && {
 48     ./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=www --group=www &> /dev/null && make &> /dev/null && make install &> /dev/null 
 49 }
 50  
 51 if [ $? -eq "0" ]
 52         then
 53                ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin ;nginx && echo "Nginx启动成功,请访问http://$ADDR:80"
 54         else
 55               echo "Nginx启动失败!!!"
 56               exit 1
 57 fi
 58  
 59  
 60 #Start installing MySQL5.7 61 echo "开始下载MySQL二进制安装包,请稍等..."
 62 if [ -z "$MAR" ]
 63                 then
 64                         echo "开始安装MySQL"
 65 fi
 66 if [ -n "$MAR" ]
 67                then
 68                         rpm -e mariadb-libs --nodeps && echo "卸载mariadb成功!正式安装MySQL"
 69 fi
 70  
 71 cd $ML || {
 72     echo "进入目录失败!!!"
 73     exit 1
 74 }
 75  
 76 #wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz &> /dev/null && {
 77 wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz && {
 78 tar zxf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  && mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
 79 cd /usr/local/mysql/ && mkdir data && mkdir log && echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
 80 source /etc/profile && groupadd mysql && useradd -r -g mysql -s /bin/false mysql
 81 }
 82 cat << EOF >/etc/my.cnf
 83 [client]  
 84 socket=/usr/local/mysql/mysql.sock  
 85 [mysqld]  
 86 basedir=/usr/local/mysql  
 87 datadir=/usr/local/mysql/data  
 88 pid-file=/usr/local/mysql/data/mysqld.pid  
 89 socket=/usr/local/mysql/mysql.sock  
 90 log_error=/usr/local/mysql/log/mysql.err
 91 EOF
 92 if [ ! -d /etc/my.cnf ]
 93                 then
 94                         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
 95                 else
 96                          echo "错误!!!"
 97 fi
 98 if [ $? -eq "0" ]
 99     then
100         echo "MySQL正式安装完成杀青!"
101     else
102         echo "MySQL安装失败!!!"
103         exit 1
104 fi
105 #PHP install sccessy
106 yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel &> /dev/null
107 if [ $? -eq 0 ]
108         then
109                 echo "依赖包安装成功!!!"
110         else
111                 echo "依赖包安装失败!!!"
112 fi
113 cd $ML || {
114         echo "进入目录失败!!!"
115         exit 1
116 }
117 wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz &> /dev/null  && {
118 tar zxf libmcrypt-2.5.7.tar.gz && cd libmcrypt-2.5.7/ && ./configure --prefix=/usr/local/libmcrypt &> /dev/null && make &> /dev/null && make install &> /dev/null
119 }
120 if [ $? -eq 0 ]
121         then
122                 echo "Libmcrypt安装成功!"
123         else
124                 echo "Libmcrypt-ERROR!"
125         exit 1
126 fi
127 # PHP install
128 cd $ML || {
129         echo "进入目录失败!!!"
130         exit 1
131 }
132 wget http://cn2.php.net/distributions/php-5.6.27.tar.gz &> /dev/null && {
133 tar zxf php-5.6.27.tar.gz && cd php-5.6.27/ &&  ./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts  &> /dev/null
134 }
135 if [ $? -eq 0 ]
136             then
137                  make &> /dev/null  && make install &> /dev/null  &&  echo "PHP编译安装成功,正在配置请稍等...."
138             else
139                  echo "PHP安装失败"
140 fi
141 cd $PML || {
142                 echo "进入目录失败!!!"
143                 exit 1
144 }
145 cp php.ini-production /etc/php.ini && {
146 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
147 }
148 if [ $? -eq 0 ]
149                         then
150                                 cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf && echo "复制主配置成功"
151                         else
152                                 echo "PHP权限失败,请重试!"
153 fi
154 if [ ! -d /usr/local/php5.6/etc/php-fpm.conf ]
155         then
156                 sed -i 's#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g' /usr/local/php5.6/etc/php-fpm.conf
157                 sed -i 's/listen = 127.0.0.1:9000/listen = 127.0.0.1:9000/g' /usr/local/php5.6/etc/php-fpm.conf
158                 sed -i 's/pm.max_children = 5/pm.max_children = 300/g' /usr/local/php5.6/etc/php-fpm.conf
159                 sed -i 's/pm.start_servers = 2/pm.start_servers = 20/g' /usr/local/php5.6/etc/php-fpm.conf
160                 sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 20/g' /usr/local/php5.6/etc/php-fpm.conf
161                 sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 100/g' /usr/local/php5.6/etc/php-fpm.conf
162         else
163                 echo "NONONO"
164 fi
165 if [ $? -eq 0 ]
166                         then
167                                 systemctl start php-fpm && echo "PHP启动成功"
168                         else
169                                 echo "启动PHP失败"
170 fi
171 #配置nginx解析php
172 if [ ! -d /usr/local/nginx/conf/nginx.conf ]
173         then
174                sed -i 's/index  index.html index.htm;/index  index.php index.html index.htm;/g' /usr/local/nginx/conf/nginx.conf
175                sed -i 's/#    root           html;/    root           html;/g' /usr/local/nginx/conf/nginx.conf
176                sed -i 's/#    fastcgi_pass   127.0.0.1:9000;/    fastcgi_pass   127.0.0.1:9000;/g' /usr/local/nginx/conf/nginx.conf
177                sed -i 's/#    fastcgi_index  index.php;/    fastcgi_index  index.php;/g' /usr/local/nginx/conf/nginx.conf
178                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
179                sed -i 's/#    include        fastcgi_params;/    include        fastcgi.conf;/g' /usr/local/nginx/conf/nginx.conf
180                sed -i '71d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;70a}' /usr/local/nginx/conf/nginx.conf
181                sed -i '65d' /usr/local/nginx/conf/nginx.conf && sed -i 'N;64alocation ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf
182         else
183                echo "解析PHP失败!!!"
184 fi
185 touch /usr/local/nginx/html/phptest.php
186 cat << EOF >/usr/local/nginx/html/phptest.php
187 <?php
188 phpinfo();
189 ?>
190 EOF
191 if [ ! -d /usr/local/nginx/html/phptest.php ]
192     then
193         nginx -s reload && systemctl restart php-fpm
194     else
195         echo "测试目录文件失败"
196 fi
197 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo  &> /dev/null && yum install -y sl &> /dev/null
198 sl
199 if [ $? -eq 0 ]
200         then
201                 echo "LNMP安装成功,请您访问http://$ADDR/phptest.php"
202         else
203                 echo "安装失败"
204 fi
205 if [ $? -eq 0 ]
206     then
207         echo "LNMP安装成功,请您访问http://$ADDR/phptest.php"
208     else
209         echo "安装失败"
210 fi
211 ###################################################################################################This is a lnmp-install~

以上为lnmp环境的搭建,不要准备包,全部在线下载,整个过程最多30分钟,这个只是基本的lnmp环境

如果说要安装zabbix4.0的话还需要满足他的先决条件,在这里我们一步步的来,遇到阻挡了再去安装。

zabbix4.0的先决条件先贴出来

Pre-requisite Minimum value Description
PHP version 5.4.0  
PHP memory_limit option 128MB In php.ini:
memory_limit = 128M
PHP post_max_size option 16MB In php.ini:
post_max_size = 16M
PHP upload_max_filesize option 2MB In php.ini:
upload_max_filesize = 2M
PHP max_execution_time option 300 seconds (values 0 and -1 are allowed) In php.ini:
max_execution_time = 300
PHP max_input_time option 300 seconds (values 0 and -1 are allowed) In php.ini:
max_input_time = 300
PHP session.auto_start option must be disabled In php.ini:
session.auto_start = 0
Database support One of: MySQL, Oracle, PostgreSQL, IBM DB2 One of the following modules must be installed:
mysql, oci8, pgsql, ibm_db2
bcmath   php-bcmath
mbstring   php-mbstring
PHP mbstring.func_overload option must be disabled In php.ini:
mbstring.func_overload = 0
PHP always_populate_raw_post_data option must be disabled Required only for PHP versions 5.6.0 or newer.
In php.ini:
always_populate_raw_post_data = -1
sockets   php-net-socket. Required for user script support.
gd 2.0 or higher php-gd. PHP GD extension must support PNG images (–with-png-dir), JPEG (–with-jpeg-dir) images and FreeType 2 (–with-freetype-dir).
libxml 2.6.15 php-xml or php5-dom
xmlwriter   php-xmlwriter
xmlreader   php-xmlreader
ctype   php-ctype
session   php-session
gettext   php-gettext
Since Zabbix 2.2.1, the PHP gettext extension is not a mandatory requirement for installing Zabbix. If gettext is not installed, the frontend will work as usual, however, the translations will not be available.

 下面开始安装zabbix4.0

软件包下载地址:https://www.zabbix.com/download_sources

解压软件包:

yum install -y net-snmp-devel
1 tar zxf zabbix-4.0.0.tar.gz
2 cd zabbix-4.0.0
3 ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2

在这里有可能会发生报错:

现象1

configure: error: Not found mysqlclient library

解决:将编译时的–with-mysql后面跟上数据库的安装路径

 

1 make && make install

 

到这里安装成功,下面开始配置各项配置文件

1 vim /usr/local/zabbix/etc/zabbix_server.conf
2 DBHost=localhost #默认注释掉了,直接取消注释即可
3 DBName=zabbix #数据库名字
4 DBUser=zabbix #默认是root,我们授权的用户是zabbix
5 DBPassword=zabbix #密码我们授权的也是zabbix

下面是数据库的导入与授权操作

1.先登录数据库

在这里也许你会不知道数据库该怎么登录,因为你并不知道数据库密码

初始密码获取方法:

1 cat /usr/local/mysql/log/mysql.err | grep password

黄色框里面的就是数据库的初始密码了

登录数据库以后必须更改初密码,否则无法进行建库等操作

更改密码:

1 use mysql;
2 update user set password=password('mysql') where user='root';
3 flush privileges;
4 create database zabbix;
5 grant all on zabbix.* to zabbix@'localhost' identified by 'zabbixmysql';
6 flush privileges;

接下来是导入数据库:

1 cd /usr/src/zabbix-4.0.0
2 mysql -uzabbix -pzabbixmysql zabbix < database/mysql/schema.sql
3 mysql -uzabbix -pzabbixmysql zabbix < database/mysql/images.sql
4 mysql -uzabbix -pzabbixmysql zabbix < database/mysql/data.sql

下面我们可以写一个数据库连通性的测试页面放到nginx的html目录下:

1 vim /usr/local/nginx/html/mysqltest.php
1 <?php
2 $link=mysql_connect('localhost','zabbix','zabbix');
3 if($link) echo "<h1>The database connection was successful!</h1>";
4 else echo "<h1>The database connection was Fail!!</h1>";
5 mysql_close();
6 ?>

浏览器访问nginx的IP后面加上测试文件文件名出先下面内容则代表连接成功:

接下来就是进入zabbix的web界面了,这时候需要把前端文件copy到nginx的HTML目录下

1 mkdir /usr/local/nginx/html/zabbix
2 cd /usr/src/zabbix-4.0.0/frontends/php/
3 cp * /usr/local/nginx/html/zabbix/

进入web界面:http://ip/zabbix/

下面是安装过程遇到的问题

提示不满足zabbix的先决条件

根据提示去更改php.ini的相关数值,参考上面的先决条件

在这个过程可能会涉及到php模块的安装,可以参考php扩展模块的安装方法安装就能完成

 

版权声明:本文为hw226234原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hw226234/p/9777584.html