搭建CentOS在线yum源镜像服务器

前端之家收集整理的这篇文章主要介绍了搭建CentOS在线yum源镜像服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

搭建CentOS在线yum源镜像服务器


准备篇:

  • 系统环境:

    操作系统:CentOS6.x

    IP地址:192.168.12.128(ip地址随意,DHCP还是静态的都行,只要能联网就行)

  • 实现目的:同步CentOS镜像站点内容到此服务器,并通过配置http服务器,能够向外网或者内网提供yum服务


一、安装http服务器

这里使用Nginx服务器搭建,CentOS安装配置LNMP服务器参考:http://www.osyunwei.com/archives/5910.html


二、站点目录位置


三、开启Nginx目录浏览功能

# vi /usr/local/Nginx/conf/Nginx.conf #编辑配置文件,在http {下面添加以下内容
autoindex on; #开启Nginx目录浏览功能
autoindex_exact_size off; #文件大小从KB开始显示
autoindex_localtime on; #显示文件修改时间为服务器本地时间


:wq! #保存,退出

# service Nginx reload #重新加载配置
参考:Nginx开启目录浏览功能: http://www.osyunwei.com/archives/5051.html


安装篇:

一、创建镜像文件存放位置

mkdir -p /usr/local/Nginx/html/centos #CentOS官方标准源
mkdir -p /usr/local/Nginx/html/repoforge #第三方rpmforge源
mkdir -p /usr/local/Nginx/html/epel #第三方epel源
说明:这里创建三个文件夹,分别存放CentOS官方标准源、第三方的rpmforge源和epel源


二、确定以上三个yum源上游源的同步镜像地址

  • CentOS官方标准源:rsync://mirrors.ustc.edu.cn/centos/

  • rpmforge源:rsync://mirrors.ispros.com.bd/repoforge/

  • epel源:rsync://mirrors.ustc.edu.cn/epel/

备注:上游yum源必须要支持rsync协议,否则不能使用rsync进行同步。

参考:

CentOS官方标准源:

rsync://mirrors.kernel.org/centos

rpmforge源

http://apt.sw.be/

rsync://ftp-stud.fht-esslingen.de/dag

epel源:

http://mirrors.fedoraproject.org/publiclist/EPEL/

rsync://mirrors.kernel.org/fedora-epel


三、创建以上三个yum源同步脚本,并且设定脚本自动执行

# mkdir -p /home/crontab #创建目录

# vi /home/crontab/yum_rsync.sh #添加以下代码

#!/bin/sh

/usr/bin/rsync -avrt rsync://mirrors.ustc.edu.cn/centos/ --exclude-from=/usr/local/Nginx/html/exclude_centos.list /usr/local/Nginx/html/centos/

/usr/bin/rsync -avrt rsync://mirrors.ispros.com.bd/repoforge/ --exclude-from=/usr/local/Nginx/html/exclude_repoforge.list /usr/local/Nginx/html/repoforge/

/usr/bin/rsync -avrt rsync://mirrors.ustc.edu.cn/epel/ --exclude-from=/usr/local/Nginx/html/exclude_epel.list /usr/local/Nginx/html/epel/

:wq! #保存退出


# chmod +x /home/crontab/yum_rsync.sh #添加脚本执行权限

备注:运行此脚本前,先要创建好同步目录及不需要同步的目录列表文件

# cd /usr/local/Nginx/html/ #进入目录

# touch exclude_centos.list #创建文件

# touch exclude_repoforge.list #创建文件

# touch exclude_epel.list #创建文件


把不需要同步的目录写到上面对应的文件中,每行一个目录,例如:

# vi exclude_epel.list

# 4/

# 4AS/

# 4ES/

# 4WS/

:wq! #保存退出


四、添加脚本定时执行任务

# vi /etc/crontab #在最后一行添加以下代码

0 1 * * * root /home/crontab/yum_rsync.sh #设置每天凌晨1点整开始执行脚本

:wq! #保存退出

# service crond restart #重启


测试篇:

一、安装rsync同步软件:

# yum install rsync xinetd #安装

# vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync

disable = no #修改

# /etc/init.d/xinetd start #启动(CentOS中是以xinetd 来管理Rsync服务的)

:wq! #保存退出


二、执行同步脚本

# sh /home/crontab/yum_rsync.sh

注意:等待脚本执行完毕,首次同步,耗费的时间比较长!


三、根据不同版本创建三个yum源的repo配置文件

# cd /etc/yum.repos.d/ #进入目录

# mv /etc/yum.repos.d/CentOS-Base.repo CentOS-Base.repo-bak


CentOS官方标准源:

1.CentOS 5.x系列:

# vi /etc/yum.repos.d/CentOS-Base.repo #添加以下代码

[base]

name=CentOS-$releasever - Base - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-5

#released updates

[updates]

name=CentOS-$releasever - Updates - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-5

#packages used/produced in the build but not released

[addons]

name=CentOS-$releasever - Addons - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/addons/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-5

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-5

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-5

#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/contrib/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-5


:wq! #保存退出


2. CentOS 6.x系列:

# vi /etc/yum.repos.d/CentOS-Base.repo #添加以下代码

[base]

name=CentOS-$releasever - Base - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#released updates

[updates]

name=CentOS-$releasever - Updates - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/contrib/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6


:wq! #保存退出


3. CentOS 7.x系列:

# vi /etc/yum.repos.d/CentOS-Base.repo #添加以下代码

[base]

name=CentOS-$releasever - Base

baseurl=http://192.168.21.188/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-7

#released updates

[updates]

name=CentOS-$releasever - Updates

baseurl=http://192.168.21.188/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/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://192.168.21.188/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus

baseurl=http://192.168.21.188/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-7

:wq! #保存退出


备注:或者参考:https://lug.ustc.edu.cn/wiki/mirrors/help/centos

把里面的http://mirrors.ustc.edu.cn/替换为http://192.168.21.188/


rpmforge源:

1.CentOS 5.x系列:

# vi /etc/yum.repos.d/rpmforge.repo #添加以下代码

[rpmforge]

name = RHEL $releasever - RPMforge.net - dag

baseurl = http://192.168.21.188/repoforge/redhat/el5/en/$basearch/rpmforge

enabled = 1

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-extras]

name = RHEL $releasever - RPMforge.net - extras

baseurl = http://192.168.21.188/repoforge/redhat/el5/en/$basearch/extras

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-testing]

name = RHEL $releasever - RPMforge.net - testing

baseurl = http://192.168.21.188/repoforge/redhat/el5/en/$basearch/testing

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1


:wq! #保存退出


2.CentOS 6.x系列:

# vi /etc/yum.repos.d/rpmforge.repo #添加以下代码

[rpmforge]

name = RHEL $releasever - RPMforge.net - dag

baseurl = http://192.168.21.188/repoforge/redhat/el6/en/$basearch/rpmforge

enabled = 1

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-extras]

name = RHEL $releasever - RPMforge.net - extras

baseurl = http://192.168.21.188/repoforge/redhat/el6/en/$basearch/extras

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-testing]

name = RHEL $releasever - RPMforge.net - testing

baseurl = http://192.168.21.188/repoforge/redhat/el6/en/$basearch/testing

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1


:wq! #保存退出


3.CentOS 7.x系列:

# vi /etc/yum.repos.d/rpmforge.repo #添加以下代码

[rpmforge]

name = RHEL $releasever - RPMforge.net - dag

baseurl = http://192.168.21.188/repoforge/redhat/el7/en/$basearch/rpmforge

enabled = 1

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-extras]

name = RHEL $releasever - RPMforge.net - extras

baseurl = http://192.168.21.188/repoforge/redhat/el7/en/$basearch/extras

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-testing]

name = RHEL $releasever - RPMforge.net - testing

baseurl = http://192.168.21.188/repoforge/redhat/el7/en/$basearch/testing

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1


:wq! #保存退出


epel源:

1.CentOS 5.x系列:

# vi /etc/yum.repos.d/epel.repo #添加以下代码

[epel]

name=Extra Packages for Enterprise Linux 5 - $basearch

baseurl=http://192.168.21.188/epel/5/$basearch

failovermethod=priority

enabled=1

gpgcheck=1

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-5

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 5 - $basearch - Debug

baseurl=http://192.168.21.188/epel/5/$basearch/debug

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-5

gpgcheck=1

[epel-source]

name=Extra Packages for Enterprise Linux 5 - $basearch - Source

baseurl=http://192.168.21.188/epel/5/SRPMS

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-5

gpgcheck=1

:wq! #保存退出


2.CentOS 6.x系列:

# vi /etc/yum.repos.d/epel.repo #添加以下代码

[epel]

name=Extra Packages for Enterprise Linux 6 - $basearch

baseurl=http://192.168.21.188/epel/6/$basearch

failovermethod=priority

enabled=1

gpgcheck=1

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-6

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 6 - $basearch - Debug

baseurl=http://192.168.21.188/epel/6/$basearch/debug

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-6

gpgcheck=1

[epel-source]

name=Extra Packages for Enterprise Linux 6 - $basearch - Source

baseurl=http://192.168.21.188/epel/6/SRPMS

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-6

gpgcheck=1


:wq! #保存退出


3.CentOS 7.x系列:

# vi /etc/yum.repos.d/epel.repo #添加以下代码

[epel]

name=Extra Packages for Enterprise Linux 7 - $basearch

baseurl=http://192.168.21.188/epel/beta/7/$basearch

failovermethod=priority

enabled=1

gpgcheck=1

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 7 - $basearch - Debug

baseurl=http://192.168.21.188/epel/beta/7/$basearch/debug

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-7

gpgcheck=1

[epel-source]

name=Extra Packages for Enterprise Linux 7 - $basearch - Source

baseurl=http://192.168.21.188/epel/beta/7/SRPMS

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-7

gpgcheck=1


:wq! #保存退出


四、测试yum源是否配置正确

当前系统版本为CentOS 6.x,以此为例

1. 添加CentOS-Base.repo

# cd /etc/yum.repos.d/

# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

# vi CentOS-Base.repo #添加以下代码


[base]

name=CentOS-$releasever - Base - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/os/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#released updates

[updates]

name=CentOS-$releasever - Updates - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/updates/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/extras/$basearch/

gpgcheck=1

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/centosplus/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6

#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - huanqiu.com

baseurl=http://192.168.21.188/centos/$releasever/contrib/$basearch/

gpgcheck=1

enabled=0

gpgkey=http://192.168.21.188/centos/RPM-GPG-KEY-CentOS-6


:wq! #保存退出


2. 添加rpmforge.repo


# vi rpmforge.repo #添加以下代码


[rpmforge]

name = RHEL $releasever - RPMforge.net - dag

baseurl = http://192.168.21.188/repoforge/redhat/el6/en/$basearch/rpmforge

enabled = 1

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-extras]

name = RHEL $releasever - RPMforge.net - extras

baseurl = http://192.168.21.188/repoforge/redhat/el6/en/$basearch/extras

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1

[rpmforge-testing]

name = RHEL $releasever - RPMforge.net - testing

baseurl = http://192.168.21.188/repoforge/redhat/el6/en/$basearch/testing

enabled = 0

protect = 0

gpgkey=http://192.168.21.188/repoforge/RPM-GPG-KEY-rpmforge

gpgcheck = 1


:wq! #保存退出


3. 添加epel.repo

# vi epel.repo #添加以下代码


[epel]

name=Extra Packages for Enterprise Linux 6 - $basearch

baseurl=http://192.168.21.188/epel/6/$basearch

failovermethod=priority

enabled=1

gpgcheck=1

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-6

[epel-debuginfo]

name=Extra Packages for Enterprise Linux 6 - $basearch - Debug

baseurl=http://192.168.21.188/epel/6/$basearch/debug

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-6

gpgcheck=1

[epel-source]

name=Extra Packages for Enterprise Linux 6 - $basearch - Source

baseurl=http://192.168.21.188/epel/6/SRPMS

failovermethod=priority

enabled=0

gpgkey =http://192.168.21.188/epel/RPM-GPG-KEY-EPEL-6

gpgcheck=1


wq! #保存退出


4. 清除缓存,列出可用源软件包

# yum clean all #清除当前yum缓存

# yum makecache #缓存yum源中的软件包信息

# yum repolist #列出yum源中可用的软件包


5. 使用yum命令安装软件

# yum install PHP #测试CentOS官方标准源

# yum install htop #测试rpmforge源

# yum install Nginx #测试epel源


至此,搭建CentOS在线yum源镜像服务器完成

此教程参考: http://www.centoscn.com/CentosServer/test/2014/0904/3675.html

也是可以将同步下来的yum源制作成ISO镜像的,这个我制作了epel源的iso,大家有兴趣可以试下,方法网上找下吧,我这当初做忘了整理文档了。

猜你在找的CentOS相关文章