#!/bin/bash
#记录脚本开始时间
starttime=$(date +'%Y-%m-%d %H:%M:%S')
#检测系统版本
systemv=`cat /etc/system-release|awk '{print $1}'`
case $systemv in
*CentOS*)
echo "当前操作系统为:"
cat /etc/system-release
echo "操作系统版本满足要求,脚本将继续运行"
;;
*)
echo "操作系统版本不满足要求"
echo "当前操作系统版本为:"
cat /etc/os-release
echo "脚本即将退出,请按任意键继续!"
read -n 1
exit
esac
#检测用户是否为root
UserId=`id -u`
if [ $UserId -ne 0 ]
then
echo "当前登陆用户为`id -un`,请用root用户执行脚本"
echo "脚本即将退出,请按任意键继续!"
read -n 1
exit
fi
function get_firewall_port()
{
firewall=$(firewall-cmd --query-service=http)
case $firewall in
*no*)
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
test=$(firewall-cmd --query-service=http)
if [ "$test" = "no" ]
then
echo "开启防火墙http端口失败,请检查原因。"
echo "脚本即将退出,请按任意键继续!"
read -n 1
exit
fi
;;
*yes*)
echo "防火墙http服务已开启"
esac
}
#**********************************************#
#nginx 开始
function Nginx ()
{
yum -y install gcc gcc-c++ automake autoconf libtool make \
zlib zlib-devel pcre pcre-devel openssl openssl-devel wget
#nginx不支持pcre2
mkdir -p /data/centos
cd /tmp
#检测依赖包是否安装
touch testrpm
echo -e "gcc gcc-c++ automake autoconf libtool make zlib zlib-devel pcre pcre-devel openssl openssl-devel wget" >> testrpm
rpm=`cat testrpm`
echo "正在进行依赖包的检测"
for test in $rpm
do
rpm -q --whatprovides $test >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "依赖包$test未安装,请检查原因"
echo "请按任意键退出脚本"
read -n 1
exit
else
echo "依赖包$test已正确安装。"
fi
done
echo "依赖包检测完毕,未发现异常,安装即将继续"
sleep 1
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
[ -e nginx-1.16.1.tar.gz ]
if [ $? -ne 0 ]
then
echo "nginx下载失败,请检查网络。"
echo "请按任意键继续!"
read -n 1
exit
fi
tar -zxvf nginx-1.16.1.tar.gz
if [ $? -ne 0 ]
then
echo "nginx解压失败,请检查原因,可能为网络传输故障。"
echo "请按任意键继续!"
read -n 1
exit
fi
cd nginx-1.16.1
./configure --sbin-path=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module
make
make install
get_firewall_port
#添加nginx链接,方便执行命令
ln -s /usr/nginx/nginx /bin
#系统服务方式添加nginx服务
touch /lib/systemd/system/nginx.service
echo -e "[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStart=/usr/local/nginx/nginx
ExecReload=/usr/local/nginx/nginx -s reload
ExecStop=/usr/local/nginx/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
" >> /lib/systemd/system/nginx.service
systemctl start nginx
systemctl enable nginx
#########################################
mv /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf.bak
touch /usr/local/nginx/nginx.conf
echo -e "#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
charset utf-8; #解决中文乱码
location / {
root /data/centos;
autoindex on; #开启目录浏览功能;
autoindex_exact_size off; #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;
autoindex_localtime on; #开启以服务器本地时区显示文件修改日期!
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
" >> /usr/local/nginx/nginx.conf
systemctl restart nginx
}
#nginx 结束
#***************************************************#
#Apache 开始
function Apache ()
{
mkdir -p /data/centos
yum install httpd -y
rpm -q --whatprovides httpd >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Apache安装失败,请检查原因"
echo "请按任意键退出脚本"
read -n 1
exit
fi
get_firewall_port
cd /etc/httpd/conf
mv httpd.conf httpd.conf.bak
cat httpd.conf.bak |grep -v "#"|grep -v ^$ > httpd.conf
sed -i 's/DocumentRoot "\/var\/www\/html"/DocumentRoot "\/data\/centos"/g' httpd.conf
sed -i 's/<Directory "\/var\/www\/html">/<Directory "\/data\/centos">/g' httpd.conf
#解决中文乱码
sed -i 's/AddDefaultCharset/#AddDefaultCharset/g' httpd.conf
sed -i '/AddDefaultCharset/a\AddDefaultCharset off' httpd.conf
echo "IndexOptions Charset=UTF-8" >> httpd.conf
#不关闭Selinux
#将当前网站目录/data/centos的SELinux安全上下文修改为跟原始网站目录的一样
yum install policycoreutils-python -y
semanage fcontext -a -t httpd_sys_content_t /data/centos
semanage fcontext -a -t httpd_sys_content_t /data/centos/*
restorecon -Rv /data/centos
#关闭Selinux
#sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
#setenforce 0
#默认不会显示根目录文件,开启根目录文件显示
sed -i 's/Options -Indexes/Options Indexes/g' /etc/httpd/conf.d/welcome.conf
systemctl start httpd
systemctl enable httpd
}
#Apache 结束
#**************************************************#
while true
do
read -p "请选择是用nginx还是Apache搭建yum源[nginx/httpd]" web_container
case $web_container in
*nginx*)
Nginx
break
;;
*httpd*)
Apache
break
;;
*)
echo "输入错误,请重新输入!"
esac
done
#设定自动同步
yum -y install createrepo yum-utils rsync
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
sed -i '38s/enabled=0/enabled=1/' /etc/yum.repos.d/elrepo.repo
mkdir -p /data/centos/1908
mkdir -p /data/centos/6.10
cd /data/centos/6.10
wget https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-6
wget https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-Debug-6
cd /data/centos/1908
wget https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-7
wget https://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-Debug-7
cd /data/centos
echo 0 >> number
echo -e '#!/bin/bash
#需要同步的取消注释并启用相应的源即可
cd /data/centos
i=$(cat number)
starttime=$(date +'%Y-%m-%d %H:%M:%S')
#Centos 7
echo "进入1908目录"
cd /data/centos/1908
echo "正在同步1908 base源"
reposync -r base && createrepo --update base
echo "正在同步1908 extras源"
reposync -r extras && createrepo --update extras
echo "正在同步1908 updates源"
reposync -r updates && createrepo --update updates
echo "正在同步1908 centosplus源"
reposync -r centosplus && createrepo --update centosplus
echo "正在同步1908 elrepo-kernel源"
reposync -r elrepo-kernel && createrepo --update elrepo-kernel
echo "正在同步1908 nginx源"
reposync -r nginx && createrepo --update nginx
echo "Centos 7.6源同步完毕"
#Centos 6
#echo "进入6.10目录"
#cd /data/centos/6.10
#echo "正在同步6.10 base源"
#rsync -avrt rsync://mirrors4.tuna.tsinghua.edu.cn/centos/6.10/os/x86_64/ base/
#echo "正在同步6.10 extras源"
#rsync -avrt --exclude=drpms rsync://mirrors4.tuna.tsinghua.edu.cn/centos/6.10/extras/x86_64/ extras/
#echo "正在同步6.10 updates源"
#rsync -avrt --exclude=drpms rsync://mirrors4.tuna.tsinghua.edu.cn/centos/6.10/updates/x86_64/ updates/
#echo "正在同步6.10 centosplus源"
#rsync -avrt --exclude=drpms rsync://mirrors4.tuna.tsinghua.edu.cn/centos/6.10/centosplus/x86_64/ centosplus/
#echo "Centos 6.10源同步完毕"
cd /data/centos
let i=$i+1
echo $i > number
endtime=$(date +'%Y-%m-%d %H:%M:%S')
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
total_seconds=$((end_seconds-start_seconds))
min=$(expr $total_seconds / 60)
seconds=$(expr $total_seconds % 60)
date "+Yum源于%Y年%m月%d日%T第$i次同步完毕,耗时"$min"分"$seconds"秒" >> rsync_time.log
' >> reposync.sh
touch /data/centos/rsync_time.log
chattr +a rsync_time.log
#echo ''''输出为空白,echo后''中的''不会输出
sed -i "s/%Y-%m-%d %H:%M:%S/'%Y-%m-%d %H:%M:%S'/g" reposync.sh
chmod +x reposync.sh
echo -e "0 0 * * * bash /data/centos/reposync.sh >> /dev/null 2>&1" >> /var/spool/cron/root
systemctl reload crond
#hostname -I获取的IP地址末尾有个空格,如果不用awk取第一个空格直接赋值变量容易出问题
ip=$(hostname -I|awk '{print $1}')
cd /data/centos
echo -e "[base]
name=CentOS-7-Base
baseurl=http://$ip/1908/base
gpgcheck=0
enabled=1
#released updates
[updates]
name=CentOS-7-Updates
baseurl=http://$ip/1908/updates
gpgcheck=0
enabled=1
#additional packages that may be useful
[extras]
name=CentOS-7-Extras
baseurl=http://$ip/1908/extras
gpgcheck=0
enabled=1
#nginx
[nginx]
name=Centos-7-nginx
baseurl=http://$ip/1908/nginx
gpgcheck=0
enabled=1
#elrepo-kernel
[elrepo-kernel]
name=Centos-7-elrepo-kernel
baseurl=http://$ip/1908/elrepo-kernel
gpgcheck=0
enabled=1
#additional packages that extend functionality of existing packages
#centosplus
[centosplus]
name=Centos-7-Plus
baseurl=http://$ip/1908/centosplus
gpgcheck=0
enabled=0
" >> centos7.repo
echo -e "[base]
name=Centos-6-Base
baseurl=http://$ip/6.10/base
gpgcheck=0
enabled=1
#released updates
[updates]
name=Centos-6-Updates
baseurl=http://$ip/6.10/updates
gpgcheck=0
enabled=1
#additional packages that may be useful
[extras]
name=Centos-6-Extras
baseurl=http://$ip/6.10/extras
gpgcheck=0
enabled=1
#centosplus
[centosplus]
name=Centos-6-centosplus
baseurl=http://$ip/6.10/centosplus
gpgcheck=0
enable=0
" >> centos6.repo
echo -e "局域网源搭建完毕
centos 7用 wget http://$ip/http.repo 指令下载repo文件到目标机器上
Centos 6 用 wget http://$ip/centos6.repo 指令下载repo文件到目标机器上"
function Epel ()
{
mkdir -p /data/centos/6.10/epel/readme
cd /data/centos/6.10/epel/readme
touch readme.html
echo -e '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<center>
<font color="#FF0000">此目录内的内容为epel官方源</font><br>
<font color="#FF0000">如果有需要可从此目录下载官方源</font><br>
<font color="#FF0000">如果需要新版本epel源请使用epel6-testing.repo</font>
' > readme.html
#readme
mkdir /tmp/epel6 && cd /tmp/epel6
wget https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-6.noarch.rpm
rpm2cpio epel-release-latest-6.noarch.rpm | cpio -idmv
mv -v epel-release-latest-6.noarch.rpm /data/centos/6.10/epel/readme/epel-release-latest-6.noarch.rpm
cd etc/yum.repos.d
mv -v epel.repo /data/centos/6.10/epel/readme/epel6.repo
mv -v epel-testing.repo /data/centos/6.10/epel/readme/epel6-testing.repo
#readme
mkdir -p /data/centos/1908/epel/readme
cd /data/centos/1908/epel/readme
touch readme.html
echo -e '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<center>
<font color="#FF0000">此目录内的内容为epel官方源</font><br>
<font color="#FF0000">如果有需要可从此目录下载官方源</font><br>
<font color="#FF0000">如果需要新版本epel源请使用epel6-testing.repo</font>
' > readme.html
mkdir /tmp/epel7 && cd /tmp/epel7
wget https://mirrors.tuna.tsinghua.edu.cn/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
rpm2cpio epel-release-latest-7.noarch.rpm | cpio -idmv
mv -v epel-release-latest-7.noarch.rpm /data/centos/1908/epel/readme/epel-release-latest-7.noarch.rpm
cd etc/yum.repos.d
mv -v epel.repo /data/centos/1908/epel/readme/epel7.repo
mv -v epel-testing.repo /data/centos/1908/epel/readme/epel7-testing.repo
cd /tmp && rm -fvr epel*
}
function Priority()
{
cd /etc/yum.repos.d/
sed -i '/^enable/a\priority=1' CentOS-Base.repo
#修改Centos7源
cd /data/centos
sed -i '/^enable/a\priority=1' centos7.repo
cd /data/centos/1908/epel/readme
sed -i '/^enable/a\priority=2' epel7.repo
sed -i '/^enable/a\priority=2' epel7-testing.repo
sed -i 's/enable=0/enable=1/g' epel7-testing.repo
#修改Centos6源
cd /data/centos
sed -i '/^enable/a\priority=1' centos6.repo
cd /data/centos/6.10/epel/readme
sed -i '/^enable/a\priority=2' epel6.repo
sed -i '/^enable/a\priority=2' epel6-testing.repo
sed -i 's/enable=0/enable=1/g' epel6-testing.repo
}
function Repo ()
{
cd /data/centos/1908
echo -e "[epel7]
name=epel7
baseurl=http://$ip/1908/epel
gpgcheck=0
enabled=1
priority=2
" >> epel7.repo
cd /data/centos/6.10
echo -e "[epel6]
name=epel6
baseurl=http://$ip/6.10/epel
gpgcheck=0
enabled=1
priority=2
" >> epel6.repo
cd /data/centos
sed -i '/^reposync -r nginx/a\echo "正在同步1908 epel源"' reposync.sh
sed -i '/1908 epel/a\reposync -r epel && createrepo --update epel' reposync.sh
sed -i '/6.10\/centosplus/a\#echo "正在同步6.10 epel 源"' reposync.sh
sed -i '/6.10 epel/a\#rsync -avrt --exclude={drpms,debug} rsync://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/ epel\/' reposync.sh
}
read -p "是否启用EPEL源[y/n]" epel
case $epel in
y*)
echo "正在启用EPEL源"
Epel
Repo
Priority
cd /data/centos
cp -fv 6.10/epel6.repo .
cp -fv 1908/epel7.repo .
;;
*)
echo 不启用epel源*
esac
read -p "是否启用Centos6 yum源的同步,默认只开启Centos 7的yum源的同步[y/n]" centos6
case $centos6 in
y*)
echo "正在启用Centos6的yum源的同步"
cd /data/centos
sed -i 's/#cd/cd/g' reposync.sh
sed -i 's/#echo/echo/g' reposync.sh
sed -i 's/#rsync/rsync/g' reposync.sh
;;
*)
echo "不启用Centos6 Yum源的同步"
esac
read -p "是否立即同步[y/n]" Sync
case $Sync in
y*)
echo 正在同步yum源,请保持网络畅通
bash /data/centos/reposync.sh
;;
*)
echo yum源安装完毕,稍后自行手动同步
esac
#记录脚本结时间
endtime=$(date +'%Y-%m-%d %H:%M:%S')
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
total_seconds=$((end_seconds-start_seconds))
min=$(expr $total_seconds / 60)
seconds=$(expr $total_seconds % 60)
date "+局域网yum源于%Y年%m月%d日%T搭建完毕,耗时"$min"分"$seconds"秒"