linux安裝yum源命令:yum配置本地源報錯

概述

由於網路限制,部分伺服器不給阿里源訪問許可權了,那就只能搭建一下集團的私有yum倉庫了,僅供參考。


一、共享yum源

YUM是「Yellow dog Updater, Modified」的縮寫,是一個軟體包管理器,YUM從指定的地方(相關網站的rpm包地址或本地的rpm路徑)自動下載RPM包並且安裝,能夠很好的解決依賴關係問題。yum源就相當是一個目錄項,當我們使用yum機制安裝軟體時,若需要安裝依賴軟體,則yum機制就會根據在yum源中定義好的路徑查找依賴軟體,並將依賴軟體安裝好。

YUM的基本工作機制如下:

1)伺服器端:在伺服器上面存放了所有的RPM軟體包,然後以相關的功能去分析每個RPM文件的依賴性關係,將這些數據記錄成文件存放在伺服器的某特定目錄內。

2)客戶端:如果需要安裝某個軟體時,先下載伺服器上面記錄的依賴性關係文件(可通過WWW或FTP方式),通過對伺服器端下載的紀錄數據進行分析,然後取得所有相關的軟體,一次全部下載下來進行安裝。

共享yum源就是在區域網內(或本地)搭建一個yum源,然後區域網內(或本地)所有的計算機在離線的環境下可以使用yum命令安裝軟體。


二、搭建私有yum倉庫及定時同步阿里雲yum源到本地

1、本機配置阿里源(調用系統初始化腳本)

for i in /etc/yum.repos.d/*.repo;do cp $i ${i%.repo}_bak;done
rm -rf /etc/yum.repos.d/*.repo
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/Centos-7.repo >/dev/null 2>&1
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo >/dev/null 2>&1
sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf     
yum clean all && yum makecache 
yum repolist

-- 安裝依賴
yum -y install   yum-utils   createrepo plugin-priorities 

這裡用藍鯨平台調用系統初始化腳本輸出日誌如下:

超詳細的私有yum倉庫搭建及定時同步阿里雲yum源到本地教程

2、安裝nginx(手動執行腳本)

yum install -y nginx
超詳細的私有yum倉庫搭建及定時同步阿里雲yum源到本地教程

3、同步公網鏡像到本地私有倉庫

用repoync 命令,Reposync用於將遠程yum存儲庫同步到本地存儲庫,

-n:只下載最新的包

-p:下載包的路徑:默認為當前目錄

--建立私有yum存放目錄
mkdir -p /data/centos/7/{base,extras,updates,epel}

--下載rpm包
##這裡同步的源文件就是上一步配置的yum源
#/data/centos/7/ 為生成的本地yum倉庫文件即rpm包所在路徑
nohup reposync -np  /data/centos/7   > /opt/yum.log 2>&1&

--建庫
cd /data/centos/7/
cd base && createrepo -p ./ && cd -
cd extras && createrepo -p ./ && cd -
cd updates && createrepo -p ./ && cd -
cd epel && createrepo -p ./ && cd -
超詳細的私有yum倉庫搭建及定時同步阿里雲yum源到本地教程

4、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.xxx.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

--測試nginx
訪問地址:http://nginx_IP地址/

>>nginx訪問:

超詳細的私有yum倉庫搭建及定時同步阿里雲yum源到本地教程

5、設置定時同步阿里yum源

# 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    

6、客戶端配置yum源

cat > /etc/yum.repos.d/mirrors-dfwlg.repo <<EOF
[base]
name=CentOS-$releasever - Base - mirror.dfwlg.com
baseurl=http://xxx88/base/
path=/
enabled=1
gpgcheck=0 

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

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

[epel]
name=CentOS-$releasever - epel - mirrors.dfwlg.com
baseurl=http://xxx.88/epel/
failovermethod=priority
enabled=1
gpgcheck=0
EOF

--刷新yum緩存
yum clean all && yum makecache 
yum repolist
超詳細的私有yum倉庫搭建及定時同步阿里雲yum源到本地教程

後面會分享更多devops和DBA方面流程,感興趣的朋友可以關注下~

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/218373.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 00:31
下一篇 2024-12-09 00:31

相關推薦

發表回復

登錄後才能評論