秘密武器揭秘:打造高效私有yum源仓库,提升Linux软件管理技能!

# 1、简介

YUM是“Yellow dog Updater, Modified”的缩写,是一个软件包管理器,YUM从指定的地方(相关网站的rpm包地址或本地的rpm路径)自动下载RPM包并且安装,能够很好的解决依赖关系问题。yum源就相当是一个目录项,当我们使用yum机制安装软件时,若需要安装依赖软件,则yum机制就会根据在yum源中定义好的路径查找依赖软件,并将依赖软件安装好。

2、YUM的基本工作机制

  • 服务器端:在服务器上面存放了所有的RPM软件包,然后以相关的功能去分析每个RPM文件的依赖性关系,将这些数据记录成文件存放在服务器的某特定目录内。
  • 客户端:如果需要安装某个软件时,先下载服务器上面记录的依赖性关系文件(可通过WWW或FTP方式),通过对服务器端下载的记录数据进行分析,然后取得所有相关的软件,一次全部下载下来进行安装。

共享yum源就是在局域网内(或本地)搭建一个yum源,然后局域网内(或本地)所有的计算机在离线的环境下可以使用yum命令安装软件。

3、搭建私有yum仓库及定时同步阿里云yum源到本地

  • 本机配置阿里源(调用系统初始化脚本)

[root@10-60-249-255 ~]# for i in /etc/yum.repos.d/*.repo;do cp $i ${i%.repo}_bak;done
[root@10-60-249-255 ~]# rm -rf /etc/yum.repos.d/*.repo
[root@10-60-249-255 ~]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo >/dev/null 2>&1
[root@10-60-249-255 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo >/dev/null 2>&1
[root@10-60-249-255 ~]# sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf    
[root@10-60-249-255 ~]# yum clean all && yum makecache
[root@10-60-249-255 ~]# yum repolist
-- 安装依赖
[root@10-60-249-255 ~]# yum -y install   yum-utils   createrepo plugin-priorities
  • 这里用蓝鲸平台调用系统初始化脚本输出日志如下

    image-20230802084632589

4、安装nginx(基于蓝鲸手动执行脚本)

[root@10-60-249-255 ~]# yum install -y nginx

5、同步公网镜像到本地私有仓库

用repoync 命令,Reposync用于将远程yum存储库同步到本地存储库,

  • -n:只下载最新的包
  • -p:下载包的路径:默认为当前目录
--建立私有yum存放目录
[root@10-60-249-255 ~]# mkdir -p /data/centos/7/{base,extras,updates,epel}

--下载rpm包
##这里同步的源文件就是上一步配置的yum源
#/data/centos/7/ 为生成的本地yum仓库文件即rpm包所在路径
[root@10-60-249-255 ~]# nohup reposync -np /data/centos/7   > /opt/yum.log 2>&1&

--建库
[root@10-60-249-255 ~]# cd /data/centos/7/
[root@10-60-249-255 7]# cd base && createrepo -p ./ && cd -
[root@10-60-249-255 7]# cd extras && createrepo -p ./ && cd -
[root@10-60-249-255 7]# cd updates && createrepo -p ./ && cd -
[root@10-60-249-255 7]# cd epel && createrepo -p ./ && cd -

image-20230802084739229

6、nginx配置(将yum仓库文件即rpm包所在路径设置为 nginx发布目录)

# vim /etc/nginx/nginx.conf (核心在server段)
=======================================================================
user root;  #得用root用户,要不会报 open() "/data/centos/7/base" failed (13: Permission denied)
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;

events {
  worker_connections 10240;
}
http {
  log_format main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;
  sendfile           on;
  tcp_nopush         on;
  tcp_nodelay         on;
  keepalive_timeout   65;
  types_hash_max_size 2048;
  include             /etc/nginx/mime.types;
  default_type       application/octet-stream;

  include /etc/nginx/conf.d/*.conf;
   
  server {
  listen       80;
  server_name mirrors.dfwlg.com;
  root         /data/centos/7/;   #这里是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;
  }
}
}
=======================================================================
# systemctl restart nginx

image-20230802084831886

7、nginx访问

#--测试nginx
访问地址:http://nginx_IP地址/

image-20230802084857709

8、设置定时同步任务(yum_update.sh)

# vim /home/scripts/yum_update.sh
==============================================================================
#!/bin/bash
echo 'Updating Aliyum Source'
DATETIME=`date +%F_%T`
exec > /var/log/aliyumrepo_$DATETIME.log
reposync -np /data/package/centos/7
if [ $? -eq 0 ];then
    createrepo --update /data/centos/7/base/base
    createrepo --update /data/centos/7/base/extras
    createrepo --update /data/centos/7/base/updates
    createrepo --update /data/centos/7/base/epel
   echo "SUCESS: $DATETIME aliyum_yum update successful" >>/var/log/aliyumrepo_$DATETIME.log
else
   echo "ERROR: $DATETIME aliyum_yum update failed" >> /var/log/aliyumrepo_$DATETIME.log
fi
==============================================================================

-- 设定定时任务(crontab -e)
30 1 * * 6 /bin/bash /home/scripts/yum_update.sh  

image-20230802084945884

9、客户端配置私有yum源

[root@10-60-249-255 ~]# cat > /etc/yum.repos.d/mirrors-dfwlg.repo <<EOF
[base]
name=CentOS-$releasever - Base - mirror.dfwlg.com
baseurl=http://xx.88/base/
path=/
enabled=1
gpgcheck=0

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

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

[epel]
name=CentOS-$releasever - epel - mirrors.dfwlg.com
baseurl=http://xx.88/epel/
failovermethod=priority
enabled=1
gpgcheck=0
EOF
========================================================================
--刷新yum缓存
#yum clean all && yum makecache
#yum repolist

10、总结

由于生产环境都是centos7操作系统,所以这里可能没考虑到其他系统的yum源问题,由于没这方面需求,所以就没继续拓展了。最后,建完私有yum仓库记得将自动配置yum源脚本加到环境标准化中…

posted @ 2023-08-02 08:51  寻梦99  阅读(85)  评论(0)    收藏  举报