Copy #!/bin/bash
. /etc/init.d/functions
# Configure yum repository
# add zabbix yum
rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com/#https://mirrors.aliyun.com/zabbix/#g' /etc/yum.repos.d/zabbix.repo
# add mysql5.7 yum
cat > /etc/yum.repos.d/mysql57-community-el7-tsinghua.repo << EOF
[mysql57-community]
name=MySQL 5.7 Community Server Tsinghua
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/
enabled=1
gpgcheck=1
gpgkey=http://repo.mysql.com/RPM-GPG-KEY-mysql
EOF
# rebuild yum
yum clean all
yum makecache
# install httpd
yum install httpd -y
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
# install mysqld
yum install mysql-community-server -y
systemctl start mysqld
systemctl enable mysqld
temporary_password=`grep "temporary password" /var/log/mysqld.log | awk '{print $NF}'`
rpm -qa | grep expect > /dev/null 2>&1
if [ $? -ne 0 ]; then
yum install expect -y > /dev/null 2>&1
fi
/usr/bin/expect << EOF
spawn mysql_secure_installation
expect "Enter password for user root:"
send "${temporary_password}\r"
expect "New password:"
send "NewPass0.0\r"
expect "Re-enter new password:"
send "NewPass0.0\r"
expect "Change the password for root ? ((Press y|Y for Yes, any other key for No) :"
send "n\r"
expect "Remove anonymous users? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Disallow root login remotely? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Remove test database and access to it? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect "Reload privilege tables now? (Press y|Y for Yes, any other key for No) :"
send "y\r"
expect eof
EOF
firewall-cmd --permanent --add-service=mysql
firewall-cmd --reload
# install Zabbix
yum install zabbix-server-mysql zabbix-web-mysql -y
# 创建及配置Zabbix MySQL数据库
mysql -u root -pNewPass0.0 -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -u root -pNewPass0.0 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'NewPass0.0';"
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pNewPass0.0 zabbix
# 修改zabbix配置文件
sed -i 's/# DBHost=localhost/DBHost=localhost/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBPassword=/DBPassword=NewPass0.0/g' /etc/zabbix/zabbix_server.conf
# 配置httpd zabbix时区
sed -i 's#\# php_value date.timezone Europe/Riga#php_value date.timezone Asia/Shanghai#g' /etc/httpd/conf.d/zabbix.conf
# Configure SELinux
setsebool -P httpd_can_connect_zabbix on
# If the database is accessible over network (including 'localhost' in case of PostgreSQL), you need to allow Zabbix frontend to connect to the database too:
setsebool -P httpd_can_network_connect_db on
systemctl restart httpd
systemctl start zabbix-server
systemctl enable zabbix-server
ausearch -c 'zabbix_server* denied' --raw | audit2allow -M my-zabbixserver
# grep zabbix-server /var/log/audit/audit.log | audit2allow -M my-zabbixserver
semodule -i my-zabbixserver.pp
# 重启服务
systemctl restart zabbix-server