shell脚本集合
Posted 时代广场的蟋蟀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本集合相关的知识,希望对你有一定的参考价值。
一、一键部署yum源
Shell脚本一键部署yum源
本脚本自动检测NAT模式或是仅主机模式、仅主机光盘是否连接
NAT模式配置在线源,仅主机模式配置本地源、仅主机光盘未连接做出提醒
#!/bin/bash
echo -e "\\033[31m =====正在验证当前为仅主机还是NAT模式===== \\033[0m"
ping -c2 -w2 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\\033[31m 检测当前为NAT模式,为您配置在线yum源 \\033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\\033[31m 在线源已配置完成 \\033[0m"
else
echo -e "\\033[31m 检测当前为仅主机模式,为您配置本地yum源 \\033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\\033[31m 检测当前为仅主机模式,但光盘未连接! \\033[0m"
else
echo -e "\\033[31m 本地yum源已配置完成 \\033[0m"
二、一键部署LNMP
本脚本全程无需改动,自动获取ens33IP地址,自动检测NAT模式还是仅主机模式,自动配置挂载、yum源
将安装包全部拖入/opt目录下
Discuz_X3.4_SC_UTF8.zip
mysql-boost-5.7.20.tar.gz
nginx-1.12.0.tar.gz
php-7.1.10.tar.bz2
#!/bin/bash
echo -e "\\033[31m =====正在验证当前为仅主机还是NAT模式===== \\033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\\033[31m 检测当前为NAT模式,为您配置在线yum源 \\033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\\033[31m 在线源已配置完成 \\033[0m"
else
echo -e "\\033[31m 检测当前为仅主机模式,为您配置本地yum源 \\033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\\033[31m 检测当前为仅主机模式,但光盘未连接! \\033[0m"
else
echo -e "\\033[31m 本地yum源已配置完成 \\033[0m"
fi
fi
echo -e "\\033[31m 接下来进入正式配置,请耐心等待 \\033[0m"
#关闭系统防火墙和安全机制
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#======编译安装nginx服务======
#安装所需开发包和编译环境、编译器
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
#创建程序用户,便于准确控制访问
useradd -M -s /sbin/nologin nginx
#解压安装包
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
#指定安装路径、指定用户名、组名、启用模块以支持统计状态
cd nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-file-aio --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
#编译及安装
make && make install
#软链接便于系统识别nginx操作命令
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
#添加nginx系统服务
echo '[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/nginx.service
#赋权及开启服务、开启开机自启
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
#=======编译安装mysql服务=======
#安装Mysql环境依赖包
yum -y install \\
ncurses \\
ncurses-devel \\
bison \\
cmake
#创建程序用户,便于准确控制访问用户
useradd -M -s /sbin/nologin mysql
#编译安装
cd /opt
tar xzvf mysql-boost-5.7.20.tar.gz -C /opt
cd /opt/mysql-5.7.20/
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_EXTRA_CHARSETS=all \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DWITH_BOOST=boost \\
-DWITH_SYSTEMD=1
#编译及安装
make && make install
#修改mysql 配置文件
echo '[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
#更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
#设置路径环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib">> /etc/profile
source /etc/profile
#初始化数据库
cd /usr/local/mysql/bin/
./mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data
#添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
yum -y install expect
#修改mysql 的登录密码
function bcd {
/usr/bin/expect <<-EOF
passwd=$1
spawn mysqladmin -u root -p password $passwd
expect "Enter password:" {send "\\r"}
EOF
}
bcd "abc123"
function efg {
/usr/bin/expect <<-EOF
spawn mysql -u root -pabc123
expect "mysql>" {send "quit\\t"}
EOF
}
#=====PHP=======
#安装环境依赖包
yum -y install gd \\
libjpeg libjpeg-devel \\
libpng libpng-devel \\
freetype freetype-devel \\
libxml2 libxml2-devel \\
zlib zlib-devel \\
curl curl-devel \\
openssl openssl-devel
#编译安装
cd /opt
tar xjvf php-7.1.10.tar.bz2
cd php-7.1.10
./configure \\
--prefix=/usr/local/php \\
--with-mysql-sock=/usr/local/mysql/mysql.sock \\
--with-mysqli \\
--with-zlib \\
--with-curl \\
--with-gd \\
--with-jpeg-dir \\
--with-png-dir \\
--with-freetype-dir \\
--with-openssl \\
--enable-fpm \\
--enable-mbstring \\
--enable-xml \\
--enable-session \\
--enable-ftp \\
--enable-pdo \\
--enable-tokenizer \\
--enable-zip
make && make install
ln -s /usr/local/php/bin/* /usr/local/bin/
#主配置文件:php.ini
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
sed -i '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini
sed -i '939c date.timezone = Asia/Shanghai' /usr/local/php/lib/php.ini
#验证安装的模块
php -m
#进程服务配置文件:php-fpm.conf
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
sed -i '17c pid = run/php-fpm.pid' /usr/local/php/etc/php-fpm.conf
#扩展配置文件:www.conf
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
#启动php-fpm
ln -s /usr/local/php/sbin/* /usr/local/sbin
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000
#配置 Nginx 支持 PHP 解析
#--65行--取消注释,修改
sed -i '65c location ~ \\.php$ {' /usr/local/nginx/conf/nginx.conf
sed -i '66c root html;' /usr/local/nginx/conf/nginx.conf
sed -i '67c fastcgi_pass 127.0.0.1:9000;' /usr/local/nginx/conf/nginx.conf
sed -i '68c fastcgi_index index.php;' /usr/local/nginx/conf/nginx.conf
sed -i '69c fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;' /usr/local/nginx/conf/nginx.conf
sed -i '70c include fastcgi_params;' /usr/local/nginx/conf/nginx.conf
sed -i '71c }' /usr/local/nginx/conf/nginx.conf
systemctl restart nginx.service
echo '<?php
phpinfo();
?>' > /usr/local/nginx/html/index.php
k=`ip a | grep "ens33" | awk 'NR==2{print$2}' | awk -F/ '{print$1}'`
#验证数据库工作是否正常
function hij {
/usr/bin/expect <<-EOF
spawn mysql -u root -p
expect "Enter" {send "\\r"}
expect "mysql>" {send "CREATE DATABASE bbs;\\r"}
expect "mysql>" {send "GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';\\r"}
expect "mysql>" {send "GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';\\r"}
expect "mysql>" {send "flush privileges;\\r"}
expect "mysql>" {send "quit\\r"}
EOF
}
hij
#=====部署Discuz社区论坛=====
cd /opt
unzip Discuz_X3.4_SC_UTF8.zip -d /opt/dis
cd /opt/dis/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/
cd /usr/local/nginx/html/bbs/
chown -R root:nginx ./config/
chown -R root:nginx ./data/
chown -R root:nginx ./uc_client/
chown -R root:nginx ./uc_server/
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/
echo -e "\\033[31m 此时可访问:$k/index.php \\033[0m"
echo -e "\\033[31m 此时可进入浏览器安装论坛:http://$k/bbs/install/index.php \\033[0m"
echo -e "\\033[31m 数据库服务器:localhost \\033[0m"
echo -e "\\033[31m 数据库名字:bbs \\033[0m"
echo -e "\\033[31m 数据库用户名:bbsuser \\033[0m"
echo -e "\\033[31m 数据库密码:admin123 \\033[0m"
echo -e "\\033[31m 管理员账号:admin \\033[0m"
echo -e "\\033[31m 管理员密码:admin123 \\033[0m"
echo -e "\\033[31m 论坛内部页面:http://$k/bbs/index.php \\033[0m"
echo -e "\\033[31m 论坛后台管理员页面:http://$k/bbs/admin.php \\033[0m"
三、一键部署LANP
Shell脚本源码编译安装一键部署LAMP架构(使用source执行脚本)source 使shell脚本里的设置对其它shell也有效。
将所有安装包全拖入/opt目录下
Discuz_X3.4_SC_UTF8.zip
mysql-boost-5.7.20.tar.gz
httpd-2.4.29.tar.gz
apr-1.6.2.tar.gz
apr-util-1.6.0.tar.gz
php-7.1.10.tar.bz2
#!/bin/bash
echo -e "\\033[31m =====正在验证当前为仅主机还是NAT模式===== \\033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\\033[31m 检测当前为NAT模式,为您配置在线yum源 \\033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\\033[31m 在线源已配置完成 \\033[0m"
else
echo -e "\\033[31m 检测当前为仅主机模式,为您配置本地yum源 \\033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\\033[31m 检测当前为仅主机模式,但光盘未连接! \\033[0m"
else
echo -e "\\033[31m 本地yum源已配置完成 \\033[0m"
fi
fi
read -p "请输入你需要配置的域名(例:www.abc.com):" y
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl
cd /opt/
tar zxvf apr-1.6.2.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2
mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util
cd /opt/httpd-2.4.29/
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi
make && make install
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/
echo '[Unit]
Description=The Apache HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/httpd/logs/httpd.pid
ExecStart=/usr/local/bin/apachectl $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/httpd.service
cp /etc/httpd.conf{,.bak}
z=`ip a | grep "ens33" | awk 'NR==2{print}' | awk -F "/" '{print$1}' | awk -F " " '{print$2}'`
sed -i "51c Listen $z:80" /etc/httpd.conf.bak
sed -i '52 s/^/#/' /etc/httpd.conf.bak
sed -i '197d' /etc/httpd.conf.bak
sed -i "196a ServerName $y:80" /etc/httpd.conf.bak
cat /etc/httpd.conf.bak > /etc/httpd.conf
#sed -i "s/Listen 80/Listen 192.168.184.30:80/" /etc/httpd.conf
#sed -i 's/#ServerName www.example.com:80/ServerName www.lic.com:80/' /etc/httpd.conf
systemctl start httpd.service
netstat -anpt | grep 80
echo -e "\\033[31m Apache安装完成!\\033[0m"
#--------编译安装mysql 服务--------
#mysql-5.7.17.tar.gz
#boost_1_59_0.tar.gz
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
echo "进行安装mysql服务"
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake
#配置软件模块
cd /opt
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz
cd /opt
mv boost_1_59_0 /usr/local/boost
cd /opt/mysql-5.7.17/
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_EXTRA_CHARSETS=all \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DWITH_BOOST=/usr/local/boost \\
-DWITH_SYSTEMD=1
make
make install
useradd -M -s /sbin/nologin mysql
echo '[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
cd /usr/local/mysql/bin/
./mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data
#添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ #用于systemctl服务管理
source /etc/profile
systemctl daemon-reload #刷新识别
systemctl start mysqld.service #开启服务
systemctl enable mysqld #开机自启动
netstat -anpt | grep 3306 #查看端口
yum -y install expect
function abc {
passwd=$1
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password $passwd
expect "Enter password:"
send "\\r"
expect eof
EOF
}
abc "abc123"
function bcd {
/usr/bin/expect <<-EOF
spawn mysql -u root -p
expect "Enter password:" {send "abc123\\r"}
expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by 'abc123';\\r"}
expect "mysql>" {send "show databases;\\r"}
expect "mysql>" {send "quit\\r"}
expect eof
EOF
}
bcd
echo -e "\\033[31m mysql安装完成!\\033[0m"
#--------编译安装PHP 解析环境--------
#安装GD库和GD库关联程序,用来处理和生成图片
yum -y install \\
gd \\
libjpeg libjpeg-devel \\
libpng libpng-devel \\
freetype freetype-devel \\
libxml2 libxml2-devel \\
zlib zlib-devel \\
curl curl-devel \\
openssl openssl-devel
#配置软件模块
cd /opt
tar jxvf php-7.1.10.tar.bz2
cd /opt/php-7.1.10/
./configure \\
--prefix=/usr/local/php7 \\
--with-apxs2=/usr/local/httpd/bin/apxs \\
--with-mysql-sock=/usr/local/mysql/mysql.sock \\
--with-config-file-path=/usr/local/php7 \\
--with-mysqli \\
--with-zlib \\
--with-curl \\
--with-gd \\
--with-jpeg-dir \\
--with-png-dir \\
--with-freetype-dir \\
--with-openssl \\
--enable-mbstring \\
--enable-xml \\
--enable-session \\
--enable-ftp \\
--enable-pdo \\
--enable-tokenizer \\
--enable-zip
#编译及安装
make && make install
#复制模版文件作为PHP 的主配置文件,并进行修改
cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini
#在测试环境时使用php.ini-development文件,而在生产环境时使用php.ini-production文件
#修改1170行为mysqli.default_socket = /usr/local/mysql/mysql.sock
sed -i '1170d' /usr/local/php7/php.ini
sed -i '1169a mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php7/php.ini
#939行--取消注释,修改时区
sed -i '939d' /usr/local/php7/php.ini
sed -i '938a date.timezone = Asia/Shanghai' /usr/local/php7/php.ini
#优化把PHP 的可执行程序文件放入路径环境变量的目录中便于系统识别
ln -s /usr/local/php7/bin/* /usr/local/bin/
php -m #查看PHP 加载了哪些模块
#修改httpd 服务的配置文件:/etc/httpd.conf,让apache支持PHP
#--393行下--插入以下内容
#AddType application/x-httpd-php .php
#AddType application/x-httpd-php-source .phps
cp /etc/httpd.conf{,.bak1}
sed -i '393a AddType application/x-httpd-php .php' /etc/httpd.conf.bak1
sed -i '394a AddType application/x-httpd-php-source .phps' /etc/httpd.conf.bak1
#--256行--修改首页文件名设置
#DirectoryIndex index.html index.php
sed -i '256c DirectoryIndex index.html index.php' /etc/httpd.conf.bak1
cat /etc/httpd.conf.bak1 > /etc/httpd.conf
#验证php测试页
cd /usr/local/httpd/htdocs
mv index.html index.html.bak
echo '<?php
phpinfo();
?>' > /usr/local/httpd/htdocs/index.php
#修改完httpd配置文件后重启服务
systemctl restart httpd.service
systemctl restart httpd.service
#浏览器访问
echo "$z $y" >> /etc/hosts
echo -e "\\033[31m php安装完成!\\033[0m"
echo -e "\\033[31m 此时可访问php页面:$z \\033[0m"
echo -e "\\033[31m 此时可访问php页面:$y \\033[0m"
四、一键部署DNS
拖入相关软件包
#!/bin/bash
echo -e "\\033[31m =====正在验证当前为仅主机还是NAT模式===== \\033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\\033[31m 检测当前为NAT模式,为您配置在线yum源 \\033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\\033[31m 在线源已配置完成 \\033[0m"
else
echo -e "\\033[31m 检测当前为仅主机模式,为您配置本地yum源 \\033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\\033[31m 检测当前为仅主机模式,但光盘未连接! \\033[0m"
else
echo -e "\\033[31m 本地yum源已配置完成 \\033[0m"
fi
fi
yum -y install bind &> /dev/null
#修改主配置文件 :/etc/named.conf
sed -i 's/127.0.0.1;/any;/' /etc/named.conf
sed -i 's/localhost;/any;/' /etc/named.conf
for ((;;))
do
read -p "请输入你需要配置的域名(例www.abc.com):" a
b=`echo $a | awk -F "." 'BEGIN{OFS="."}{$2=$2;print$2,$3}'`
c=`ip a | grep "ens33" | awk NR==2'{print}' | awk -F/ '{print$1}' | awk '{print$2}'`
#修改区域配置文件 :/etc/named.rfc1912.zones
echo "zone \\"$b\\" IN {
type master;
file \\"$b.zone\\";
allow-update { none; };
};" >> /etc/named.rfc1912.zones
#修改区域数据配置文件 :/var/named/named.localhost
cd /var/named
cp -p named.localhost $b.zone
sed -i "2c @ IN SOA $b. rname.invalid. (" /var/named/$b.zone
sed -i "8c NS $b." /var/named/$b.zone && sed -i "8 s/^/\\t/" /var/named/$b.zone
sed -i "9c A $c" /var/named/$b.zone && sed -i "9 s/^/\\t/" /var/named/$b.zone
sed -i "10c www IN A $c" /var/named/$b.zone
#添加指定dns服务器
sed -i "2c nameserver $c" /etc/resolv.conf
read -p "是否需要继续添加(y/n):" d
case $d in
y)
continue
;;
n)
#关闭系统防火墙和系统安全机制
systemctl stop firewalld
setenforce 0
#开启dns服务
systemctl restart named
break
;;
*)
echo "请正确输入"
systemctl stop firewalld
setenforce 0
systemctl restart named
break
esac
done
五、一键部署Mysql
拖入相关软件包
mysql-boost-5.7.20.tar.gz
#!/bin/bash
echo -e "\\033[31m =====正在验证当前为仅主机还是NAT模式===== \\033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\\033[31m 检测当前为NAT模式,为您配置在线yum源 \\033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\\033[31m 在线源已配置完成 \\033[0m"
else
echo -e "\\033[31m 检测当前为仅主机模式,为您配置本地yum源 \\033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\\033[31m 检测当前为仅主机模式,但光盘未连接! \\033[0m"
else
echo -e "\\033[31m 本地yum源已配置完成 \\033[0m"
fi
fi
#关闭系统防火墙和安全机制
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
echo -e "\\033[31m 检查安装包是否正确,请耐心等待 \\033[0m"
ls /opt | egrep "^mysql-boost-5.7.20.tar.gz$"
if [ $? -eq 0 ];then
echo -e "\\033[31m 接下来进入正式配置,请耐心等待 \\033[0m"
else
echo -e "\\033[31m 请准备正确安装包mysql-boost-5.7.20.tar.gz \\033[0m"
exit
fi
#安装Mysql环境依赖包
yum -y install \\
ncurses \\
ncurses-devel \\
bison \\
cmake \\
gcc \\
gcc-c++ \\
make
#创建程序用户,便于准确控制访问用户
useradd -M -s /sbin/nologin mysql
#编译安装
cd /opt
tar xzvf mysql-boost-5.7.20.tar.gz -C /opt
cd /opt/mysql-5.7.20/
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DWITH_EXTRA_CHARSETS=all \\
-DWITH_INNOBASE_STORAGE_ENGINE=1 \\
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \\
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \\
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DWITH_BOOST=boost \\
-DWITH_SYSTEMD=1
#编译及安装
make && make install
#修改mysql 配置文件
echo '[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES' > /etc/my.cnf
#更改mysql安装目录和配置文件的属主属组
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
#设置路径环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib">> /etc/profile
source /etc/profile
#初始化数据库
cd /usr/local/mysql/bin/
./mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data
#添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
六、一键部署Redis
拖入相关软件包
redis-5.0.7.tar.gz
#!/bin/bash
echo -e "\\033[31m =====正在验证当前为仅主机还是NAT模式===== \\033[0m"
ping -c1 -W1 www.baidu.com &> /dev/null
if [ $? -eq 0 ];then echo -e "\\033[31m 检测当前为NAT模式,为您配置在线yum源 \\033[0m"
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
yum clean all &> /dev/null
yum list &> /dev/null
echo -e "\\033[31m 在线源已配置完成 \\033[0m"
else
echo -e "\\033[31m 检测当前为仅主机模式,为您配置本地yum源 \\033[0m"
mount /dev/sr0 /mnt &> /dev/null
cd /etc/yum.repos.d/
mkdir -p /etc/yum.repos.d/repo.bak
mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
echo '[local]
name=local
baseurl=file:///mnt
enabled=1
gpgcheck=0' > /etc/yum.repos.d/local.repo
yum clean all &> /dev/null
yum makecache &> /dev/null
df -h | grep "/mnt"
if [ $? -ne 0 ];then
echo -e "\\033[31m 检测当前为仅主机模式,但光盘未连接! \\033[0m"
else
echo -e "\\033[31m 本地yum源已配置完成 \\033[0m"
fi
fi
#关闭防火墙
systemctl stop firewalld
setenforce 0
yum install -y gcc gcc-c++ make
yum -y install expect
cd /opt
tar zxvf redis-5.0.7.tar.gz -C /opt/
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis install
cd /opt/redis-5.0.7/utils
/usr/bin/expect <<EOF
spawn ./install_server.sh
expect "instance" {send "\\r"}
expect "config" {send "\\r"}
expect "log" {send "\\r"}
expect "data" {send "\\r"}
expect "executable" {send "/usr/local/redis/bin/redis-server\\r"}
expect "abort" {send "\\r"}
expect eof
EOF
ln -s /usr/local/redis/bin/* /usr/local/bin/
/etc/init.d/redis_6379 restart
sed -i '/bind 127.0.0.1/c bind 0.0.0.0' /etc/redis/6379.conf
sed -i 's/appendonly no/appendonly yes/' /etc/redis/6379.conf
/etc/init.d/redis_6379 restart
/etc/init.d/redis_6379 status
netstat -natp | grep redis
七、一键部署LVS(DR模式)
随便找一台服务器,该服务器被设定为NFS共享服务器
创建一个脚本文件,将以下内容复制进去
#!/bin/bash
b=`ip a | grep "ens33" | awk NR==2'{print}' | awk -F/ '{print$1}' | awk '{print$2}'`
z=`echo $b | awk -F. '{print$1"."$2"."$3}'`
echo -e "\\033[31m 请准备四台服务器 \\033[0m"
echo -e "\\033[31m 本台服务器为NFS共享服务器($b)\\033[0m"
read -p "请输入root密码:" h
read -p "请输入VIP:" e
read -p "请输入负载调度器IP:" a
read -p "请输入Web节点服务器1IP:" c
read -p "请输入Web节点服务器2IP:" d
read -p "请输入共享目录1(例:kgc):" f
read -p "请输入共享目录2(例:benet):" g
yum -y install expect
systemctl stop firewalld.service
setenforce 0
yum -y install nfs-utils rpcbind
mkdir /opt/$f
mkdir /opt/$g
chmod -R 777 /opt/$f
chmod -R 777 /opt/$g
echo "/opt/$f $z.0/24(rw,sync)
/opt/$g $z.0/24(rw,sync)" > /etc/exports
systemctl restart rpcbind.service
systemctl restart nfs.service
#Web1内容导入/opt/$f
cat > /opt/$f/Web1.sh <<EOF
#!/bin/bash
function Web1 {
systemctl stop firewalld.service
setenforce 0
yum -y install nfs-utils rpcbind httpd
systemctl restart rpcbind
systemctl restart httpd
mount.nfs $b:/opt/$f /var/www/html
echo "this is $f web" > /var/www/html/index.html
echo "DEVICE=lo:0
ONBOOT=yes
IPADDR=$e
NETMASK=255.255.255.255" > /etc/sysconfig/network-scripts/ifcfg-lo:0
ifup lo:0
route add -host $e dev lo:0
echo "net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
sysctl -p
}
Web1
EOF
chmod -R 777 /opt/$f
#Web2内容导入/opt/$g
cat > /opt/$g/Web2.sh <<EOF
#!/bin/bash
function Web2 {
systemctl stop firewalld.service
setenforce 0
yum -y install nfs-utils rpcbind httpd
systemctl restart rpcbind
systemctl restart httpd
mount.nfs $b:/opt/$g /var/www/html
echo "this is $g web" > /var/www/html/index.html
echo "DEVICE=lo:0
ONBOOT=yes
IPADDR=$e
NETMASK=255.255.255.255" > /etc/sysconfig/network-scripts/ifcfg-lo:0
ifup lo:0
route add -host $e dev lo:0
echo "net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
sysctl -p
}
Web2
EOF
chmod -R 777 /opt/$g
#将负载调度器配置导入/opt/$f
cat > /opt/$f/DR.sh <<-EOF
#!/bin/bash
function DR {
systemctl stop firewalld
setenforce 0
modprobe ip_vs
cat /proc/net/ip_vs
yum -y install ipvsadm
#配置虚拟IP地址
echo "DEVICE=ens33:0
ONBOOT=yes
IPADDR=$e
NETMASK=255.255.255.255" > /etc/sysconfig/network-scripts/ifcfg-ens33:0
systemctl restart network
echo "net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0" >> /etc/sysctl.conf
sysctl -p
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
ipvsadm -C
ipvsadm -A -t $e:80 -s rr
ipvsadm -a -t $e:80 -r $c:80 -g
ipvsadm -a -t $e:80 -r $d:80 -g
ipvsadm
}
DR
EOF
chmod -R 777 /opt/$f
#登陆Web1执行共享脚本
/usr/bin/expect <<-EOF
spawn ssh root@$c
expect "(yes/no)" {send "yes\\r"}
expect "password" {send "$h\\r"}
expect "~]#" {send "yum -y install nfs-utils rpcbind httpd\\r"}
expect "~]#" {send "mount.nfs $b:/opt/$f /var/www/html\\r"}
expect "~]#" {send ". /var/www/html/Web1.sh\\r"}
expect eof
EOF
#登陆Web2执行共享脚本
/usr/bin/expect <<-EOF
spawn ssh root@$d
expect "(yes/no)" {send "yes\\r"}
expect "password" {send "$h\\r"}
expect "~]#" {send "yum -y install nfs-utils rpcbind httpd\\r"}
expect "~]#" {send "mount.nfs $b:/opt/$g /var/www/html\\r"}
expect "~]#" {send ". /var/www/html/Web2.sh\\r"}
expect eof
EOF
#登陆负载调度器执行共享脚本
/usr/bin/expect <<-EOF
spawn ssh root@$a
expect "(yes/no)" {send "yes\\r"}
expect "password" {send "$h\\r"}
expect "~]#" {send "yum -y install nfs-utils rpcbind httpd\\r"}
expect "~]#" {send "mount.nfs $b:/opt/$f /var/www/html\\r"}
expect "~]#" {send ". /var/www/html/DR.sh\\r"}
expect eof
EOF
echo -e "\\033[31m 此时可使用NFS共享服务器,浏览器访问VIP:$e,间隔5秒点击刷新,验证负载均衡策略是否成功。 \\033[0m"
以上是关于shell脚本集合的主要内容,如果未能解决你的问题,请参考以下文章