如何在 CentOS 7 上设置本地 HTTP Yum 存储库

软件存储库(简称“ repo ”)是用于保存和维护软件包的中央文件存储位置,用户可以从中检索软件包并将其安装在他们的计算机上。

存储库通常存储在网络上的服务器上,例如互联网,可供多个用户访问。但是,您可以在计算机上创建和配置本地存储库,并以单个用户身份访问它,或者允许LAN局域网)上的其他计算机访问它。

设置本地存储库的一个优点是您不需要互联网连接来安装软件包。

YUM(Yellowdog Updater Modified)是一种基于Linux系统上广泛使用的RPMRedHat Package Manager )的软件包管理工具,它可以轻松地在Red Hat/CentOS Linux上安装软件。

在本文中,我们将解释如何在CentOS 7 VPS上通过HTTPNginx)Web 服务器设置本地YUM存储库,并向您展示如何在客户端CentOS 7机器上查找和安装软件包。

我们的测试环境

1
2
Yum HTTP Repository Server:     CentOS 7 [192.168.0.100]
Client Machine: CentOS 7 [192.168.0.101]

步骤1:安装Nginx Web服务器

**1.**首先使用 YUM 包管理器从 EPEL 存储库安装 Nginx HTTP 服务器,如下所示。

1
2
yum install epel-release
yum install nginx

**2.**安装 Nginx Web 服务器后,您可以首次启动它并使其在系统启动时自动启动。

1
2
3
systemctl start nginx
systemctl enable nginx
systemctl status nginx

3.接下来,您需要打开端口80443以允许 Web 流量到 Nginx 服务,并使用以下命令更新系统防火墙规则以允许HTTPHTTPS上的入站数据包。

1
2
3
# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload

**4.**现在,您可以使用以下 URL 确认您的 Nginx 服务器已启动并正在运行;如果您看到默认的 Nginx 网页,则一切正常。

1
http://192.168.0.100

Nginx 默认页面Nginx 默认页面

第 2 步:创建 Yum 本地存储库

**5.**在此步骤中,您需要安装创建、配置和管理本地存储库所需的软件包。

1
yum install createrepo yum-utils

**6.**接下来,创建用于存储软件包和任何相关信息的必要目录(yum 存储库)。

1
mkdir -p /home/yum/repos/{base,extras,updates,docker-ce-stable,kubernetes}

7.然后使用reposync工具将CentOS YUM存储库同步到本地目录,如图所示。

1
2
3
4
5
reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/home/yum/repos/
reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/home/yum/repos/
reposync -g -l -d -m --repoid=kubernetes --newest-only --download-metadata --download_path=/home/yum/repos/
reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/home/yum/repos/
reposync -g -l -d -m --repoid=docker-ce-stable --newest-only --download-metadata --download_path=/home/yum/repos/
示例输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.fibergrid.in
* epel: mirror.xeonbd.com
* extras: mirrors.fibergrid.in
* updates: mirrors.fibergrid.in
base/7/x86_64/group | 891 kB 00:00:02
No Presto metadata available for base
(1/9911): 389-ds-base-snmp-1.3.7.5-18.el7.x86_64.rpm | 163 kB 00:00:02
(2/9911): 389-ds-base-devel-1.3.7.5-18.el7.x86_64.rpm | 267 kB 00:00:02
(3/9911): ElectricFence-2.2.2-39.el7.i686.rpm | 35 kB 00:00:00
(4/9911): ElectricFence-2.2.2-39.el7.x86_64.rpm | 35 kB 00:00:00
(5/9911): 389-ds-base-libs-1.3.7.5-18.el7.x86_64.rpm | 695 kB 00:00:04
(6/9911): GConf2-devel-3.2.6-8.el7.i686.rpm | 110 kB 00:00:00
(7/9911): GConf2-devel-3.2.6-8.el7.x86_64.rpm | 110 kB 00:00:00
(8/9911): GConf2-3.2.6-8.el7.i686.rpm | 1.0 MB 00:00:06

在上面的命令中,选项:

  • -g– 允许删除下载后未通过 GPG 签名检查的软件包。
  • -l– 启用 yum 插件支持。
  • -d– 允许删除存储库中不再存在的本地包。
  • -m– 允许下载comps.xml文件。
  • --repoid– 指定存储库 ID。
  • --newest-only– 告诉 reposync 仅提取 repos 中每个包的最新版本。
  • --download-metadata– 允许下载所有非默认元数据。
  • --download_path– 指定下载包的路径。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-h, --help            show this help message and exit
-c CONFIG, --config=CONFIG
config file to use (defaults to /etc/yum.conf)
-a ARCH, --arch=ARCH act as if running the specified arch (default: current
arch, note: does not override $releasever. x86_64 is a
superset for i*86.)
--source operate on source packages
-r REPOID, --repoid=REPOID
specify repo ids to query, can be specified multiple
times (default is all enabled)
-e CACHEDIR, --cachedir=CACHEDIR
directory in which to store metadata
-t, --tempcache Use a temp dir for storing/accessing yum-cache
-d, --delete delete local packages no longer present in repository
-p DESTDIR, --download_path=DESTDIR
Path to download packages to: defaults to current dir
--norepopath Don't add the reponame to the download path. Can only
be used when syncing a single repository (default is
to add the reponame)
-g, --gpgcheck Remove packages that fail GPG signature checking after
downloading
-u, --urls Just list urls of what would be downloaded, don't
download
-n, --newest-only Download only newest packages per-repo
-q, --quiet Output as little as possible
-l, --plugins enable yum plugin support
-m, --downloadcomps also download comps.xml
--download-metadata download all the non-default metadata
--allow-path-traversal
Allow packages stored outside their repo directory to
be synced (UNSAFE, USE WITH CAUTION!)

**8.**接下来,检查本地目录的内容,以确保所有包都已在本地同步。

1
2
3
4
5
6
7
8
9
10
ls -l /home/yum/repos/base/
ls -l /home/yum/repos/base/Packages/
ls -l /home/yum/repos/extras/
ls -l /home/yum/repos/extras/Packages/
ls -l /home/yum/repos/updates/
ls -l /home/yum/repos/updates/Packages/
ls -l /home/yum/repos/kubernetes/
ls -l /home/yum/repos/kubernetes/Packages/
ls -l /home/yum/repos/docker-ce-stable/
ls -l /home/yum/repos/docker-ce-stable/Packages/

**9.**现在通过运行以下命令为本地存储库创建一个新的 repodata,其中标志-g用于使用指定的.xml文件更新软件包组信息。

1
2
3
4
5
createrepo -g comps.xml /home/yum/repos/base/
createrepo /home/yum/repos/docker-ce-stable/
createrepo /home/yum/repos/extras/
createrepo /home/yum/repos/updates/
createrepo /home/yum/repos/kubernetes/

**10.**为了通过 Web 浏览器查看存储库及其中的包,请创建一个指向存储库根目录的 Nginx 服务器块,如图所示。

1
vim /etc/nginx/conf.d/repos.conf 

在文件repos.conf中添加以下配置。

1
2
3
4
5
6
7
8
server {
listen 80;
root /home/yum/repos;
location / {
index index.php index.html index.htm;
autoindex on; #enable listing of directory index
}
}

保存文件并关闭它。

**11.**然后重新启动 Nginx 服务器并使用以下 URL 从 Web 浏览器查看存储库。

1
http://192.168.0.100

查看本地 Yum 存储库查看本地 Yum 存储库

步骤 3:创建 Cron Job 来同步和创建存储库

**12.**接下来,添加一个 cron 作业,它将自动将您的本地存储库与官方 CentOS 存储库同步以获取更新和安全补丁。

1
# vim /etc/cron.daily/update-localrepos

在脚本中添加这些命令。

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/home/yum/repos/
createrepo -g comps.xml /home/yum/repos/base/
reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/home/yum/repos/
createrepo /home/yum/repos/updates/
reposync -g -l -d -m --repoid=kubernetes --newest-only --download-metadata --download_path=/home/yum/repos/
createrepo /home/yum/repos/kubernetes/
reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/home/yum/repos/
createrepo /home/yum/repos/extras/
reposync -g -l -d -m --repoid=docker-ce-stable --newest-only --download-metadata --download_path=/home/yum/repos/
createrepo /home/yum/repos/docker-ce-stable/

保存脚本并关闭,并在其上设置适当的权限。

1
# chmod 755 /etc/cron.daily/update-localrepos

步骤4:在客户端机器上设置本地Yum存储库

13.现在在您的CentOS客户端机器上,将您的本地存储库添加到 YUM 配置中。

1
# vim /etc/yum.repos.d/local-repos.repo

将以下配置复制并粘贴到文件local-repos.repo中(必要时进行更改)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[local-base]
name=CentOS Base
baseurl=http://10.10.0.172:88/base/
gpgcheck=0
enabled=1

[local-extras]
name=CentOS Extras
baseurl=http://10.10.0.172:88/extras/
gpgcheck=0
enabled=1

[local-updates]
name=CentOS Updates
baseurl=http://10.10.0.172:88/updates/
gpgcheck=0
enabled=1

[docker-ce-stable]
name=Docker CE Stable
baseurl=http://10.10.0.172:88/docker-ce-stable/
enabled=1
gpgcheck=0

[kubernetes]
name=Kubernetes
baseurl=http://10.10.0.172:88/kubernetes/
enabled=1
gpgcheck=0

保存文件并开始使用本地 YUM 镜像。

**14.**接下来,运行以下命令在客户端计算机上可用的 YUM 存储库列表中查看您的本地存储库。

1
2
3
yum repolist
或者
yum repolist all

在客户端上查看本地 Yum 存储库在客户端上查看本地 Yum 存储库

就这样!在本文中,我们解释了如何在 CentOS 7 上设置本地 YUM 存储库。我们希望您发现本指南很有用。如果您有任何问题或任何其他想法要分享,请使用下面的评论表。