自建Debian/CentOS镜像站

搭建Debian镜像站

一、使用场景

自建镜像站基本有两大使用场景:

  • 提升软件包拉取速度,安装在有大带宽的机器上提供服务;
  • 局域网客户端无外网访问能力,需搭建内网镜像站作为源站。

二、安装apt-mirror

这里以Debian streach为例,其他版本同理,服务器所用的系统并不需要和搭建的镜像站匹配,一个系统上可以搭建多个操作系统、不同代号版本的镜像站,你可以理解为镜像站只是基于HTTP服务提供文件下载、实时更新的功能即可。

  • apt-get install apt-mirror

三、配置镜像站源站

配置文件所在路径:/etc/apt/mirror.list

设置源站地址,即从哪里同步拉取镜像仓库,这里以清华源为例,设置源站为Debian11 bullseye版本的源站地址:

  • ############# config ##################
  • #
  • # set base_path /var/spool/apt-mirror
  • #
  • # set mirror_path $base_path/mirror
  • # set skel_path $base_path/skel
  • # set var_path $base_path/var
  • # set cleanscript $var_path/clean.sh
  • # set defaultarch <running host architecture>
  • # set postmirror_script $var_path/postmirror.sh
  • # set run_postmirror 0
  • set nthreads 20
  • set _tilde 0
  • #
  • ############# end config ##############
  • deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
  • deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
  • deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
  • deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
  • # mirror additional architectures
  • #deb-alpha http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-amd64 http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-armel http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-hppa http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-i386 http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-ia64 http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-m68k http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-mips http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-mipsel http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-powerpc http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-s390 http://ftp.us.debian.org/debian unstable main contrib non-free
  • #deb-sparc http://ftp.us.debian.org/debian unstable main contrib non-free
  • clean http://ftp.cn.debian.org/debian
展开

四、设置定时任务

增加一个定时任务,每天定时向上游源(清华源)同步更新镜像站

  • crontab -e

写入:

  • 0 23 * * * apt-mirror #每天23点执行一次apt-mirror,定时向上游更新镜像站文件
  • 0 22 * * * /var/spool/apt-mirror/var/clean.sh #提前一个小时清理被上游镜像站遗弃的软件包

如果想立马拉取,临时改成特定时间点即可,或者后台运行(第一次拉取会花很长时间,不建议前台执行):

  • nohup apt-mirror &

五、修正apt-mirror脚本bug

apt-mirror有个小bug,可以参考bug report页面,它不处理.xz后缀的压缩文件,会导致同步后有些版本仓库404,比如bullseye-backports

修正方法也很简单:

  • vim $(which apt-mirror) #修改apt-mirror脚本

大概在第523行,正则匹配后面加上.xz后缀情况就行:

自建Debian/CentOS镜像站

  • #修改前:
  • if ( $filename =~
  • m{^$component/i18n/Translation-[^./]*.bz2$} )
  • #修改后:
  • if ( $filename =~
  • m{^$component/i18n/Translation-[^./]*.(bz2|xz)$} )

或者使用sed命令写入即可:

自建Debian/CentOS镜像站

  • sed -ine '523s/bz2/(bz2|xz)/g' $(which apt-mirror)

六、拉取路径配置与web

apt-mirror拉取镜像站默认存储到的路径为:/var/spool/apt-mirror,通过/etc/apt/mirror.listset base_path指定,如果需要修改路径,修改此参数即可。

也可以将拉取的路径设置一个软链接到web路径:

  • ln -sf /var/spool/apt-mirror/mirrors.tuna.tsinghua.edu.cn/ /var/www/mirror

之后在web配置文件里指定路径为/var/www/mirror即可,同时需要开启允许列出目录文件,否则会403,以Nginx为例:

  • root /var/www/mirror;
  • location / {
  • autoindex on;
  • }

自建Debian/CentOS镜像站

七、设置源站为自建站

  • $ sudo vim /etc/apt/sources.list #修改成自建站
  • $ sudo apt-get update #更新软件源

自建Debian/CentOS镜像站

八、拉取软件效果

完成后用浏览器访问效果如图:

自建Debian/CentOS镜像站
自建Debian/CentOS镜像站

拉取速度:

自建Debian/CentOS镜像站

内网速度越快提升则越明显。

搭建Centos镜像站

一、使用场景

自建镜像站基本有两大使用场景:

  • 提升软件包拉取速度,安装在有大带宽的机器上提供服务;
  • 局域网客户端无外网访问能力,需搭建内网镜像站作为源站。

二、同步脚本与定时任务配置

Centos的镜像站搭建更为简单,只需要通过rsync将软件同步到本地仓库就行。

以centos7即epel源为例,如需拉取多个版本,甚至全版本,修改版本号或某个大目录即可。

创建一个同步脚本centos_rsync.sh

  • $ mkdir -p /server/scripts/
  • $ cd $_
  • $ vim centos_rsync.sh
  • #!/bin/bash
  • rsync -avrt rsync://mirrors4.tuna.tsinghua.edu.cn/epel/7/ /var/www/mirror/centos/epel/7/
  • rsync -avrt rsync://mirrors4.tuna.tsinghua.edu.cn/centos/7/ /var/www/mirror/centos/7/
  • $ chmod + x centos_rsync.sh

之后写入到定时任务即可:

  • crontab -e

内容如下:

  • 00 12 * * * /server/scripts/centos_rsync.sh #每天中午12点执行更新与同步

三、拉取路径配置与web

与Debian同理,将拉取的路径设置为web路径即可,确保客户端能通过HTTP方式GET到文件。

以Nginx为例,配置对应路径,开启autoindex

  • root /var/www/mirror;
  • location / {
  • autoindex on;
  • }

四、修改源站为自建站

1.修改base源

域名替换成自建的镜像站地址即可:

  • $ cd /etc/yum.repos.d/
  • $ vim CentOS-Base.repo
  • [base]
  • name=CentOS-$releasever - Base
  • baseurl=https://mirrors.linux-code.com/centos/$releasever/os/$basearch/
  • gpgcheck=1
  • gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • #released updates
  • [updates]
  • name=CentOS-$releasever - Updates
  • baseurl=https://mirrors.linux-code.com/centos/$releasever/updates/$basearch/
  • gpgcheck=1
  • gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • #additional packages that may be useful
  • [extras]
  • name=CentOS-$releasever - Extras
  • baseurl=https://mirrors.linux-code.com/centos/$releasever/extras/$basearch/
  • gpgcheck=1
  • 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=https://mirrors.linux-code.com/centos/$releasever/centosplus/$basearch/
  • gpgcheck=1
  • enabled=0
  • gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • $

2.修改epel源

将域名替换成自建的镜像站地址即可:

  • $ cd /etc/yum.repos.d/
  • $ vim epel-7.repo
  • [epel]
  • name=Extra Packages for Enterprise Linux 7 - $basearch
  • baseurl=https://mirrors.linux-code.com/centos/epel/7/$basearch
  • failovermethod=priority
  • enabled=1
  • gpgcheck=0
  • gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  • [epel-debuginfo]
  • name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
  • baseurl=https://mirrors.linux-code.com/centos/epel/7/$basearch/debug
  • failovermethod=priority
  • enabled=0
  • gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  • gpgcheck=0
  • [epel-source]
  • name=Extra Packages for Enterprise Linux 7 - $basearch - Source
  • baseurl=https://mirrors.linux-code.com/centos/epel/7/SRPMS
  • failovermethod=priority
  • enabled=0
  • gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  • gpgcheck=0
  • $

3.生成缓存

删除之前的缓存,并生成新缓存:

  • yum clean all
  • yum makecache

自建Debian/CentOS镜像站

五、拉取软件效果

完成后用浏览器访问效果如图:

自建Debian/CentOS镜像站
自建Debian/CentOS镜像站

拉取速度:

自建Debian/CentOS镜像站

写在最后

自建镜像站,需要足够大的磁盘,同时磁盘读写能力、内网传输速度、系统负载、内存(buffer很重要)大小都会影响软件拉取速度。

单个版本,如Debian11Centos7+epel,给500G左右空间就好,如果涉及到全版本,建议上4-8T NAS盘。

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
<<上一篇
下一篇>>
文章目录

搭建Debian镜像站

一、使用场景

二、安装apt-mirror

三、配置镜像站源站

四、设置定时任务

五、修正apt-mirror脚本bug

六、拉取路径配置与web

七、设置源站为自建站

八、拉取软件效果

搭建Centos镜像站

一、使用场景

二、同步脚本与定时任务配置

三、拉取路径配置与web

四、修改源站为自建站

五、拉取软件效果

写在最后

关闭
目 录