LNMP架构部署zabbix监控
- 环境:CentOS7
- VMware虚拟机
一:LNMP之Nginx服务器编译安装
1.安装nginx依赖:
yum -y install wget gcc gcc-c++ make pcre pcre-devel zlib-devel zlib openssl openssl-devel
2.上传nginx压缩包
/usr/local/src
3.解压nginx
tar -zxvf nginx-1.6.0.tar.gz
4.进入解压后的目录下
cd nginx-1.6.0/
5.安装configure到/usr/local/nginx
./configure --prefix=/usr/local/nginx #先进行检查是否符合安装环境
6.执行安装
make && make install
二:手动启动和关闭nginx
1.改环境变量:
vi /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin/ #加入到配置文件最后,可直接使用nginx -v查看nginx版本信息
source /etc/profile #使配置文件生效
2.nginx配置文件
nginx -t
#回显以下内容则证明nginx配置文件正常
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax
is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test
is successful
3.启动nginx
nginx
4.检查nginx进程
ps auxfww | grep nginx
5.检查nginx监听端口80
netstat -tulnp|grep nginx
6.验证nginx是否安装成功:
1.浏览器访问IP+端口,出现welcometo nginx页面则证明nginx安装成功
三,使用systemctl管理nginx,/usr/lib/systemd/system/nginx.service
#nginx.service中的nginx为服务类型,若使用php服务咋改为php.service
1.pkill nginx #杀掉nginx进程
2. vi /usr/lib/systemd/system/nginx.service
内容:
[Unit]
Description=nginx #代表了服务
After=network.target
[Service]
Type=fprking
ExecStart=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target
3.启动nginx
systemctl start nginx
四:LNMP之PHP源码编译安装
1.安装php所需的依赖包
yum -y install epel-release
yum -y install gcc gcc-c++ make pcre pcre-devel zlib-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel
2.上传压缩包
cd /usr/local/src
tar -zxf php-8.1.0.tar.gz
cd php-8.1.0/
3.PHP安装环境编译
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=wwww --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd-compression-support --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mastring --enable-intl --with-mcrypt --with-libmbfl --enable-ftp --with-gd --enable-gd-jis-conv --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext -disable-fileinfo --enable-opcache --with-pear --enable-maintainer-zts --with-ldap=shared --without-gdbm
4.编译安装
make && make install
5.cp php.ini-production /usr/local/php/etc/php.ini
6.启动PHP-fpm
01.修改环境变量:
vi /etc/profile
export PATH=$PATH:/usr/local/php/sbin/:/usr/local/php/bin/ #写在文件最后
02.使其配置文件生效
source /etc/profile
03. 查看php版本信息
php -v
[root@localhost php-8.1.0]# php -v PHP 5.4.16 (cli) (built: Apr 1
2020 04:07:17) Copyright © 1997-2013 The PHP Group Zend Engine
v2.4.0, Copyright © 1998-2013 Zend Technologies
php-fpm -t #检查配置文件,会出现一个报错。
报错信息:
[root@localhost ~]# php-fpm -t [13-Dec-2021 04:03:22] ERROR: failed to
open configuration file ‘/usr/local/php/etc/php-fpm.conf’: No such
file or directory (2) [13-Dec-2021 04:03:22] ERROR: failed to load
configuration file ‘/usr/local/php/etc/php-fpm.conf’ [13-Dec-2021
04:03:22] ERROR: FPM initialization failed [root@localhost ~]#
解决方法:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
4.启动php:
vi /usr/lib/systemd/system/php-fpm.service
添加以下内容:
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=fprking
ExecStart=/usr/local/php/sbin/php-fpm
[Install]
WantedBy=multi-user.target
启动php-fpm
systemctl start php-fpm
5.查询PHP监听端口
netstat -tulnp|grep php
6.查询PHP启动进程
ps auxfww | grep php
启动报错信息以及解决方法
报错1:
[22-Dec-2021 12:01:47] ERROR: [pool www] cannot get uid for user
‘wwww’ [22-Dec-2021 12:01:47] ERROR: FPM initialization failed
解决方法:useradd www #没有这个用户,添加上这个用户即可
报错2:
[22-Dec-2021 12:01:47] ERROR: [pool www] cannot get uid for user
‘wwww’ [22-Dec-2021 12:01:47] ERROR: FPM initialization failed
解决方法: useradd wwww
编译报错信息以及解决方法
报错1:
checking for sqlite3 >= 3.7.7… no configure: error: Package
requirements (sqlite3 >= 3.7.7) were not met:No package ‘sqlite3’ found
Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.Alternatively, you may set the environment variables SQLITE_CFLAGS and
SQLITE_LIBS to avoid the need to call pkg-config. See the pkg-config
man page for more details.
解决方法; yum -y install sqlite-devel
报错2:
configure: error: Package requirements (icu-uc >= 50.1 icu-io
icu-i18n) were not met:No package ‘icu-uc’ found No package ‘icu-io’ found No package
‘icu-i18n’ foundConsider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.Alternatively, you may set the environment variables ICU_CFLAGS and
ICU_LIBS to avoid the need to call pkg-config. See the pkg-config man
page for more details.
解决方法:yum -y install libicu-devel
报错3:
configure: error: Cannot find ldap libraries in /usr/lib.
解决方法:cp -frp /usr/lib64/libldap* /usr/lib/
编译成功的回显:
±-------------------------------------------------------------------+ | License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
±-------------------------------------------------------------------+Thank you for using PHP.
测试PHP是否安装成功:
php --version
五:nginx默认无法使用php需要进行配置修改:
修改nginx.conf文件
cd /usr/local/nginx/conf
vi nginx.conf
1.添加index.php:
location / {
root html;
index index.html index.htm index.php;
}
2.取消注释,并进行修改:
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME d o c u m e n t r o o t document_root documentrootfastcgi_script_name;
include fastcgi_params;
}
3.检查配置文件是否正确
nginx -t
六:LNMP之MySQL安装
1.安装mysql依赖包
yum -y install perl perl-devel autoconf libaio
2.上传MySQL安装包
cd /usr/local/src/
3.解压安装包
tar zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
4.复制解压后的mysql目录到系统的本地软件目录
cp mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql -r
5、添加系统mysql组和mysql用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
6.在/usr/local/mysql目录下创建data目录
mkdir /usr/local/mysql/data
7.更改mysql目录下所有的目录及文件夹所属的用户组和用户,以及权限
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
8.编译安装并初始化mysql
务必记住初始化输出日志末尾的密码(数据库管理员临时密码)*
cd /usr/local/mysql/bin
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
临时密码:sOzjjli7gv,2
9.补充说明
编辑配置文件my.cnf,添加配置如下
vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
symbolic-links=0
max_connections=400
innodb_file_per_table=1
lower_case_table_names=1
character_set_server=utf8
lower_case_table_names:是否区分大小写,1表示存储时表名为小写,操作时不区分大小写;0表示区分大小写;不能动态设置,修改后,必须重启才能生效:
character_set_server:设置数据库默认字符集,如果不设置默认为latin1
innodb_file_per_table:是否将每个表的数据单独存储,1表示单独存储;0表示关闭独立表空间,可以通过查看数据目录,查看文件结构的区别;
10.启动数据库
/usr/local/mysql/support-files/mysql.server start
11.添加软连接,并重启mysql服务
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
12.启动数据库
service mysql restart
13.查看数据库状态
service mysqld status
14.登录数据库,修改临时密码
mysql -uroot -psOzjjli7gv,2 #临时密码是之前生成的默认密码
set password for root@localhost = password(‘123456’); #新密码为123456
连接数据库时报错
root@iZufkfljcZ:~# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
解决方法:
1.查找mysql.sock文件位置
find / -name mysql.sock
2.如果可以查到位置,只需要做软连接到/tmp/下即可
例如:ln -s /ver/lib/mysql/mysql.sock /tmp/
七:zabbix编译安装
zabbix server编译安装
1.安装zabbix server所需要的依赖包
yum -y install libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel
2.创建zabbix所需要的用户
useradd -s /sbin/nologin zabbix
3.上传源码包
cd /usr/local/src/
tar -zxvf zabbix-5.4.9.tar.gz
4.切换至zabbix安装路径下
cd zabbix-5.4.9
5.编译zabbix安装环境
./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml2
选项说明
-
--prefix指定安装目录
-
--enable-server安装zabbix server
-
--enable-agent安装zabbix agent
-
--with-mysql用mysql来存储
编译时报错
错误1: configure: error: MySQL library not found
解决方式是安装 mysql-devel yum
install mysql-devel -y
错误2 configure: error: Invalid Net-SNMP directory - unable to find
net-snmp-config
解决办法是安装net-snmp-devel
yum install -y net-snmp-devel
6.安装zabbix
make && make install
报错
报错信息:
…/…/src/libs/zbxipcservice/libzbxipcservice.a(libzbxipcservice_a-ipcservice.o):在函数’zbx_ipc_service_start’中/usr/local/src/zabbix-5.4.9/src/libs/zbxipcservice/ipcservice.c:1585:对’event_base_new’未定义的引用
…/…/src/libs/zbxipcservice/libzbxipcservice.a(libzbxipcservice_a-ipcservice.o):在函数’zbx_ipc_service_start’中/usr/local/src/zabbix-5.4.9/src/libs/zbxipcservice/ipcservice.c:1865:对’event_base_new’未定义的引用
解决方法;报错属于依赖不全,缺少libevent-devel
7.安装完成后查看一下路径下的目录
cd /usr/local/zabbix3.4.15/
8.环境变量设置:
vim /etc/profile
export PATH=$PATH:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/
source /etc/profile ###立即生效
9.查看版本信息
zabbix_server --version
zabbix server的启动
1.需要到mysql做数据库初始化
2.配置zabbix server——配置连接mysql的用户名密码
3.启动zabbix server
1.登录数据库
mysql -u root -p
create database zabbix character set utf8 collate utf8_bin;#新建数据库
grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'zabbixpwd';#创建zabbix用户,并允许通过127.0.0.1进行访问,密码为zabbixpwwd
flush privileges;
set names utf8;#将数据库用户编码为utf8
2.使用新用户进行数据库登录验证
mysql -h127.0.0.1 -u zabbix -p
报错
Access deied for user ‘zabbix’@‘localhost’ (using pwassword: YES)
解决方法:
使用drop user zabbix@localhost;删除1个zabbix用户即可,原因是zabbix用户重复
3.使用zabbix用户进行数据库操作
use zabbix;#切换zabbix用户
/数据库初始化操作
source /usr/local/src/zabbix-4.0.3/database/mysql/schema.sql;
source /usr/local/src/zabbix-4.0.3/database/mysql/data.sql;
source /usr/local/src/zabbix-4.0.3/database/mysql/images.sql;
4.zabbix_server的配置/usr/local/zabbix/etc/zabbix_server.conf
vim /usr/local/zabbix/etc/zabbix_server.conf
配置文件清空,直接写入以下内容:
LogFile=/usr/local/zabbix/zabbix_server.log #log文件位置
DBHost=127.0.0.1 #连接mysql的ip地址
DBName=zabbix #数据库的名字
DBUser=zabbix #数据库用户
DBPassword=zabbixpwd #数据库用户密码
DBPort=3306 #数据库端口号
Timeout=30
AlertScriptsPath=/usr/local/zabbix/alertscripts #配置邮件告警、微信告警等的脚本位置
ExternalScripts=/usr/local/zabbix/externalscripts
LogSlowQueries=3000 #慢查询
5.启动zabbix server
chown zabbix:zabbix -R /usr/local/zabbix
zabbix_server
6.验证
ps auxf | grep zabbix_server #查询zabbix_server进程
zabbix登录
http://ip/zabbix/setup.php