搭建内网 Yum 源

温馨提醒

安装工具

  • CentOS/RHEL

    1
    2
    
    yum install yum-utils createrepo
    dnf install dnf-utils createrepo

配置本地 yum 源

1
2
3
4
5
# CentOS6
wget -O /etc/yum.repos.d/CentOS-6.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo

# CentOS7
wget -O /etc/yum.repos.d/CentOS-7.repo https://mirrors.aliyun.com/repo/Centos-7.repo

同步官方 yum 源到本地 yum 服务器

  • 查看可同步的仓库

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    [root@localhost yum.repos.d]# yum repolist all
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.aliyun.com
     * extras: mirrors.aliyun.com
     * updates: mirrors.aliyun.com
    repo id                                                                                repo name                                                                                                   status
    base/7/x86_64                                                                          CentOS-7 - Base - mirrors.aliyun.com                                                                        enabled: 10,072
    centos6-base                                                                           CentOS-6 Base                                                                                               enabled:  6,713
    centos6-extras                                                                         CentOS-6 Extras                                                                                             enabled:     47
    centos6-updates                                                                        CentOS-6 Updates                                                                                            enabled:  1,193
    centosplus/7/x86_64                                                                    CentOS-7 - Plus - mirrors.aliyun.com                                                                        disabled
    contrib/7/x86_64                                                                       CentOS-7 - Contrib - mirrors.aliyun.com                                                                     disabled
    extras/7/x86_64                                                                        CentOS-7 - Extras - mirrors.aliyun.com                                                                      enabled:    526
    updates/7/x86_64                                                                       CentOS-7 - Updates - mirrors.aliyun.com                                                                     enabled:  6,173
    repolist: 24,724
  • 创建仓库存放目录

    1
    2
    
    mkdir /data/repo/centos6/{centos6-base,centos6-updates,centos6-extras}
    mkdir /data/repo/centos7/{base,updates,extras}
  • 同步官方仓库到本地

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # 同步 CentOS6 源
    mv /etc/yum.repos.d/CentOS-7.repo /etc/yum.repos.d/CentOS-7.repo.bak
    reposync -r centos6-base -r centos6-updates -r centos6-extras -p /data/repo/centos6 --download-metadata --newest-only
    mv /etc/yum.repos.d/CentOS-7.repo.bak /etc/yum.repos.d/CentOS-7.repo
    
    # 同步 CentOS7 源
    mv /etc/yum.repos.d/CentOS-6.repo /etc/yum.repos.d/CentOS-6.repo.bak
    reposync -r base -r updates -r extras -p /data/repo/centos7 --download-metadata --newest-only
    mv /etc/yum.repos.d/CentOS-6.repo.bak /etc/yum.repos.d/CentOS-6.repo

    -r:指定 repo id(执行yum repolist all输出内容的第一列)

    –download-metadata:同步 repodata

    –newest-only:只保留最新版本,节省空间

    如果没加--download-metadata,则执行:createrepo /data/repo/centos7/base

  • 定时同步

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    cat > /usr/local/bin/yumrepo_sync.sh <<'EOF'
    #!/bin/bash
    
    set -e
    LOCK_FILE="/var/run/yumrepo_sync.lock"
    LOG_FILE="/var/log/yumrepo_sync.log"
    
    exec >> $LOG_FILE 2>&1
    
    # 防止重复执行
    if [ -f "$LOCK_FILE" ]; then
      echo "$(date '+%F %T') already running"
      exit 1
    fi
    
    touch $LOCK_FILE
    echo "========== $(date '+%F %T') START =========="
    
    # === 同步 CentOS 6 ===
    if [ -f /etc/yum.repos.d/CentOS-7.repo ]; then
      mv /etc/yum.repos.d/CentOS-7.repo /etc/yum.repos.d/CentOS-7.repo.bak
    fi
    
    reposync \
      -r centos6-base \
      -r centos6-updates \
      -r centos6-extras \
      -p /data/repo/centos6 \
      --download-metadata \
      --newest-only
    
    if [ -f /etc/yum.repos.d/CentOS-7.repo.bak ]; then
      mv /etc/yum.repos.d/CentOS-7.repo.bak /etc/yum.repos.d/CentOS-7.repo
    fi
    
    # === 同步 CentOS 7 ===
    if [ -f /etc/yum.repos.d/CentOS-6.repo ]; then
      mv /etc/yum.repos.d/CentOS-6.repo /etc/yum.repos.d/CentOS-6.repo.bak
    fi
    
    reposync \
      -r base \
      -r updates \
      -r extras \
      -p /data/repo/centos7 \
      --download-metadata \
      --newest-only
    
    if [ -f /etc/yum.repos.d/CentOS-6.repo.bak ]; then
      mv /etc/yum.repos.d/CentOS-6.repo.bak /etc/yum.repos.d/CentOS-6.repo
    fi
    
    echo "========== $(date '+%F %T') END =========="
    rm -f $LOCK_FILE
    EOF

    chmod +x /usr/local/bin/yumrepo_sync.sh

    1
    
    0 3 * * * /usr/local/bin/yumrepo_sync.sh

客户端配置

  • CentOS6

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    [root@Yum-Repositories ~]# cat ncentos6.repo
    [base]
    name=centos6-base
    baseurl=http://192.168.1.125/centos6/centos6-base/Packages/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    [extras]
    name=centos6-extras
    baseurl=http://192.168.1.125/centos6/centos6-extras/Packages/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
    [updates]
    name=centos6-updates
    baseurl=http://192.168.1.125/centos6/centos6-updates/Packages/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
  • CentOS7

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    [root@Yum-Repositories ~]# cat ncentos7.repo
    [base]
    name=centos7-base
    baseurl=http://192.168.1.125/centos7/base/Packages/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    [extras]
    name=centos7-extras
    baseurl=http://192.168.1.125/centos7/extras/Packages/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
    [updates]
    name=centos7-updates
    baseurl=http://192.168.1.125/centos7/updates/Packages/
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

配置 Nginx

  • 下载 Fancy Index

    1
    2
    
    cd /usr/local/src/
    git clone https://github.com/aperezdc/ngx-fancyindex.git
  • 下载 Nginx-Fancyindex-Theme

    1
    2
    
    cd /usr/local/src/
    git clone git clone https://github.com/alehaa/nginx-fancyindex-flat-theme.git
  • 下载 Nginx

    1
    2
    3
    
    cd /usr/local/src/
    wget https://nginx.org/download/nginx-1.26.3.tar.gz
    tar xf nginx-1.26.3.tar.gz
  • 安装 Nginx

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # 创建 nginx 用户
    useradd -s /sbin/nologin nginx
    
    # 配置
    cd nginx-1.26.3
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-stream --with-pcre --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/ngx-fancyindex
    
    # 编译安装
    make && make install
  • 配置 Nginx

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    vim /usr/local/nginx/conf/vhost/fancyindex.conf
    
    server {
        listen      80;
        server_name 192.168.1.125;
    
        location / {
            root /data/repo;
    
            # 使用 fancyindex
            fancyindex on;
    
            # 不显示精确大小
            fancyindex_exact_size off;
    
            # 文件日期
            fancyindex_time_format "%Y-%m-%d %H:%M";
    
            # 使用用户本地时间
            fancyindex_localtime on;
    
            fancyindex_header "/theme/header.html";
            fancyindex_footer "/theme/footer.html";
        }
    
        location /theme/ {
            alias /usr/local/nginx/theme/;
        }
    
    }

    拷贝主题

    1
    2
    
    mkdir /usr/local/nginx/theme
    cp -r /usr/local/src/nginx-fancyindex-flat-theme/layout/* /usr/local/nginx/theme/
  • 启动 Nginx

    1
    2
    
    /usr/local/nginx/sbin/nginx -t
    /usr/local/nginx/sbin/nginx