概述

不管内部大量部署服务器还是私有云环境,为了能够不受互联网速度和带宽的限制,搭建一个本地yum源是很必要的,尤其在内网环境下。其中epel源是最主要的一个,也是用的最多的,本例介绍在Centos7环境下搭建epel源。

环境准备

操作系统 CentOS7
组建 Apache
Epel源目录 /var/www/html/
磁盘空间 大于等于50G

实施步骤

  1. 系统准备好后通过wget,从fedora网站下载epel源,比如我要下载centos7的epel源。下载过程可能会较慢,可以选择空闲时间慢慢下。
1
#wget -np -H --cut-dirs=0 -r -c -L http://dl.fedoraproject.org/pub/epel/7/x86_64  -P /var/www/html/

2.重启apache服务,然后通过浏览器测试,是否能打开浏览,http://1.1.1.1/pub/epel/7/x86_64在客户机 /etc/yum.repos.d/下建立epel.repo文件,内容如下:

1
2
3
4
[epel]
name=epel
baseurl=http://1.1.1.1/pub/epel/7/x86_64
enabled=1
  1. 在客户机上验证:
1
#yum repolist

后记:

其他的yum源可以通过同样的方法进行添加。

Bash脚本同步CentOS 6/7镜像

下面的脚本将同步CentOS 6/CentOS 7镜像,用你想要的替换目录变量,将脚本保存到文件中,make:

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then

echo "Updates via rsync already running."

exit 0

fi

base_dir="/var/mirrors/centos/"

centos_7_mirror_dir="/var/mirrors/centos/7/"

centos_6_mirror_dir="/var/mirrors/centos/6/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$centos_7_mirror_dir"  && -d "$centos_6_mirror_dir" ]] ; then

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/7/  "$centos_7_mirror_dir" && \

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/6/  "$centos_6_mirror_dir" && \

    # sync keys

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-7  "$base_dir" && \

    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-6  "$base_dir" && \

    rm -rf /var/lock/subsys/rsync_updates

else

echo "Directories doesn't exist"

fi

用于同步Epel镜像的Bash脚本

 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
#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then

echo "Updates via rsync already running."

exit 0

fi

epel6_mirror_dir="/var/mirrors/epel/6/x86_64"

epel7_mirror_dir="/var/mirrors/epel/7/x86_64"

base_dir="/var/mirrors/epel/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$epel6_mirror_dir"  && -d "$epel7_mirror_dir" ]] ; then

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/6/x86_64/ "$epel6_mirror_dir" && \

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/7/x86_64/ "$epel7_mirror_dir" && \

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-7 "$base_dir" && \

    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-6 "$base_dir" && \

    rm -rf /var/lock/subsys/rsync_updates

else

echo "Directories doesn't exist"

fi

if [[ $? -eq '0' ]]; then

echo ""

echo "Sync successful.."

else

echo " Syncing failed"

exit 1

fi

这些脚本可以在cron中运行,以保持镜像最新。

用Nginx服务镜像

同步完成后,你可以通过使用nginx开始使用它们。

安装nginx:

yum -y install epel-release

yum -y install nginx

然后添加配置文件:

cat /etc/nginx/conf.d/centos.conf

server {

listen 80;

server_name centos.mirrors.domain.com;

root /var/mirrors/centos/;

location / {

autoindex on;

}

}

对于epel:

cat /etc/nginx/conf.d/epel.conf

server {

listen 80;

server_name epel.mirrors.domain.com;

root /var/mirrors/epel;

location / {

autoindex on;

}

}

启动并启用nginx:

systemctl start nginx

systemctl enable nginx

配置CentOS Server以使用镜像

CentOS Base镜像:

cd /etc/yum.repos.d/

mkdir old

mv CentOS* old

vim centos.repo

添加以下内容:

[base]

name=CentOS-$releasever - Base

baseurl=http://centos.mirrors.domain.com/$releasever/os/$basearch/

enabled=1

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[updates]

name=CentOS-$releasever - Updates

baseurl=http://centos.mirrors.domain.com/$releasever/updates/$basearch/

enabled=1

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[extras]

name=CentOS-$releasever - Extras

baseurl=http://centos.mirrors.domain.com/$releasever/extras/$basearch/

enabled=0

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[centosplus]

name= CentOS-$releasever - Plus

baseurl=http://centos.mirrors.domain.com/$releasever/centosplus/$basearch/

enabled=0

gpgcheck=1

gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

通过更新repo缓存来测试repos是否正常工作:

yum clean all

yum makecache fast

yum -v repolist

Epel Mirrors:

# cat /etc/yum.repos.d/epel.repo

[epel]

name=Extra Packages for Enterprise Linux 7 - $basearch

baseurl=http://epel.mirrors.domain.com/$releasever/$basearch/

enabled=1

gpgcheck=1

gpgkey=http://epel.mirrors.domain.com/RPM-GPG-KEY-EPEL-$releasever