CentOS基于nginx构建本地YUM仓库

以下是一篇关于如何使用 Nginx 构建本地 YUM 仓库的技术性博客文章,基于您提供的步骤进行了整理和阐述。


使用 Nginx 构建本地 YUM 仓库

在 CentOS 或 RHEL 系统中,构建一个本地 YUM 仓库可以加速软件包的安装与更新。本文将详细介绍如何使用 Nginx 创建本地 YUM 仓库,包括配置和更新的步骤。

1. 开启 YUM 下载缓存

首先,确保 YUM 缓存被开启,以便下载的软件包可以保留:

sed -i '3c keepcache=1' /etc/yum.conf

2. 安装必要的插件

安装所需的 YUM 插件和工具,包括 yum-plugin-downloadonlycreatereporsync

yum install -y yum-plugin-downloadonly createrepo rsync

3. 创建仓库目录

创建一个目录来存放下载的 RPM 包:

mkdir -p /mirrors/centos

4. 下载软件包

使用 YUM 下载所需的软件包,而不立即安装:

yum install nginx -y --downloadonly --downloaddir=/mirrors/centos

您也可以手动将 RPM 包上传到 /mirrors/centos 目录中。

5. 创建 YUM 仓库

使用 createrepo 命令在仓库目录中生成元数据:

createrepo /mirrors/centos

6. 安装 Nginx

在安装 Nginx 之前,首先添加 Nginx 官方 YUM 源:

cat <<EOL > /etc/yum.repos.d/nginx-stable.repo
[nginx-stable]
baseurl = http://nginx.org/packages/centos/$releasever/$basearch/
enabled = 1
gpgcheck = 0
gpgkey = https://nginx.org/keys/nginx_signing.key
name = nginx stable repo
EOL

然后,安装 Nginx:

yum -y install nginx

7. 配置 Nginx

编辑 Nginx 的配置文件,创建一个新的配置文件:

cd /etc/nginx/conf.d
vim mirrors.conf

在配置文件中添加以下内容:

server {
    listen 88;
    server_name localhost;
    root /mirrors/;
    
    location / {
        autoindex on; # 开启目录列表显示
        autoindex_exact_size off; # 显示大小
        autoindex_localtime on; # 按本地时间显示
    }
}

8. 启动 Nginx 服务

启用并启动 Nginx 服务:

systemctl enable nginx --now
systemctl status nginx

9. 配置 YUM 仓库

创建一个新的 YUM 仓库配置文件:

vim /etc/yum.repos.d/mirrors.repo

在文件中添加以下内容:

[yumbase]
name=yumbase-local-repository
baseurl=http://your_server_ip:88/centos/
enabled=1
gpgcheck=0

your_server_ip 替换为您的服务器 IP 地址。

10. 验证 YUM 仓库

在客户端上运行以下命令以验证 YUM 仓库的配置:

yum clean all
yum makecache
yum -y install 软件名.版本号

11. 更新 YUM 仓库

当仓库中的 RPM 包发生变化时(例如添加或删除包),需要更新 YUM 仓库的数据文件:

mv /mirrors/centos/package.rpm /root  # 移走一个 RPM 包
createrepo --update /mirrors/centos/  # 更新仓库元数据
yum makecache   # 更新缓存数据
yum repolist    # 查询仓库中 RPM 包数量

12. 同步外部源(可选)

您可以通过 rsync 从外部镜像源同步软件包。以下是从清华大学源同步 CentOS 包的示例脚本:

#!/bin/bash
/usr/bin/rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/centosplus/x86_64/Packages/ /mirrors/centos
/usr/bin/rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/extras/x86_64/Packages/ /mirrors/centos
/usr/bin/rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/Packages/ /mirrors/centos
/usr/bin/rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/centos/7/updates/x86_64/Packages/ /mirrors/centos
/usr/bin/rsync -avz rsync://mirrors.tuna.tsinghua.edu.cn/epel/7Server/x86_64/Packages/ /mirrors/centos

结论

通过以上步骤,您成功地使用 Nginx 构建了一个本地 YUM 仓库。这将有助于加速软件包的安装和更新过程。根据实际需求,您可以定期更新仓库内容,确保软件包的完整性和可用性。


posted @   broadviews  阅读(99)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示