Myluzh Blog

CentOS7 部署本地YUM仓库

发布时间: 2024-5-31 文章作者: myluzh 分类名称: Linux 朗读文章


0x01 概述
环境准备:
CentOS7_A:YUM-SERVER,用来部署本地yum仓库。
ip:172.16.10.78
CentOS7_A:用来测试搭建YUM仓库是否生效。
ip:172.16.10.79

0x02 YUM-SERVER部署web服务
在yum-server机子上搭建一个web服务,开启目录索引
# 写入nginx repo
cat <<EOF > nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF

# 更新索引,安装nginx
yum mackcache
yum install nginx -y
systemctl start nginx && systemctl enable nginx

# 把原来的默认nginx配置文件删掉
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
# 写入一个新的配置文件,/www/html/yumrepos开启目录访问
cat < /etc/nginx/conf.d/yumrepos.conf
server {
    listen 80;
    server_name _;

    root /www/html/yumrepos;
    index index.html index.htm index.php;
    autoindex on;
}
EOF
# 重载nginx配置
nginx -s reload

# 测试web服务是否生效
myluzh@myluzhMacBookPro ~ % curl -I http://172.16.10.78/
HTTP/1.1 200 OK
Server: nginx/1.26.1
Date: Fri, 31 May 2024 02:39:23 GMT
Content-Type: text/html
Connection: keep-alive

0x03 同步远程软件包到本地YUM-SERVER
# 安装yum源仓库需要用到的软件包
yum install -y yum-utils createrepo
# 同步repo中的软件包到本地
reposync -c /etc/yum.repos.d/CentOS-Base.repo -p /www/html/yumrepos
reposync -c /etc/yum.repos.d/nginx.repo -p /www/html/yumrepos
# 若要同步xxx.repo中指定块[section]中的rpm包,可以指定名称同步
reposync -n --repoid=base --repoid=updates -p /www/html/yumrepos
reposync -n --repoid=nginx-stable -p /www/html/yumrepos
# 同步完成后需要创建yum仓库信息
createrepo -v /www/html/yumrepos/nginx-stable
例子: 这里我我们尝试同步nginx.repo的[nginx-stable]到 /www/html/yumrepos。然后通过createrrepo命令创建yum仓库信息,完成后会生成一个repodata目录。
# 同步rpm包到本地
[root@yum-server yum.repos.d]# reposync -n --repoid=nginx-stable -p /www/html/yumrepos
# 看到/www/html/yumrepos/已经有rpm包了,同步完了,并且repodata目录也在了。
[root@yum-server yum.repos.d]# tree /www/html/yumrepos/
/www/html/yumrepos/
└── nginx-stable
    ├── repodata
    │   ├── 072abe47aba9470da49aec44a27995d64abf5ed9552f2395e86c4df4ebe67958-other.sqlite.bz2
    │   ├── 30a638b3f62e9d91a7d6510af6fab860a2127d5c8f97aa2565f7828f1d129046-primary.xml.gz
    │   ├── 68781504601a61c29874727e2cc9634ab3e27ef50e51f809345f2cab78299372-filelists.xml.gz
    │   ├── b77f53099d19991cfbf6106efe6b9c8f2fbee9cd0736a6e1291da1cc393cb984-other.xml.gz
    │   ├── c9fe8dd5051660f6e0f140cd203b7cf70e98347f4d998bc846d18068182095ff-primary.sqlite.bz2
    │   ├── ee1b534b4cec99036352565813f2527dfb09cdc865715a72e5f6748bdc9d637a-filelists.sqlite.bz2
    │   └── repomd.xml
    └── RPMS
        ├── nginx-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-debug-1.8.0-1.el7.ngx.x86_64.rpm
        ├── nginx-debuginfo-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-geoip-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-geoip-debuginfo-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-image-filter-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-image-filter-debuginfo-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-njs-1.26.1+0.8.4-2.el7.ngx.x86_64.rpm
        ├── nginx-module-njs-debuginfo-1.26.1+0.8.4-2.el7.ngx.x86_64.rpm
        ├── nginx-module-perl-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-perl-debuginfo-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-xslt-1.26.1-2.el7.ngx.x86_64.rpm
        ├── nginx-module-xslt-debuginfo-1.26.1-2.el7.ngx.x86_64.rpm
        └── nginx-nr-agent-2.0.0-12.el7.ngx.noarch.rpm

3 directories, 21 files

0x04 客户端测试本地YUM-SERVER
# 定义一个仓库,地址为本地的YUM-SERVER
cat <<EOF > /etc/yum.repos.d/nginx.repo
[nginx-nginx-stable]
name=nginx-stable-repo
baseurl=http://172.16.10.78/nginx-stable/
gpgcheck=0
enabled=1
EOF

# 测试下是否可以从本地YUM-SERVER安装
yum makecache 
yum install nginx
一些其他的yum源配置例子:
[base]
name=CentOS-$releasever-Base
baseurl=http://yum01/yumrepos/base/
gpgcheck=0
enabled=1

[epel]
name=CentOS-$releasever-epel
baseurl=http://yum01/yumrepos/epel/
gpgcheck=0
enabled=1


[updates]
name=CentOS-$releasever-Updates
baseurl=http://yum01/yumrepos/updates/
gpgcheck=0
enabled=1

[extras]
name=CentOS-$releasever-Extras
baseurl=http://yum01/yumrepos/extras/
gpgcheck=0
enabled=1

[centos-openstack-train]
name=CentOS-7-OpenStack Train
baseurl=http://yum01/yumrepos/centos-openstack-train/
gpgcheck=0
enabled=1

[centos-qemu-ev]
name=CentOS-$releasever-QEMU EV
baseurl=http://yum01/yumrepos/centos-qemu-ev/
gpgcheck=0
enabled=1

[docker-ce-stable]
name=CentOS-$releasever-docker-ce-stable
baseurl=http://yum01/yumrepos/docker-ce-stable/
gpgcheck=0
enabled=1

0x05 其他
参考链接:无

标签: centos yum rpm repo centos7 reposync createrepo

发表评论