Shell 脚本攻略 | LNMP+Zabbix 一建部署

文章目录

准备环境

此脚本基于我之前写过一篇博客:基于 LNMP 部署 Zabbix 集中监控系统


脚本如下

#!/bin/bash

#----开局优化----
systemctl stop firewalld && systemctl disable firewalld
#关闭防火墙及开机自启

setenforce 0 && sed -i "s/SELINUX=*/SELINUX=disabled/g" /etc/selinux/config
#关闭安全访问控制机制及修改其配置配置文件避免开机自启

systemctl stop NetworkManager && systemctl disable NetworkManager
#关闭网络工具,在这里可能会和network产生冲突

#----配置在线YUM源仓库(aliyun)----
mkdir -p /etc/yum.repos.d
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repos-0.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#下载阿里云的镜像仓库至'/etc/yum.repos.d/目录下

yum clean all && yum makecache fast

#----开始搭建LNMP架构,作为搭建Zabbix的基础----

#--Nginx--
echo '[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1' > /etc/yum.repos.d/nginx.repo
yum clean all && yum makecache fast
yum install -y nginx
systemctl start nginx.service && systemctl enable nginx.service

#--MySQL(MariaDB)--
yum -y install mariadb-server mariadb tcl expect
systemctl start mariadb.service && systemctl enable mariadb.service

/usr/bin/expect <<-EOF
spawn mysql_secure_installation
expect "(enter for none)" {
    
    send "\r"}
expect "Set root password" {
    
    send "y\r"}
expect "New password" {
    
    send "123123\r"}
expect "Re-enter new password" {
    
    send "123123\r"}
expect "Remove anonymous users" {
    
    send "y\r"}
expect "Disallow root login remotely" {
    
    send "y\r"}
expect "Remove test database and access to it" {
    
    send "n\r"}
expect "Reload privilege tables now" {
    
    send "y\r"}
EOF

#--PHP--
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#升级软件包

yum -y install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql
yum -y install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql
#安装PHP环境工具包,重复安装两次避免遗漏

sed -i '/user = apache/c user = nginx' /etc/php-fpm.d/www.conf
sed -i '/group = apache/c group = nginx' /etc/php-fpm.d/www.conf
sed -i '/index  index.html index.htm;/c index  index.php index.html index.htm;' /etc/nginx/conf.d/default.conf
#修改apache为nginx

sed -i '31c location ~ \.php$ {' /etc/nginx/conf.d/default.conf
sed -i '32c root           /usr/share/nginx/html;' /etc/nginx/conf.d/default.conf
sed -i '33c fastcgi_pass   127.0.0.1:9000;' /etc/nginx/conf.d/default.conf
sed -i '34c fastcgi_index  index.php;' /etc/nginx/conf.d/default.conf
sed -i '35c fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;' /etc/nginx/conf.d/default.conf
sed -i '36c include        fastcgi_params;' /etc/nginx/conf.d/default.conf
sed -i '37c }' /etc/nginx/conf.d/default.conf
#修改nginx配置文件以与php相关联

sed -i '202c short_open_tag = On' /etc/php.ini
sed -i '359c expose_php = Off' /etc/php.ini
sed -i '368c max_execution_time = 300' /etc/php.ini
sed -i '378c max_input_time = 300' /etc/php.ini
sed -i '389c memory_limit = 128M' /etc/php.ini
sed -i '656c post_max_size = 16M' /etc/php.ini
sed -i '799c upload_max_filesize = 2M' /etc/php.ini
sed -i '800c always_populate_raw_post_data = -1' /etc/php.ini
sed -i '877c date.timezone = Asia/Shanghai' /etc/php.ini
#优化PHP配置文件

systemctl start php-fpm && systemctl enable php-fpm && systemctl restart nginx

echo '<?php
 phpinfo();
?>' > /usr/share/nginx/html/info.php
#编写PHP测试首页

/usr/bin/expect <<-EOF
spawn mysql -u root -p123123
expect "(none)" {
    
    send "CREATE DATABASE zabbix character set utf8 collate utf8_bin;\r"}
expect "(none)" {
    
    send "GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY '123123';\r"}
expect "(none)" {
    
    send "flush privileges;\r"}
expect "(none)" {
    
    send "exit\r"}
EOF
#创建zabbix库并设置好字符集问题
#创建数据库用户及配置用户权限


#----安装 Zabbix Server----
rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
#安装升级zabbix存储库

yum clean all

yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent
#安装Zabbix server,Web前端及agent代理,安装两次避免遗漏

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123123 zabbix
#导入初始架构和数据,输入新创建的密码

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/
#递归修改文件权限

sed -i '91c DBHost=localhost' /etc/zabbix/zabbix_server.conf
sed -i '124c DBPassword=123123' /etc/zabbix/zabbix_server.conf
#优化zabbix配置文件

systemctl start zabbix-server.service && systemctl enable zabbix-server.service 
systemctl start zabbix-agent.service && systemctl enable zabbix-agent.service

netstat -natp|grep 10051
netstat -natp|grep 'zabbix'

systemctl restart php-fpm.service && systemctl restart nginx

i=`ip a | grep "ens33" | awk 'NR==2{print$2}' | awk -F/ '{print$1}'`
echo -e "\033[31m 登录Zabbix Web页面:$i/zabbix \033[0m"
echo -e "\033[31m 用户名:Admin,密码:zabbix \033[0m"

猜你喜欢

转载自blog.csdn.net/weixin_51486343/article/details/115376127