1. 部署环境

2. 将阿里云镜像repo文件到本地

wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

3. 安装httpd服务,用于提供存放yum源

yum install httpd -y

使用默认配置即可,如果有特殊需求可以修改配置文件/etc/httpd/conf/httpd.conf
http默认的home目录是/var/www/html

4. 安装yum-utils提供reporsync服务

yum install yum-utils -y

5. 选择指定仓库标识作为本地yum源:

执行yum repolist命令查看yum仓库标识

使用epel作为本地yum源,用/var/www/html作为yum仓库根目录

reposync -r epel -p /var/www/html/

命令执行完成之后,会将阿里云中的epel源同步到本地/var/www/html中;在/var/www/html中自动创建epel目录用于存放rpm包;第一次同步时间比较长(视网速而定)。

6. createrepo命令将对/var/www/html/epel下的rpm包创建为本地的yum仓库,目的是生成repodata目录并自动创建索引信息。

  1. createrepo -pdo /var/www/html/epel/ /var/www/html/epel/ # 第一个是repodata存放的目录,第二个是需要生成索引信息的yum源仓库目录

7. 验证本地yum源是否能正常使用

登录内网中其他服务器,编写repo文件。

  1. # vim /etc/yum.repos.d/epel-7.repo
  2. [epel]
  3. name=local epel
  4. baseurl=http://yum_server_IP/epel
  5. enabled=1
  6. gpgcheck=0

备份其他repo文件至/etc/yum.repos.d/repobak目录下,以防影响测试。(本地源不可用的话会影响其他源的使用)

执行yum clean all、yum makecache。

7. 为了保证本地源能和阿里云镜像源同步,可以通过脚本定时任务实现

  1. # vim /root/yum-update.sh
  2. #!/bin/bash
  3. datetime=`date +"%Y-%m-%d"`
  4. exec > /var/log/epel.log # 同步日志输出
  5. reposync -d -r epel -p /var/www/html/ #同步镜像源
  6. if [ $? -eq 0 ];then
  7. createrepo --update /var/www/html/epel # 每次添加新的rpm时,必须更新epel索引信息
  8. echo "SUCCESS: $datetime epel update successful"else
  9. echo "ERROR: $datetime epel update failed"fi
  10. # 定时任务:每周六凌晨三点同步yum源
  11. crontab -e
  12. 0 3 * * 6 /bin/bash /root/yum-update.sh