CentOS7搭建本地YUM仓库

1. 仓库环境准备

1.1. 系统环境

# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
# uname -r
3.10.0-327.el7.x86_64
# ip a|awk -F'[ /]+' 'NR==9{print $3}'
192.168.66.11

1.2. 修改yum源

备份系统自带的yum源
[root@linux-node1 ~]# tar -zcvf CentOS-backup.tar.gz /etc/yum.repos.d/CentOS-*
修改yum源
[root@linux-node1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@linux-node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
或者
[root@linux-node1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@linux-node1 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

1.3. 检验阿里云源是否正常

[root@linux-node1 ~]# yum repolist

2. 部署仓库

2.1. 安装相关软件

[root@linux-node1 ~]#  yum install -y wget make cmake gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel createrepo yum-utils

yum-utils:reposync同步工具

createrepo:编辑yum库工具

plugin-priorities:控制yum源更新优先级工具,这个工具可以用来控制进行yum源检索的先后顺序,建议可以用在client端。

注意:由于很多人喜欢最小化安装,上边软件是一些常用环境。

2.2. 根据源标识同步源到本地目录

2.2.1. 创建本地目录

mkdir /mirror

2.2.2. 同步到本地目录

reposync -p / mirror

注意:不用担心没有创建相关目录,系统自动创建相关目录,并下载,时间较长请耐心等待。

可以用 reposync -r --repoid=repoid指定要查询的repo id,可以指定多个(# reposync -r base -p /mirror # 这里同步base目录到本地)

2.2.3. 更新新的rpm包

reposync -np /mirror

注意:时间同样较长,请耐心等待。

2.2.4. 创建索引

createrepo -po /mirror/base/ /mirror/base/
createrepo -po /mirror/extras/ /mirror/extras/
createrepo -po /mirror/updates/ /mirror/updates/
createrepo -po /mirror/epel/ /mirror/epel/

2.2.5. 更新源数据

createrepo --update /mirror/base
createrepo --update /mirror/extras
createrepo --update /mirror/updates
createrepo --update /mirror/epel

2.3. 创建定时任务

2.3.1. 更新脚本

# vim /data/scripts/centos_yum_update.sh

#!/bin/bash
DATETIME=`date +%F_%T`

# 脚本执行日志
[ -d /var/log/rsync_repo/ ] || mkdir -p /var/log/rsync_repo/
exec > /var/log/rsync_repo/$DATETIME.log

# 同步阿里云仓库
reposync -np /mirror

# 判断是否同步成功,成功更新仓库索引
if [ $? -eq 0 ];then
    createrepo --update /mirror/base
    createrepo --update /mirror/extras
    createrepo --update /mirror/updates
    createrepo --update /mirror/epel
    echo "SUCESS: $DATETIME aliyum_yum update successful"
else
    echo "ERROR: $DATETIME aliyum_yum update failed"
fi

2.3.2. 设置定时任务

# crontab -e
# 每月第一个周六的13点更新阿里云yum源
00 13 * * 6 [ $(date +%d) -eq $(cal | awk 'NR==3{print $NF}') ] && /bin/bash /data/scripts/centos_yum_update.sh

2.4. 部署NGINX

安装nginx开启目录权限保证本地机器可以直接本地yum源

2.4.1. 创建运行账户

groupadd -g 8888 www
useradd -u 8888 -r -g www -s /bin/false -M www

2.4.2. 安装NGINX

yum install nginx -y

2.4.3. 配置NGINX

找到nginx配置文件,并修改nginx配置文件:

user www;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        root         /mirror ;   # 这里是yum源存放目录      
        location / {
            autoindex on;              # 打开目录浏览功能
            autoindex_exact_size off;  # off:以可读的方式显示文件大小
            autoindex_localtime on;    # on、off:是否以服务器的文件时间作为显示的时间
            charset utf-8,gbk;         # 展示中文文件名
            index index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

3. 客户端配置

# vim /etc/yum.repos.d/CentOS.repo

[base]
name=CentOS-$releasever - Base - mirror.template.com
baseurl=http://192.168.66.11/base/
path=/
enabled=1
gpgcheck=0

[updates]
name=CentOS-$releasever - Updates - mirror.template.com
baseurl=http://192.168.66.11/updates/
path=/
enabled=1
gpgcheck=0

[extras]
name=CentOS-$releasever - Extras - mirrors.template.com
baseurl=http://192.168.66.11/extras/
path=/
enabled=1
gpgcheck=0

[epel]
name=CentOS-$releasever - epel - mirrors.template.com
baseurl=http://192.168.66.11/epel/
failovermethod=priority
enabled=1
gpgcheck=0
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值