#!/bin/bash
base_ori(){
#1.关闭防火墙
stop_firewalld() {
fw_stat=$(systemctl status firewalld | awk '/Active/{print $3}')
if [ $fw_stat == "(running)" ]; then
systemctl stop firewalld && echo "关闭防火墙"
fi
fw_en=$(systemctl is-enabled firewalld)
if [ $fw_en == "enabled" ]; then
systemctl disable firewalld && echo "关闭防火墙自启动"
fi
echo "防火墙关闭完成"
}
#2.关闭SELinux
stop_selinux() {
sed -i.bak '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config && echo "关闭SELinux完成"
}
#3.挂载sr0
mount_sr0() {
echo "开始挂载sr0"
cd /mnt/
if [ ! -d cdrom ]; then
mkdir cdrom
fi
echo "/dev/sr0 /mnt/cdrom iso9660 defaults 0 0" >>/etc/fstab && mount -a && echo "挂载sr0完成" || echo "挂载sr0失败" exit 1
}
#4.配置yum仓库
yum_repo() {
echo "开始配置yum仓库"
yum_addr=/etc/yum.repos.d
cd $yum_addr
mkdir repos
mv *.repo repos
cat <<EOF >$yum_addr/development.repo
[development]
name=development
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
EOF
yum repolist && echo "配置yum仓库完成"
}
#5.安装软件
ins_app() {
ins_package="vim gcc* apr apr-devel apr-util apr-util-devel pcre pcre-devel net-tools tree wget lrzsz cyrus-sasl-devel expat-devel libdb-devel openldap-devel openssl* chrony"
yum -y install $ins_package && echo "软件包安装完成"
}
#6.校检
check_log() {
clear
echo "1.校检防火墙自启动状态"
systemctl is-enabled firewalld
echo "2.校检防火墙当前状态"
systemctl status firewalld | grep Active
echo "3.校检SELinux状态"
cat /etc/selinux/config | grep -v "^#" | grep -v "^$" | head -1
echo "4.校检CD-ROM是否成功挂载"
lsblk | grep rom
echo "5.查看repo仓库是否配置成功"
yum repolist
echo "6.校检ip"
ifconfig ens33 | awk 'NR==2{print $2}'
}
#7.调用程序
echo "1.开始关闭防火墙"
stop_firewalld
echo "2.开始关闭SELinux"
stop_selinux
echo "3.开始挂载sr0"
mount_sr0
echo "4.开始配置yum仓库"
yum_repo
echo "5.开始安装软件"
ins_app
echo "6.校检流程"
check_log
}
network_config(){
read -p "请输入网卡名称: " interface
read -p "请输入IP地址: " ip_addr
read -p "请输入子网掩码: " prefix_code
read -p "请输入网关: " gate_code
interface_addr=/etc/sysconfig/network-scripts/ifcfg-$interface
echo "开始配置网卡"
# sed -i '/^BOOTPROTO=/cBOOTPROTO=none' $interface_addr
# sed -i '/^ONBOOT=/cONBOOT=yes' $interface_addr
cat > $interface_addr <<EOF
TYPE=Ethernet
BOOTPROTO=none
DEVICE=$interface
NAME=$interface
ONBOOT=yes
IPADDR=$ip_addr
PREFIX=$prefix_code
GATEWAY=$gate_code
DNS1=114.114.114.114
EOF
systemctl restart network && echo "网卡配置完成" || echo "网卡配置失败"
}
kickstart(){
#declare var
yum_ins="yum -y install"
sys_res="systemctl restart"
#1.配置dhcp
dhcp_cfg() {
#declare dhcp_var
dhcp_path="/etc/dhcp"
dhcp_conf=$dhcp_path/dhcpd.conf
# 网段
net_seg="192.168.152.0"
# 子网掩码
net_mask="255.255.255.0"
#地址池
ran="192.168.152.201 192.168.152.220"
# 网关
op_router="192.168.152.2"
# tftp主机
next_ser="192.168.152.200"
$yum_ins dhcp
#解决cp覆盖时强制询问
rm -rf $dhcp_conf
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example $dhcp_conf
#去除注释和空行
sed -i "/^$/d;/^#/d" $dhcp_conf
#将内容存入中转文件
cat $dhcp_conf | awk 'NR<6{print $0}' >$dhcp_path/transfer
#将中转内容转回并删除
cat $dhcp_path/transfer >$dhcp_conf
rm -rf $dhcp_path/transfer
#配置dhcpd.conf文件
cat >>$dhcp_conf <<EOF
subnet $net_seg netmask $net_mask {
range $ran;
option routers $op_router;
next-server $next_ser;
filename "pxelinux.0";
}
EOF
#重启dhcp
if $sys_res dhcpd &>/dev/null; then
echo "dhcp启动完成"
else
echo "dhcp启动失败"
exit 1
fi
}
#2.配置tftp
tftp_cfg() {
$yum_ins tftp-server xinetd lsof
sed -i.bak '14s/yes/no/' /etc/xinetd.d/tftp
$sys_res xinetd
echo "配置tftp完成"
}
#3.配置syslinux
sys_kick() {
#路径
pxe_path="/var/lib/tftpboot"
pxe_cfg="$pxe_path/pxelinux.cfg"
pxe_def="$pxe_cfg/default"
$yum_ins syslinux system-config-kickstart
mkdir $pxe_cfg
cp /usr/share/syslinux/pxelinux.0 $pxe_path
cp /mnt/cdrom/images/pxeboot/* $pxe_path
cp /mnt/cdrom/isolinux/isolinux.cfg $pxe_def
sed -i '1s/vesamenu.c32/linux/' $pxe_def
sed -i '64a\ append initrd=initrd.img inst.repo=ftp://192.168.152.200/pub inst.ks=ftp://192.168.152.200/ks.cfg' $pxe_def
sed -i '64d' $pxe_def
echo "配置syslinux完成"
}
#4.安装vsftpd
ins_ftp() {
if $yum_ins vsftpd &>/dev/null; then
if $sys_res vsftpd &>/dev/null; then
echo "安装vsftpd完成"
else
echo "重启vsftpd失败"
exit 1
fi
else
echo "安装vsftp失败"
exit 1
fi
}
#5.准备执行Kickstart
use_kick() {
mount /dev/sr0 /var/ftp/pub/
cat <<EOF >/etc/yum.repos.d/development.repo
[development]
name=origin
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
EOF
yum clean all
yum repolist
echo "配置development yum仓库完成"
#安装图形化
$yum_ins xorg-x11-xauth
#安装语言包
yum -y groupinstall "fonts"
echo "kickstart最后准备完成"
}
#执行Kickstart
echo "1.开始配置dhcp"
dhcp_cfg
echo "2.开始配置tftp"
tftp_cfg
echo "3.开始配置syslinux"
sys_kick
echo "4.安装vsftpd"
ins_ftp
echo "5.执行kickstart"
use_kick
echo "final: 开始kickstart"
echo "接下来需要重连xshell"
echo "通过system-config-kickstart命令进入配置图形化界面"
}
apache_install(){
if tar zxf httpd-2.4.54.tar.gz && cd httpd-2.4.54/ 1>/dev/null; then
./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-ssl --enable-mpms-shared=all --enable-mpm-with=event && make && make install
if [ $? -eq 0 ] ; then
if ln -s /usr/local/apache/bin/* /usr/local/bin/ 1>/dev/null; then
# 修改PATH环境变量
if echo "PATH=$PATH:/usr/local/apache/bin" >>/etc/profile && source /etc/profile 1>/dev/null; then
echo "httpd ok!"
else
echo "httpd no!"
fi
else
echo "ln!"
exit 1
fi
else
ehco "apache安装失败"
fi
else
echo "没有Apache_tar包"
fi
# 编辑.service脚本
cat >/usr/lib/systemd/system/httpd.service <<EOF
[Unit]
# 服务名称
Description=httpd
# 在network服务后启动
After=network.target
[Service]
# 启动类型
Type=forking
# 配置变量
ExecStart=/usr/local/apache/bin/apachectl start
ExecReload=/usr/local/apache/bin/apachectl restart
ExecStop=/usr/local/apache/bin/apachectl stop
PrivateTmp=true
[Install]
# 表示"WantedBy"服务开机自启
WantedBy=multi-user.target
EOF
if [ $? == "0" ]; then
clear
echo
systemctl restart httpd
echo -e "\napache 启动成功!\n"
systemctl enable httpd
echo -e "\napache 开机自启动设置成功!\n"
echo -e "apache 运行状态如下:\n"
systemctl status httpd
echo -e "\napache 端口运行状态如下:\n"
netstat -antup | grep httpd
fi
}
mysql_install(){
# 全局变量
# MySQL源码包
mysql_rpm="mysql-community-5.7.38-1.el7.src.rpm"
# 源码路径
src_path="/usr/local/src"
# MySQL安装路径
mysql_path="/usr/local/mysql"
# 数据存储目录
data_path="/data/mysql"
if [ ! -e $mysql_rpm ] ; then
echo -e "当前路径没有${mysql_rpm}包"
exit 1
fi
# 安装软件,解压tar包
app(){
# tar包
boost_tar="boost_1_59_0.tar.bz2"
mysql_tar="mysql-5.7.38.tar.gz"
# 需要用到的软件
app_ins="cmake make gcc gcc-c++ bison ncurses ncurses-devel openssl openssl-devel bzip2"
# 1.卸载系统自带boost
yum -y remove boost-*
echo "删除系统自带的boost软件"
# 2.安装必须得软件
yum -y install $app_ins
echo "安装必须得依赖软件结束"
# 3.安装MySQL源码包
rpm -ivh $mysql_rpm
echo "安装mysql_rpm完成"
# 4.将tar包解压到/usr/local/src/
cd rpmbuild/SOURCES/
tar jxvf $boost_tar -C $src_path
tar zxvf $mysql_tar -C $src_path
echo "tar包解压完成"
}
# 安装MySQL
install(){
# MySQL账户名称
user_name="mysql"
# MySQL权限
user_judge=$user_name.$user_name
# 预编译选项
mysql_opt="
-DCMAKE_INSTALL_PREFIX=$mysql_path \
-DMYSQL_DATADIR=$data_path/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=$mysql_path/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=$src_path/boost_1_59_0
"
# 1.创建MySQL账号
useradd -M -s /sbin/nologin $user_name
echo "创建MySQL账号结束"
# 2.创建MySQL数据存储目录
mkdir -p $data_path/data
mkdir -p $data_path/log
chown -R $user_judge $data_path
echo "创建数据库存储目录结束"
# 3.编译安装MySQL
cd $src_path/mysql-5.7.38/
cmake $mysql_opt && make -j $(grep processor /proc/cpuinfo | wc -l) && make install
# 4.修改目录权限
chown -R $user_judge $mysql_path
echo "安装mysql完成"
}
# 配置启动MySQL
config(){
# MySQL配置文件
my_cnf="/etc/my.cnf"
# 脚本路径
mysqld_path="/etc/init.d/mysqld"
# 初始化数据库选项
mysqld_opt="
--initialize-insecure \
--user=mysql \
--basedir=$mysql_path \
--datadir=$data_path/data
"
# MySQL默认用户名密码
default_name="root"
default_passwd="root"
# 1.修改MySQL主配置文件
mv $my_cnf{,.bak}
cat > $my_cnf <<EOF
[mysqld]
basedir=$mysql_path #mysql安装目录
datadir=$data_path/data #mysql数据存放目录
port=3306 #mysql监听端口
socket=$mysql_path/mysql.sock #mysql.sock文件存放目录
symbolic-links=0 #关闭mysql的符号链接
character-set-server=utf8 #指定mysql的字符集为utf8
log-error=$data_path/log/mysqld.log #指定mysql的错误日志存放路径
pid-file=$mysql_path/mysqld.pid #mysql的pid文件存放目录
datadir=$data_path/data #xtrabackup存放备份文件路径
log-bin=$data_path/log/mysql_bin # 二进制日志binlog文件位置和名称
server-id=1 #MySQL服务器的唯一标识符,用于主从同步
EOF
echo "配置MySQL主配置文件完成"
# 2.配置脚本
cp $mysql_path/support-files/mysql.server $mysqld_path
chmod +x $mysqld_path
# 3. 配置脚本开机自启
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list mysqld
echo "配置脚本完成"
# 4.初始化数据库
${mysql_path}/bin/mysqld $mysqld_opt
echo "初始化数据库结束"
# 5.启动数据路
$mysqld_path start
netstat -antup | grep 3306
# 6.将MySQL命令软连接到/usr/local/bin
ln -s $mysql_path/bin/* /usr/local/bin/
echo "将MySQL命令软链接到/usr/local/bin"
# 7.设置MySQL密码为"root
mysqladmin -u root password "root"
echo "用户名root的密码为\"root\""
# 8.登录测试验证
mysql -u $default_name -p $default_passwd
}
# 执行脚本
app
install
config
}
start(){
options=("基础初始化" "配置网卡" "配置kickstart" "源码Apache安装" "源码MySQL安装" "结束配置")
opt_length=${#options[@]}
while true; do
select i in ${options[*]} ; do
case $i in
${options[0]})
base_ori
;;
${options[1]})
network_config
;;
${options[2]})
kickstart
;;
${options[3]})
apache_install
;;
${options[4]})
mysql_install
;;
${options[$((opt_length - 1))]})
echo "退出"
break 2
;;
*)
echo "\e[31m没有那个选项!\e[0m"
break 2
;;
esac
done
done
}
start