脚本自动部署本地YUM源+Apache配置

分享一个部署本地YUM源的脚本:

#!/bin/bash

==============================================================

一键配置本地 YUM 源 Web 服务(CentOS 7)

功能:强制删除旧 repodata 并重新生成 + 配置 Apache

路径:/opt/centos_7/{base,epel,updates,k8s,...}

IP:192.168.10.99

==============================================================

#REPO_ROOT="/opt/centos_7"

#SERVER_IP="192.168.10.99"

#CONF_FILE="/etc/httpd/conf.d/local-repo.conf"

#HTTPD_LOG="/var/log/httpd/error_log"

set -e # 遇错退出

read -ep "请输入YUM源仓库路径,如/opt/centos/7.9/base/Packages,那请输入/opt/centos/7.9" REPO_ROOT

read -ep "请输入你的YUM源服务器IP地址:" SERVER_IP

CONF_FILE="/etc/httpd/conf.d/local-repo.conf"

HTTPD_LOG="/var/log/httpd/error_log"

echo "🚀 开始配置本地 YUM 源(强制重建 repodata)..."

1. 安装依赖

yum install -y httpd createrepo

#2.关闭防火墙、关闭强制访问控制、修改当前时区

systemctl disable firewalld --now &>/dev/null && echo "防火墙已关闭"

setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config &>/dev/null && echo "访问控制已经关闭"

#修改当前时间

timedatectl set-timezone Asia/Shanghai && echo "时区已经修改为:Asia/Shanghai"

3. 强制重建每个仓库的 repodata

echo "🔄 强制重建所有仓库的 repodata..."

for dir in "$REPO_ROOT"/*; do

if [ -d "$dir" ]; then

repo_name=(basename "dir")

跳过 .repo 文件(如 tommypeng.repo)

if [[ "$repo_name" == *.repo ]]; then

echo " ⚠️ 跳过非目录: $repo_name"

continue

fi

echo " ➤ 处理仓库: $repo_name"

cd "$dir"

删除旧 repodata(如果存在)

if [ -d "repodata" ]; then

echo " 🗑️ 删除旧 repodata/"

rm -rf repodata

fi

重新生成

echo " 🔨 重新生成 repodata..."

createrepo .

echo " ✅ 完成: $repo_name"

fi

done

3. 创建 Apache 配置

cat > "$CONF_FILE" <<EOF

Alias /centos_7 $REPO_ROOT

<Directory "$REPO_ROOT">

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

</Directory>

EOF

6. 重载 Apache

systemctl reload httpd

7. 本地验证

echo "🔍 验证本地访问..."

if curl -sf http://localhost/centos_7/base/repodata/repomd.xml >/dev/null; then

echo "✅ 仓库 Web 服务已就绪!"

else

echo "❌ 访问失败,请检查 /var/log/httpd/error_log"

exit 1

fi

echo "----------------------------------------"

echo "🌐 仓库地址: http://$SERVER_IP/centos_7/"

local_yum(){

echo "生成本地repo文件至/etc/yum.repos.d/路径下"

echo "----------------------------------------"

echo ""

echo "📋 客户端 repo 配置示例(保存为 /etc/yum.repos.d/my_local.repo):"

cat >/etc/yum.repos.d/my_local.repo <<EOF

local-base

name=Local CentOS Base

baseurl=http://$SERVER_IP/centos_7/base/

enabled=1

gpgcheck=0

local-epel

name=Local EPEL

baseurl=http://$SERVER_IP/centos_7/epel/

enabled=1

gpgcheck=0

local-updates

name=Local Updates

baseurl=http://$SERVER_IP/centos_7/updates/

enabled=1

gpgcheck=0

local-k8s

name=Local Kubernetes

baseurl=http://$SERVER_IP/centos_7/k8s/

enabled=1

gpgcheck=0

EOF

}

while true;do

echo "是否要我帮你生成你其他服务器的源仓库配置文件?1:需要 2.退出"

read -ep "请选择:" choice

case $choice in

local_yum

if [ -f /etc/yum.repos.d/my_local.repo ];then

echo "已经生成,复制到需要使用的机器/etc/yum.repos.d/路径并在备份后删除其他的.repo文件"

else

echo "未生成成功"

exit 1

fi

;;

exit 0

;;

*)

echo "选择有误,请选择有效选项(1 or 2)"

;;

esac

done

相关推荐
stella·6 小时前
服务器割接,我所学习到的内容。
linux·运维·服务器·学习·shell·割接
柏木乃一1 天前
进程(7)命令行参数与环境变量
linux·服务器·shell·环境变量·鸣潮
Neolnfra1 天前
Shell攻防完全手册:从反弹到后渗透
渗透·shell·反弹shell
gis分享者3 天前
Bash 中如何使用正则表达式进行文本处理?(中等)
正则表达式·bash·shell·文本·处理
gis分享者4 天前
请编写一个 Bash 脚本检查系统中的所有服务状态(中等)
bash·shell·服务·状态·检查·所有
_OP_CHEN4 天前
【Linux系统编程】(十七)揭秘 Linux 进程创建与终止:从 fork 到 exit 的底层逻辑全解析
linux·运维·服务器·操作系统·shell·进程·进程创建与终止
pr_note5 天前
via ladder
shell·tcl
import_random8 天前
[macos系统]我用的是苹果电脑, 终端的shell是zsh,而不是bash, 那我在执行.sh文件的时候, 是bash xxx.sh还是zsh xxx.sh
shell
gis分享者8 天前
如何在 Shell 脚本中如何使用条件判断语句?(中等)
面试·shell·脚本·语法·使用·判断·条件
zfj3219 天前
sshd除了远程shell外还有哪些功能
linux·ssh·sftp·shell