wangbin
  • wangbin
  • 2018-10-07
  • IT

nginx搭建yum源镜像

一. 简介

yum是从yum软件仓库获取rpm包来安装的,yum软件仓库通常被称为yum源,我们常用的有官方源和epel源两个。

不过出于以下几个原因:

  1. 官方的yum源是搭在国外的,国内访问速度不理想
  2. 内部机器众多,同时更新可能造成出口网络拥堵
  3. 内网无法访问互联网

我们需要本地搭建yum源的镜像,这样不仅可以提高访问速度,而且也不用担心没有网络的情况。

搭建yum源镜像也很简单,不过占用的硬盘空间比较大,正好买了Hostens家两年的1T硬盘vps一直没找到用处,搭个玩玩。

二. 同步源数据

这里我们搭建的是CentOS 7 Base源镜像,只需要执行下面命令就可以同步数据啦

# CentOS-Base.repo

mkdir -p /vps/hosts/mirrors/centos

# base
rsync -avrt --delete \
    --exclude-from '/save/install/common/conf/yum/yum-exclude.txt' \
    rsync://mirrors.kernel.org/mirrors/centos/7/os/ \
    /vps/hosts/mirrors/centos/7/os/

# updates
rsync -avrt --delete \
    --exclude-from '/save/install/common/conf/yum/yum-exclude.txt' \
    rsync://mirrors.kernel.org/mirrors/centos/7/updates/ \
    /vps/hosts/mirrors/centos/7/updates/

# extras
rsync -avrt --delete \
    --exclude-from '/save/install/common/conf/yum/yum-exclude.txt' \
    rsync://mirrors.kernel.org/mirrors/centos/7/extras/ \
    /vps/hosts/mirrors/centos/7/extras/

# centosplus
rsync -avrt --delete \
    --exclude-from '/save/install/common/conf/yum/yum-exclude.txt' \
    rsync://mirrors.kernel.org/mirrors/centos/7/centosplus/ \
    /vps/hosts/mirrors/centos/7/centosplus/

其中/save/install/common/conf/yum/yum-exclude.txt文件保存的是我们同步需要忽略的文件夹路径,内容如下

#### rsync://mirrors.kernel.org/mirrors/centos/7/
# rsync://mirrors.kernel.org/mirrors/centos/7/os
# os
x86_64/LiveOS
x86_64/images/

# rsync://mirrors.kernel.org/mirrors/centos/7/updates
# updates
# 没有忽略的文件夹

# rsync://mirrors.kernel.org/mirrors/centos/7/extras
# extras
# 没有忽略的文件夹

# rsync://mirrors.kernel.org/mirrors/centos/7/centosplus
# centosplus
# 没有忽略的文件夹

执行完,大概会占用42G的硬盘空间。

三. nginx配置

下面配置nginx

# mirrors.wangbin.io;
server {

    listen       50443 ssl http2;
    listen       [::]:50443 ssl http2;

    server_name  mirrors.wangbin.io;

    access_log  logs/wangbin.io/access-mirrors.wangbin.io.log siyou325;
    error_log   logs/wangbin.io/error.log;

    ssl_certificate             /vps/certificate/acme/*.wangbin.io/fullchain.cer;
    ssl_certificate_key         /vps/certificate/acme/*.wangbin.io/*.wangbin.io.key;
    ssl_trusted_certificate     /vps/certificate/acme/*.wangbin.io/fullchain.cer;

    root         /vps/hosts/mirrors;
    index        index.html index.htm index.php;

    location ~ ^(.*)/$ {
        charset utf-8;
        autoindex on;
        autoindex_localtime on;             #显示的文件时间为文件的服务器时间
        autoindex_exact_size off;           #改为off后,显示出文件的大概大小,单位是kB或者MB或者GB
    }

    # 禁止访问.svn目录,防止svn信息泄漏,必加项
    location ~ ^(.*)\/\.svn\/ {
        deny all;
    }

}

完成后执行

nginx -t
nginx -s reload

nginx就配置完成啦

四. siyou325-CentOS-Base.repo

siyou325-CentOS-Base.repo

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://mirrors.wangbin.io/centos/$releasever/os/$basearch/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://mirrors.wangbin.io/centos/$releasever/updates/$basearch/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://mirrors.wangbin.io/centos/$releasever/extras/$basearch/
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirrors.wangbin.io/centos/$releasever/centosplus/$basearch/
gpgcheck=0
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

用siyou325-CentOS-Base.repo替换/etc/yum.repos.d/CentOS-Base.repo,命令如下

mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/bak
mv siyou325-CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo

完成后执行

yum clean all
yum makecache

五. 结尾

好啦,安装个软件试试吧。

参考:

  1. https://www.cnblogs.com/weijing24/p/5638725.html

  2. http://www.zyops.com/autodeploy-yum/