Zabbix&LNMP installation configuration

Keywords: Zabbix Nginx PHP MySQL

Monitoring terminal operation

#!/bin/bash
#Install zabbix+LNMP
#
lnmp(){

#Turn off firewall & core security features
systemctl stop firewalld.service
systemctl disable firewalld.service &> /dev/null
setenforce 0
sed -i "7cSELINUX=disabled" /etc/sysconfig/selinux

#yum install nginx
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

#Create nginx installation source (centos can be replaced according to different systems, and the later versions are the same)
echo "[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=0
enabled=1" > /etc/yum.repos.d/nginx.repo
yum install nginx -y

#start nginx
systemctl start nginx
systemctl enable nginx

#Install mysql 5.7 and start
yum install -y mariadb-server mariadb expect
systemctl enable mariadb.service
systemctl start mariadb.service

#Set database initial password
/usr/bin/expect <<EOF
spawn mysqladmin -u root -p password 123123 
expect {
    "Enter*"
    {send "\r"}
}
expect eof
EOF

#Install PHP 7.2
yum -y install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
for ((i=1;i>0;i++));do
rpm -q php72w-cli &> /dev/null
if [ $? -ne 0 ];then
    yum install -y php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql
else
    break
fi
done

#Modify fpm module to support nginx
sed -i -e "8cuser = nginx" -e "10cgroup = nginx" /etc/php-fpm.d/www.conf

#Change nginx configuration file to recognize php dynamic file
sed -i "10s/index.html/index.html index.php/" /etc/nginx/conf.d/default.conf
sed -i -e "30,36s/#//" -e "31s/html/\/usr\/share\/nginx\/html/" -e "34s/\/scripts/\$document_root/" /etc/nginx/conf.d/default.conf

#Configure PHP
sed -i -e "359s/On/Off/" -e "202s/Off/On/" /etc/php.ini

#Restart service
systemctl start php-fpm
systemctl enable php-fpm
systemctl restart nginx
}

#Check if LNMP schema has been installed
systemctl restart nginx &> /dev/null
service mysqld restart &> /dev/null
systemctl restart mariadb &> /dev/null
netstat -atnp | egrep '(nginx|3306)' &> /dev/null
if [ $? -ne 0 ];then
    lnmp
fi

#Configure PHP to accommodate zabbix
sed -i -e "368s/30/300/" -e "378s/60/300/" -e "656s/8/16/" -e "799aalways_populate_raw_post_data = -1" -e "877cdate.timezone = Asia/Shanghai" /etc/php.ini
systemctl restart php-fpm

#Building zabbix database and managing users
mysql  -uroot -p123123 -e "CREATE DATABASE zabbix character set utf8 collate utf8_bin;"
mysql  -uroot -p123123 -e "GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY '123123';"
mysql  -uroot -p123123 -e "flush privileges;"

#Solve the problem of local login failure
mysql -uroot -p123123 -e "drop user ''@localhost;"
mysql -uroot -p123123 -e "drop user ''@localhost.localdomain;"
mysql -uroot -p123123 -e "flush privileges"  

#Install zabbix
rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
for ((k=1;k>0;k++));do
    rpm -q zabbix-agent &> /dev/null
    if [ $? -ne 0 ];then
        yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
    else
        break
    fi
done

#Generate database file
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123123 zabbix

#Modify zabbix server configuration file
sed -i -e "91s/# //" -e "125cDBPassword=123123" /etc/zabbix/zabbix_server.conf

#Working directory authorization
cp -r /usr/share/zabbix/ /usr/share/nginx/html/
chown -R zabbix:zabbix /etc/zabbix/
chown -R zabbix:zabbix /usr/share/nginx/
chown -R zabbix:zabbix /usr/lib/zabbix/
chmod -R 755 /etc/zabbix/web/
chmod -R 777 /var/lib/php/session/

#Start zabbix
systemctl start zabbix-server.service
systemctl enable zabbix-server.service
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service

#Restart all services
systemctl restart php-fpm nginx mariadb zabbix-server zabbix-agent

#Check whether all services are started
test=`netstat -atnp | egrep '(nginx|3306|10051|10050)' | grep -v "TIME_WAIT" | wc -l`
if [ $test -ge 5 ];then
    echo "zabbix Server settings complete"
fi

dizhi=`ifconfig ens33 | awk 'NR==2{print $2}'`
echo "Please log in with your browser ${dizhi}/zabbix/Installation
//The default login user name is: Admin
//The default login password is: zabbix“

Log in to the monitoring platform


Configure agent

The agent is the controlled server. If the server needs to be monitored by itself, it also needs to install abbix agent
#Turn off the firewall and set it to turn on and off automatically
systemctl stop firewalld.service 
systemctl disable firewalld.service 

#Install yum source
rpm -ivh http://repo.zabbix.com/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm

#Install ZABBIX agent
yum install -y zabbix-agent

#Modify profile
grep -n '^'[a-Z] /etc/zabbix/zabbix_agentd.conf

13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=127.0.0.1
139:ServerActive=127.0.0.1
150:Hostname=Zabbix server
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf

vim /etc/zabbix/zabbix_agentd.conf

Server=192.168.142.123          
#Line 98, pointing to monitoring server IP
ServerActive=192.168.142.123        
#Line 139, pointing to monitoring server IP
Hostname=czt                    
#150 lines, host name can be defined by yourself

#Startup service
systemctl enable zabbix-agent.service
systemctl start zabbix-agent.service

#View service listening port
netstat -anpt | grep zabbix

Add the controlled host on the WEB platform to facilitate the identification of the monitoring end

Visit http://192.168.142.123/zabbix/ configuration host create host:

  • Under configuration, click Create host

  • Fill in the corresponding content in the host configuration interface, and then click the template

  • Select the template links of HTTP and SSH in the template, click add in the prompt, and then click Add

  • Newly added monitoring items

Thank you for reading!

Posted by ccl on Fri, 07 Feb 2020 09:23:31 -0800