如何在 CentOS 7 上设置本地 HTTP Yum 存储库
软件存储库(简称“repo”)是保存和维护软件包的中央文件存储位置,用户可以从中检索软件包并将其安装到计算机上。
存储库通常存储在网络(例如互联网)上的服务器上,可以由多个用户访问。但是,您可以在计算机上创建和配置本地存储库,并以单个用户身份访问它,或者允许访问LAN(局域网)上的其他计算机。
设置本地存储库的优点之一是您不需要互联网连接来安装软件包。
YUM(Yellowdog Updater Modified)是一个广泛使用的基于Linux系统的RPM(RedHat Package Manager)的包管理工具,它使得软件在Red Hat上的安装变得简单/CentOS Linux。
在本文中,我们将解释如何在 CentOS 7 VPS 上通过 HTTP (Nginx) Web 服务器设置本地 YUM 存储库 并向您展示如何在 CentOS 7 客户端计算机上查找和安装软件包。
我们的测试环境
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 服务器,如下所示。
yum install epel-release
yum install nginx
2.安装Nginx Web服务器后,您可以首次启动它并使其在系统启动时自动启动。
systemctl start nginx
systemctl enable nginx
systemctl status nginx
3.接下来,您需要打开端口80和443以允许Web流量到达Nginx服务,更新系统防火墙规则以允许入站数据包使用以下命令在 HTTP 和 HTTPS 上。
firewall-cmd --zone=public --permanent --add-service=http
firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --reload
4. 现在您可以使用以下 URL 确认您的 Nginx 服务器已启动并正在运行;如果您看到默认的 Nginx 网页,则一切正常。
http://SERVER_DOMAIN_NAME_OR_IP
第2步:创建Yum本地存储库
5. 在此步骤中,您需要安装创建、配置和管理本地存储库所需的软件包。
yum install createrepo yum-utils
6. 接下来,创建必要的目录(yum 存储库)来存储包和任何相关信息。
mkdir -p /var/www/html/repos/{base,centosplus,extras,updates}
7. 然后使用reposync工具将CentOS YUM存储库同步到本地目录,如图所示。
reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/var/www/html/repos/
reposync -g -l -d -m --repoid=centosplus --newest-only --download-metadata --download_path=/var/www/html/repos/
reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/var/www/html/repos/
reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/var/www/html/repos/
样本输出
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 仅提取存储库中每个包的最新版本。--download-metadata
– 允许下载所有非默认元数据。--download_path
– 指定下载包的路径。
8. 接下来,检查本地目录的内容以确保所有包已在本地同步。
ls -l /var/www/html/repos/base/
ls -l /var/www/html/repos/base/Packages/
ls -l /var/www/html/repos/centosplus/
ls -l /var/www/html/repos/centosplus/Packages/
ls -l /var/www/html/repos/extras/
ls -l /var/www/html/repos/extras/Packages/
ls -l /var/www/html/repos/updates/
ls -l /var/www/html/repos/updates/Packages/
9. 现在,通过运行以下命令为本地存储库创建一个新的 repodata,其中标志 -g
用于使用指定的 更新包组信息.xml
文件。
createrepo -g comps.xml /var/www/html/repos/base/
createrepo -g comps.xml /var/www/html/repos/centosplus/
createrepo -g comps.xml /var/www/html/repos/extras/
createrepo -g comps.xml /var/www/html/repos/updates/
10. 要通过 Web 浏览器查看存储库及其中的包,请创建一个指向存储库根目录的 Nginx 服务器块,如图所示。
vim /etc/nginx/conf.d/repos.conf
在文件repos.conf中添加以下配置。
server {
listen 80;
server_name repos.test.lab; #change test.lab to your real domain
root /var/www/html/repos;
location / {
index index.php index.html index.htm;
autoindex on; #enable listing of directory index
}
}
保存文件并关闭它。
11. 然后重新启动 Nginx 服务器并使用以下 URL 从 Web 浏览器查看存储库。
http://repos.test.lab
步骤 3:创建 Cron 作业来同步和创建存储库
12. 接下来,添加一个 cron 作业,该作业将自动将您的本地存储库与官方 CentOS 存储库同步,以获取更新和安全补丁。
vim /etc/cron.daily/update-localrepos
在脚本中添加这些命令。
#!/bin/bash
##specify all local repositories in a single variable
LOCAL_REPOS=”base centosplus extras updates”
##a loop to update repos one at a time
for REPO in ${LOCAL_REPOS}; do
reposync -g -l -d -m --repoid=$REPO --newest-only --download-metadata --download_path=/var/www/html/repos/
createrepo -g comps.xml /var/www/html/repos/$REPO/
done
保存脚本并关闭它并为其设置适当的权限。
chmod 755 /etc/cron.daily/update-localrepos
步骤 4:在客户端计算机上设置本地 Yum 存储库
13. 现在,在 CentOS 客户端计算机上,将本地存储库添加到 YUM 配置中。
vim /etc/yum.repos.d/local-repos.repo
将以下配置复制并粘贴到文件 local-repos.repo 中(根据需要进行更改)。
[local-base]
name=CentOS Base
baseurl=http://repos.test.lab/base/
gpgcheck=0
enabled=1
[local-centosplus]
name=CentOS CentOSPlus
baseurl=http://repos.test.lab/centosplus/
gpgcheck=0
enabled=1
[local-extras]
name=CentOS Extras
baseurl=http://repos.test.lab/extras/
gpgcheck=0
enabled=1
[local-updates]
name=CentOS Updates
baseurl=http://repos.test.lab/updates/
gpgcheck=0
enabled=1
保存文件并开始使用本地 YUM 镜像。
14. 接下来,运行以下命令以在客户端计算机上的可用 YUM 存储库列表中查看本地存储库。
yum repolist
OR
yum repolist all
就这样!在本文中,我们解释了如何在 CentOS 7 上设置本地 YUM 存储库。我们希望本指南对您有用。如果您有任何问题或任何其他想法要分享,请使用下面的评论表。