一、搭建nginx
1)基础依赖包安装
yum -y install gcc gcc-c++ vim tree make cmake autoconf yum -y install openssl openssl-devel openssl-clients curl curl-devel \ yum -y install wget rsync expect readline readline-devel bison bison-devel \ yum -y install pcre pcre-devel zlib-devel zlib freetype freetype-devel man \ yum -y install lrzsz tar iostat bc zip unzip lvm2 sysstat
2)nginx环境准备
useradd nginx -s /sbin/nologin -M wget http://nginx.org/download/nginx-1.9.9.tar.gz tar xf nginx-1.9.9.tar.gz cd nginx-1.9.9
问题处理
./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries. [root@web-1 nginx-1.9.9]# yum -y install gd-devel
yum -y install openssl openssl-devel
3)编译参数。指定目录 /opt/lnmp_zabbix/nginx
./configure \ --prefix=/opt/lnmp_zabbix/nginx \ --user=nginx \ --group=nginx \ --conf-path=/opt/lnmp_zabbix/nginx/conf/nginx.conf \ --error-log-path=/opt/lnmp_zabbix/nginx/log/error.log \ --http-log-path=/opt/lnmp_zabbix/nginx/log/access.log \ --http-client-body-temp-path=/opt/lnmp_zabbix/nginx/client/ \ --http-proxy-temp-path=/opt/lnmp_zabbix/nginx/proxy/ \ --http-fastcgi-temp-path=/opt/lnmp_zabbix/nginx/fcgi/ \ --http-uwsgi-temp-path=/opt/lnmp_zabbix/nginx/uwsgi \ --http-scgi-temp-path=/opt/lnmp_zabbix/nginx/scgi --with-pcre \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_realip_module \ --pid-path=/opt/lnmp_zabbix/nginx/nginx.pid \ --with-file-aio --with-http_image_filter_module \ --with-stream # tcp 代理 # --with-pcre=/opt/lnmp_zabbix/pcre-8.35 \ # --with-zlib=/opt/lnmp_zabbix/zlib-1.2.8 \ # --with-openssl=/opt/lnmp_zabbix/openssl-1.0.1i
3)编译成功则执行
make && make install
二、编译安装mysql(方法一)
1) 下载mysql,创建mysql用户
groupadd mysql useradd -r -g mysql mysql -s /sbin/nologin wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.41.tar.gz tar xf mysql-5.6.41.tar.gz cd mysql-5.6.41
2) 执行编译过程
cmake \ -DCMAKE_INSTALL_PREFIX=/opt/lnmp_zabbix/mysql \ -DMYSQL_DATADIR=/opt/lnmp_zabbix/mysql/data \ -DMYSQL_UNIX_ADDR=/opt/lnmp_zabbix/mysql.sock \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_FEDERATED_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3306 \ -DSYSCONFDIR=/etc \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_SSL=system \ -DINSTALL_SHAREDIR=share make && make install
3)查看编译后的文件
[root@zabbix mysql]# ls /opt/lnmp_zabbix/mysql/ bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files [root@zabbix mysql]# ll -sh /opt/lnmp_zabbix/mysql/ total 68K 4.0K drwxr-xr-x 2 root root 4.0K Nov 21 23:33 bin 20K -rw-r--r-- 1 root root 18K Jun 15 21:03 COPYING 4.0K drwxr-xr-x 3 root root 4.0K Nov 21 23:33 data 4.0K drwxr-xr-x 2 root root 4.0K Nov 21 23:33 docs 4.0K drwxr-xr-x 3 root root 4.0K Nov 21 23:33 include 4.0K drwxr-xr-x 3 root root 4.0K Nov 21 23:33 lib 4.0K drwxr-xr-x 4 root root 4.0K Nov 21 23:33 man 4.0K drwxr-xr-x 10 root root 4.0K Nov 21 23:33 mysql-test 4.0K -rw-r--r-- 1 root root 2.5K Jun 15 21:03 README 4.0K drwxr-xr-x 2 root root 4.0K Nov 21 23:33 scripts 4.0K drwxr-xr-x 28 root root 4.0K Nov 21 23:33 share 4.0K drwxr-xr-x 4 root root 4.0K Nov 21 23:33 sql-bench 4.0K drwxr-xr-x 2 root root 4.0K Nov 21 23:33 support-files
4)myql的配置文件(后续操作,为了移植程序包)
[root@zabbix mysql]# cp /opt/lnmp_zabbix/mysql/support-files/my-default.cnf /etc/my.cnf 创建关键行目录 mkdir -p /opt/lnmp_zabbix/mysql/data mkdir -p /opt/lnmp_zabbix/mysql/logs cd /opt/lnmp_zabbix/ touch logs/error.log chown -R mysql:mysql /opt/lnmp_zabbix/mysql/data/mysql # 可能有错
5)初始话mysql
/opt/lnmp_zabbix/mysql/scripts/mysql_install_db --user=mysql --datadir=/opt/lnmp_zabbix/mysql/data/
6)设置开机自启动加入目录
cp support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld start # 设置 ROOT 密码 ./bin/mysqladmin -u root password 123456
三、直接使用安装好的mysql文件
1)上传mysql压缩包文件到/usr/local/
[root@mysqltest local]# ls /usr/local/mysql*
/usr/local/mysql.20170704.152.tgz
2、检查是否有/usr/bin/perl 这个文件
[root@mysqltest opt]# ll /usr/bin/perl -rwxr-xr-x. 2 root root 7184 Nov 23 2013 /usr/bin/perl 如果没有执行安装命令 yum install -y perl perl-devel
3)在/usr/local/下 创建mysql启动脚本文件。mysqld_init.sh
cat mysqld_init.sh tar xf /usr/local/mysql.20170704.152.tgz useradd mysql -s /sbin/nologin -M ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql mkdir -p /data/mysql_data/tmp/ chown -R mysql.mysql /data/mysql_data/ mkdir -p /data/mysql_binlog/ chown -R mysql.mysql /data/mysql_binlog/ mv /etc/my.cnf /etc/my.cnf.bak cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf mkdir -p /usr/local/mysql/logs touch /usr/local/mysql/logs/error.log /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql_data --user=mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on service mysqld start /usr/local/mysql/bin/mysqladmin -u root password 123456
4)执行启动脚本文件 /bin/bash usr/local/mysqld_init.sh
5) 测试
4、此时mysql已经启动起来了,可以检查端口测试 进入mysql ==》mysql -uroot -p 123456
四、编译安装php文件
依赖包安装
yum install libxml2 libxml2-devel -y
yum install bzip2 bzip2-devel
yum -y install libxslt libxslt-devel
yum -y install libcurl libcurl-devel
yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y
wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
tar -zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure --prefix=/usr/local
make
make install
1)下载源码包
wget http://cn2.php.net/distributions/php-5.6.24.tar.gz tar xf php-5.6.24.tar.gz
cd php-5.6.24
2)为了make,php时避免报错。检查,
lrwxrwxrwx 1 root root 41 Nov 24 11:58 /usr/lib/libmysqlclient.so.18 -> /usr/local/mysql/lib/libmysqlclient.so.18
php.make时会报该错误 error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory 解决问题1 [root@zabbix php-5.6.24]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib [root@zabbix php-5.6.24]# ldconfig
3) 编译参数
./configure \ --prefix=/opt/lnmp_zabbix/php \ --with-config-file-path=/opt/lnmp_zabbix/php/etc \ --with-mysql=/usr/local/mysql \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-iconv-dir=/opt/lnmp_zabbix/libiconv \ --with-freetype-dir \ --with-bz2 \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir=/usr \ --with-xmlwriter-dir=/usr \ --with-xmlreader-dir=/usr \ --with-libdir=lib \ --with-gettext \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-fpm \ --enable-mbstring \ --with-mcrypt \ --with-gd \ --enable-gd-native-ttf \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --enable-short-tags \ --enable-static \ --with-xsl \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --enable-ftp \ --enable-opcache=no
4) 执行成功后 make && make install
5) 启动
cp /opt/lnmp_zabbix/php/etc/php-fpm.conf.default /opt/lnmp_zabbix/php/etc/php-fpm.conf [root@zabbix php]# /opt/lnmp_zabbix/php/sbin/php-fpm [root@zabbix sbin]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1254/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1358/master tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 122190/php-fpm tcp 0 0 :::22 :::* LISTEN 1254/sshd tcp 0 0 ::1:25 :::* LISTEN 1358/master tcp 0 0 :::3306 :::* LISTEN 12836/mysqld
五、编译安装zabbix
1)下载zabbix3以上版本
yum install -y net-snmp-devel groupadd -g 201 zabbix useradd -g zabbix -u 201 -m zabbix https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.5/zabbix-3.2.5.tar.gz tar xf zabbix-3.2.5.tar.gz cd zabbix-3.2.5.tar.gz
2) 编译安装
./configure \ --prefix=/usr/local/zabbix \ --sysconfdir=/etc/zabbix/ \ --enable-server \ --enable-agent \ --with-net-snmp \ --with-libcurl \ --with-mysql=/usr/local/mysql/bin/mysql_config \ --with-libxml2 make && make install
3)配置zabbix
3.1)建立mysql的数据库
mysql -uroot -p mysql> CREATE DATABASE zabbix CHARACTER SET utf8 COLLATE utf8_bin; mysql> GRANT ALL PRIVILEGES ON zabbix.* TO zabbix@localhost IDENTIFIED BY 'zabbix'; mysql> flush privileges; mysql> show databases;
3.2)导入zabbix的库
cd zabbix-3.2.5 /usr/bin/mysql -uzabbix -pzabbix zabbix < database/mysql/schema.sql /usr/bin/mysql -uzabbix -pzabbix zabbix < database/mysql/images.sql /usr/bin/mysql -uzabbix -pzabbix zabbix < database/mysql/data.sql
3.3)配置zabbix的配置文件
vim /etc/zabbix/zabbix_server.conf DBHost=localhost 数据库ip地址 DBName=zabbix DBUser=zabbix DBPassword=zabbix ListenIP=127.0.0.1,192.168.1.10 zabbix server ip地址
3.4)设置zabbix的启动文件
设置防火墙(线上先检查防火墙再执行) # iptables -I INPUT -p tcp -m multiport --dports 10050:10051 -j ACCEPT # service iptables save &&service iptables restart 做命令的软链接 ln -s /usr/local/zabbix/sbin/* /usr/sbin/ cp misc/init.d/fedora/core/zabbix_* /etc/init.d/ \\zabbix目录 chmod +x /etc/init.d/zabbix_* sed -i "s@BASEDIR=/usr/local@BASEDIR=/usr/local/zabbix@g" /etc/init.d/zabbix_server \\写入系统服务
六、配置web服务
1)修改nginx的启动脚本文件
[root@zabbix conf]# cat /opt/lnmp_zabbix/nginx/conf/nginx.conf user nginx; worker_processes 4; worker_cpu_affinity 00000001 00000010 00000100 00001000; worker_rlimit_nofile 204800; pid /var/run/nginx.pid; events { worker_connections 204800; use epoll; multi_accept off; } http { include /opt/lnmp_zabbix/nginx/conf/mime.types; default_type application/octet-stream; log_format main '$remote_addr --- $remote_user --- [$time_local] --- $request --- ' '"$status" --- $body_bytes_sent --- "$http_referer" --- ' '"$http_user_agent" --- "$http_x_forwarded_for"'; log_format mtr '$remote_addr [$time_local] "$request_uri" ' '$status "$http_referer" ' '"$http_user_agent" "$host"'; sendfile on; keepalive_timeout 30; client_header_timeout 30; client_body_timeout 40; server_tokens off; tcp_nodelay on; gzip on; include /opt/lnmp_zabbix/nginx/conf/vhost/*.conf; fastcgi_send_timeout 300; fastcgi_read_timeout 300; #fastcgi_buffer_size 16k; #fastcgi_buffers 16 16k; #fastcgi_busy_buffers_size 16k; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; server_names_hash_bucket_size 128; client_header_buffer_size 2k; large_client_header_buffers 4 4k; client_max_body_size 100k; open_file_cache max=51200 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 1; }
配置vhost/zabbix.conf
[root@zabbix conf]# cat /opt/lnmp_zabbix/nginx/conf/vhost/zabbix.conf server { listen 89; charset utf8; server_name localhost; access_log /opt/lnmp_zabbix/nginx/log/zabbix.log main; error_log /opt/lnmp_zabbix/nginx/log/error.log; index index.html index.php index.html; root /opt/wwwroot/zabbix; location /{ try_files $uri $uri/ /index.php?$args; } location ~ ^(.+.php)(.*)$ { fastcgi_split_path_info ^(.+.php)(.*)$; include fastcgi.conf; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param PATH_INFO $fastcgi_path_info; } }
2)拷贝项目目录到/opt/wwwroot/zabbix
cd /root/tools/zabbix-3.2.5 cp -r frontends/php/* /opt/wwwroot/zabbix/
3) 重启nginx,php,mysql,zabbix
打开网页,说明成功了一大半了
但配置页面由于php的原因,检查一些设置会失败
七、配置php
1)设置php的配置文件,php.ini
cp /root/tools/php-5.6.24/php.ini-production /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/post_max_size = 8M/post_max_size = 16M/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/max_execution_time = 30/max_execution_time = 300/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/max_input_time = 60/max_input_time = 300/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/mysqli.default_socket =/& \/home\/mysql\/var\/mysql.sock/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/mysqli.default_host =/& localhost/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/mysqli.default_user =/& zabbix/' /opt/lnmp_zabbix/php/etc/php.ini sed -i 's/;always_populate_raw_post_data = -1/always_populate_raw_post_data = -1/' /opt/lnmp_zabbix/php/etc/php.ini
2)写php开机自启动的脚本文件 vim /etc/init.d/php-fpm
[root@zabbix sbin]# cat /etc/init.d/php-fpm #!/bin/sh # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 phpfpm="/opt/lnmp_zabbix/php/sbin/php-fpm" prog=$(basename ${phpfpm}) lockfile=/var/lock/subsys/phpfpm start() { [ -x ${phpfpm} ] || exit 5 echo -n $"Starting $prog: " daemon ${phpfpm} retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc ${phpfpm} -HUP RETVAL=$? echo } force_reload() { restart } configtest() { ${phpfpm} -t } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; status) rh_status ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|configtest}" exit 2 esac
chmod +x /etc/init.d/php-fpm
[root@zabbix sbin]# /etc/init.d/php-fpm stop Stopping php-fpm: [ OK ] [root@zabbix sbin]# /etc/init.d/php-fpm start Starting php-fpm: [ OK ]
3)再次访问http://192.168.10.45:89/setup.php ,可进入下一步系统配置
Admin/zabbix
4)zabbix客户端配置
[root@k8s5 ~]# egrep -v "^#|^$" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=192.168.10.45 Hostname=Zabbix server Include=/etc/zabbix/zabbix_agentd.d/*.conf
测试: /usr/local/zabbix/bin/zabbix_get -s 10.0.5.21 -p 10050 -k "system.uptime"
yum -y localinstall *.rpm 安装当前文件夹文件
八、zabbix常见问题归纳
1)zabbix-agent 不能创建进程id
zabbix_agentd [1023]: cannot create PID file [/var/run/zabbix/zabbix_agentd.pid]: [2] No such file or directory [root@k8s5 ~]# ll /var/run/zabbix/ ls: 无法访问/var/run/zabbix/: 没有那个文件或目录 [root@k8s5 ~]# mkdir -p /var/run/zabbix [root@k8s5 ~]# touch /var/run/zabbix/zabbix_agentd.pid 再给权限 chown zabbix:zabbix /var/run/zabbix/
九、报警信息
注意:zabbix模板的默认监控里面 :
1 min average per core " 是每个core 的平均值,不是CPU 的平均值。 一个cpu可以有多个 core
即监控的数值需要乘以核数。