Redhat7.4 同步阿里雲鏡像rpm包並自建本地yum倉庫

環境介紹

操作系統IP地址主機名備註
Redhat7.4192.168.10.21yum-server 
Redhat7.4192.168.10.22jump01 

環境準備

1. 查看系統版本

[root@yum-server ~]# cat /etc/redhat-release 

Red Hat Enterprise Linux Server release 7.4 (Maipo)

2. 關閉防火牆

[root@yum-server ~]# systemctl stop ebtables firewalld

[root@yum-server ~]# systemctl disable ebtables firewalld

3. 關閉selinux

[root@yum-server ~]# sed -i 's/enforcing/disabled/g' /etc/sysconfig/selinux 

[root@yum-server ~]# setenforce 0

操作步驟

1. 備份系統自帶的yum源

[root@yum-server ~]# tar -zcvf Centos-bak.tar.gz /etc/yum.repos.d/*

2. 修改yum源爲阿里雲yum源

[root@yum-server ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@yum-server ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

或者

[root@yum-server ~]# curl -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@yum-server ~]# curl -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo  

3. 檢測阿里雲倉庫源是否正常

[root@yum-server ~]# yum repolist
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                         repo name                                                               status
base/$releasever/x86_64                         CentOS-$releasever - Base - mirrors.aliyun.com                          10,019
epel/x86_64                                     Extra Packages for Enterprise Linux 7 - x86_64                          13,341
extras/$releasever/x86_64                       CentOS-$releasever - Extras - mirrors.aliyun.com                           435
updates/$releasever/x86_64                      CentOS-$releasever - Updates - mirrors.aliyun.com                        2,500
repolist: 26,295
 

4. 安裝相關需要軟件

[root@yum-server ~]# 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源更新優先級工具,這個工具可以用來控制進行yum源檢索的先後順序,建議可以用在client端。

注:由於很多人喜歡最小化安裝,上邊軟件是一些常用環境。

 

5. 創建本地目錄

[root@yum-server ~]# mkdir /mirrors/

6. 同步到本地目錄

[root@yum-server ~]# reposync -r base -p /mirrors/

注:不用擔心沒有創建相關目錄,系統自動創建相關目錄,並下載,時間較長請耐心等待。

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

 

7. 創建索引

createrepo語法:

格式:createrepo -po  源目錄  索引元數據的輸出位置目錄

[root@yum-server ~]# createrepo -po /mirrors/base/  /mirrors/base/

[root@yum-server ~]# createrepo -po /mirrors/epel/  /mirrors/epel/

8. 更新源數據

[root@yum-server ~]# createrepo --update /mirrors/base

[root@yum-server ~]# createrepo --update /mirrors/epel

[root@yum-server ~]# createrepo --update /mirrors/extras

[root@yum-server ~]# createrepo --update /mirrors/updates

9. 創建定時任務腳本

[root@yum-server ~]# vim /mirrors/centos_yum_update.sh

#!/bin/bash

echo 'Updating Aliyum Source'

DATETIME=`date +%F_%T`

exec > /var/log/aliyumrepo_$DATETIME.log

     reposync -np /mirrors

if [ $? -eq 0 ];then

      createrepo --update /mirrors/base

      createrepo --update /mirrors/extras

      createrepo --update /mirrors/updates

      createrepo --update /mirrors/epel

      echo "SUCESS: $DATETIME aliyum_yum update successful"

  else

      echo "ERROR: $DATETIME aliyum_yum update failed"

fi

10. 給腳本添加執行權限

[root@yum-server ~]# chmod a+x /mirrors/centos_yum_update.sh

11. 將腳本加入到定時任務中

[root@yum-server ~]# crontab -e

# Updating Aliyum Source

00 22 * * 6  /bin/bash /mirrors/centos_yum_update.sh

解釋:每月第一個週六的22點更新阿里雲yum源

12. 安裝nginx,提供

[root@yum-server ~]# yum -y install nginx

13. 配置nginx的服務,讓其家目錄爲:/mirrors

[root@yum-server ~]# vim /etc/nginx/nginx.conf 

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {

    worker_connections 1024;

}

http {

    include /etc/nginx/mime.types;

    default_type application/octet-stream;

    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;

    keepalive_timeout 65;

    gzip on;

    include /etc/nginx/conf.d/*.conf;

}

server {

    listen 80;

    server_name localhost;

    root /mirrors;

    index index.html index.htm;

    #charset koi8-r;

    #access_log /var/log/nginx/yum.access.log main;

    location / {

        autoindex on;

        autoindex_exact_size off;

        autoindex_localtime on;

        charset utf-8,gbk;

    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html

    #

    error_page 500 502 503 504 /50x.html;

    location = /50x.html {

        root /usr/share/nginx/html;

    }

}

14. 啓動nginx服務

[root@yum-server ~]# systemctl restart nginx

[root@yum-server ~]# systemctl enable nginx

客戶端測試

1. 修改客戶端yum源,並指向本地搭建的yum源主機

[root@jump01 ~]# vim /etc/yum.repos.d/centos_local.repo 

[base]

name=base

baseurl=http://192.168.10.21/base/

failovermethod=priority

enabled=1

gpgcheck=0

[extras]

name=extras

baseurl=http://192.168.10.21/extras/

failovermethod=priority

enabled=1

gpgcheck=0


[updates]

name=updates

baseurl=http://192.168.10.21/updates/

failovermethod=priority

enabled=1

gpgcheck=0


[epel]

name=epel

baseurl=http://192.168.10.21/epel/

failovermethod=priority

enabled=1

gpgcheck=0


2. 生成yum緩存

[root@jump01 ~]# yum makecache 

3. 安裝memcached測試

[root@jump01 ~]# yum -y install memcached

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章