CentOS7搭建本地YUM倉庫

1. 倉庫環(huán)境準(zhǔn)備

1.1. 系統(tǒng)環(huán)境

# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
# uname -r
3.10.0-327.el7.x86_64
# ip a|awk -F'[ /]+' 'NR==9{print $3}'
192.168.56.11

1.2. 修改yum源

備份系統(tǒng)自帶的yum源
[root@linux-node1 ~]# tar -zcvf CentOS-backup.tar.gz /etc/yum.repos.d/CentOS-*
修改yum源
[root@linux-node1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@linux-node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
或者
[root@linux-node1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@linux-node1 ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

1.3. 檢驗(yàn)阿里云源是否正常

[root@linux-node1 ~]# yum repolist

2. 部署倉庫

2.1. 安裝相關(guān)軟件

[root@linux-node1 ~]#  yum install -y wget make cmake gcc gcc-c++ pcre-devel zlib-devel openssl openssl-devel createrepo yum-utils

yum-utils:reposync同步工具

createrepo:編輯yum庫工具

plugin-priorities:控制yum源更新優(yōu)先級(jí)工具,這個(gè)工具可以用來控制進(jìn)行yum源檢索的先后順序,建議可以用在client端。

注意:由于很多人喜歡最小化安裝,上邊軟件是一些常用環(huán)境。

2.2. 根據(jù)源標(biāo)識(shí)同步源到本地目錄

2.2.1. 創(chuàng)建本地目錄

mkdir /mirror

2.2.2. 同步到本地目錄

reposync -p / mirror

注意:不用擔(dān)心沒有創(chuàng)建相關(guān)目錄,系統(tǒng)自動(dòng)創(chuàng)建相關(guān)目錄,并下載,時(shí)間較長請(qǐng)耐心等待。

可以用 reposync -r --repoid=repoid指定要查詢的repo id,可以指定多個(gè)(# reposync -r base -p /mirror # 這里同步base目錄到本地)

2.2.3 更新新的rpm包

reposync -np /mirror

注意:時(shí)間同樣較長,請(qǐng)耐心等待。

2.3. 創(chuàng)建索引

createrepo -po /mirror/base/ /mirror/base/
createrepo -po /mirror/extras/ /mirror/extras/
createrepo -po /mirror/updates/ /mirror/updates/
createrepo -po /mirror/epel/ /mirror/epel/

2.4. 更新源數(shù)據(jù)

createrepo --update /mirror/base
createrepo --update /mirror/extras
createrepo --update /mirror/updates
createrepo --update /mirror/epel

2.5. 創(chuàng)建定時(shí)任務(wù)

2.5.1. 更新腳本

# vim /data/scripts/centos_yum_update.sh

#!/bin/bash
DATETIME=`date +%F_%T`

# 腳本執(zhí)行日志
[ -d /var/log/rsync_repo/ ] || mkdir -p /var/log/rsync_repo/
exec > /var/log/rsync_repo/$DATETIME.log

# 同步阿里云倉庫
reposync -np /mirror

# 判斷是否同步成功,成功更新倉庫索引
if [ $? -eq 0 ];then
    createrepo --update /mirror/base
    createrepo --update /mirror/extras
    createrepo --update /mirror/updates
    createrepo --update /mirror/epel
    echo "SUCESS: $DATETIME aliyum_yum update successful"
else
    echo "ERROR: $DATETIME aliyum_yum update failed"
fi

2.5.2. 設(shè)置定時(shí)任務(wù)

# crontab -e
# 每月第一個(gè)周六的13點(diǎn)更新阿里云yum源
00 13 * * 6 [ $(date +%d) -eq $(cal | awk 'NR==3{print $NF}') ] && /bin/bash /data/scripts/centos_yum_update.sh

2.6. 部署NGINX

安裝nginx開啟目錄權(quán)限保證本地機(jī)器可以直接本地yum源

2.6.1. 創(chuàng)建運(yùn)行賬戶

groupadd -g 8888 www
useradd -u 8888 -r -g www -s /bin/false -M www

2.6.2. 安裝NGINX

yum install nginx -y

2.6.3. 配置NGINX

找到nginx配置文件,并修改nginx配置文件:

user www;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        root         /mirror ;   # 這里是yum源存放目錄      
        location / {
            autoindex on;              # 打開目錄瀏覽功能
            autoindex_exact_size off;  # off:以可讀的方式顯示文件大小
            autoindex_localtime on;    # on、off:是否以服務(wù)器的文件時(shí)間作為顯示的時(shí)間
            charset utf-8,gbk;         # 展示中文文件名
            index index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

3. 客戶端配置

# vim /etc/yum.repos.d/CentOS.repo

[base]
name=CentOS-$releasever - Base - mirror.template.com
baseurl=http://192.168.56.11/base/
path=/
enabled=1
gpgcheck=0

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

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

[epel]
name=CentOS-$releasever - epel - mirrors.template.com
baseurl=http://192.168.56.11/epel/
failovermethod=priority
enabled=1
gpgcheck=0
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容