CentOS7下的YUM源服务器搭建

注意:环境要求

'环境准备,修改hostname,关闭防火墙,disabled selinux' 
[root@localhost ~]# hostnamectl set-hostname --static yum-server
[root@yum-server ~]# systemctl disable firewalld --now
[root@yum-server ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux

配置服务器端yum

安装yum源工具

[root@yum-server ~]# yum -y install epel-release.noarch     # nginx需要epel源
[root@yum-server ~]# yum -y install nginx    # 安装nginx
[root@yum-server ~]# createrepo yum-utils    # 安装repository管理工具

配置nginx

[root@yum-server nginx]# cd /etc/nginx/
[root@yum-server nginx]# cp nginx.conf{,.bak}    # 备份!备份!备份!
[root@yum-server nginx]# vim nginx.conf
    server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
        # 在server段加入以下三段内容
        autoindex on;              # 表示:自动在index.html的索引打开
        autoindex_exact_size on;   # 表示:如果有文件,则显示文件的大小
        autoindex_localtime on;    # 表示:显示更改时间,以当前系统的时间为准

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    
[root@yum-server nginx]# nginx -t     # 检测一下nginx语法是否有错
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@yum-server nginx]# systemctl enable nginx.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

[root@yum-server nginx]# systemctl enable nginx.service --now  # 启动nginx,设为开机自启
[root@yum-server nginx]# curl -I http://localhost    # 访问本地,状态码返回200,服务正常
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Sun, 05 Jul 2020 09:48:05 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes

配置nginx页面目录

[root@yum-server nginx]# cd /usr/share/nginx/html/
[root@yum-server html]# mkdir -p CentOS-YUM/Aliyun/{version_6,version_7}/64bit
[root@yum-server html]# tree /usr/share/nginx/html/CentOS-YUM/
/usr/share/nginx/html/CentOS-YUM/
└── Aliyun
    ├── version_6
    │   └── 64bit
    └── version_7
        └── 64bit

5 directories, 0 files
[root@yum-server html]# cd CentOS-YUM/
[root@yum-server CentOS-YUM]# vim index.html
<p style="font-weight:bolder;color:green;font-size:30px;">ALL of the packages in the below:</p>
<br/>

<a href="http://192.168.57.133/CentOS-YUM/Aliyun">version_6</a><br/>

These packagers using for Centos 6<br/>

<a href="http://192.168.57.133/CentOS-YUM/Aliyun">version_7</a><br/>

These packagers using for Centos 7<br/>

<p style="font-weight:bolder;color:red;font-size:18px;">Please replace the file and fill in the f    ollowing content:</p>
<p style="font-weight:bolder;color:blue;font-size:15px;">Way: /etc/yum.repos.d/CentOS-Base.repo</    p>

替换yum源文件

# 备份原来的官方yum源
[root@yum-server CentOS-YUM]# cd /etc/yum.repos.d/
[root@yum-server yum.repos.d]# mv ./* /tmp/
[root@yum-server yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo    # 下载aliyun的centos7的yum源
[root@yum-server yum.repos.d]# vim yum.reposync.sh    # 同步脚本
#!/usr/bin/bash
reposync -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_7/64bit/

/usr/bin/sed -i "s/7/6/g" `grep 7 -rl /etc/yum.repos.d/CentOS-Base.repo`

reposync -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_6/64bit/

/usr/bin/sed -i "s/6/7/g" `grep 6 -rl /etc/yum.repos.d/CentOS-Base.repo`

[root@yum-server yum.repos.d]# chmod +x yum.reposync.sh   # 要给执行权限
[root@yum-server yum.repos.d]# ll
total 8
-rw-r--r-- 1 root root 2523 Jun 16  2018 CentOS-Base.repo
-rwxr-xr-x 1 root root  303 Jul  5 19:02 yum.reposync.sh
[root@yum-server yum.repos.d]# sh yum.reposync.sh
# 等待同步完成
# 同步完成,查看文件大小,合计27G
[root@yum-server CentOS-YUM]# du -ch Aliyun/
9.0G    Aliyun/version_7/64bit/base/Packages
9.0G    Aliyun/version_7/64bit/base
616M    Aliyun/version_7/64bit/extras/Packages
616M    Aliyun/version_7/64bit/extras
3.6G    Aliyun/version_7/64bit/updates/Packages
3.6G    Aliyun/version_7/64bit/updates
14G     Aliyun/version_7/64bit
14G     Aliyun/version_7
9.0G    Aliyun/version_6/64bit/base/Packages
9.0G    Aliyun/version_6/64bit/base
616M    Aliyun/version_6/64bit/extras/Packages
616M    Aliyun/version_6/64bit/extras
3.6G    Aliyun/version_6/64bit/updates/Packages
3.6G    Aliyun/version_6/64bit/updates
14G     Aliyun/version_6/64bit
14G     Aliyun/version_6
27G     Aliyun/
27G     total

建立yum源仓库

'因为建仓最终的目的也是可供client来进行检索的,所以得每个Packages目录都要建成仓库,所以建仓的时候,目录指到最底层的Packages,而-np更新的时候只用指定到64bit的目录就可以了,否则会重复建立base、extras、updates三个目录进行下载
[root@yum-server ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_7/64bit/base/Packages/
Spawning worker 0 with 10070 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@yum-server ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_7/64bit/extras/Packages/
Spawning worker 0 with 397 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@yum-server ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_7/64bit/updates/Packages/
Spawning worker 0 with 884 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

[root@yum-server ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_6/64bit/base/Packages/
Spawning worker 0 with 10070 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@yum-server ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_6/64bit/updates/Packages/
Spawning worker 0 with 884 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@yum-server ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_6/64bit/extras/Packages/
Spawning worker 0 with 397 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete[root@yum-server ~]# tree -d /usr/share/nginx/html/CentOS-YUM/Aliyun/          # 建仓完成后,会自动生成一个repodata目录
/usr/share/nginx/html/CentOS-YUM/Aliyun/
├── version_6
│   └── 64bit
│       ├── base
│       │   └── Packages
│       │       └── repodata
│       ├── extras
│       │   └── Packages
│       │       └── repodata
│       └── updates
│           └── Packages
│               └── repodata
└── version_7
    └── 64bit
        ├── base
        │   └── Packages
        │       └── repodata
        ├── extras
        │   └── Packages
        │       └── repodata
        └── updates
            └── Packages
                └── repodata

22 directories
'可以写一个更新yum源的脚本,然后写一个计划任务,定期更新yum源(reposync -np 就是更新新的rpm包)
#!/usr/bin/bash
reposync -np /usr/share/nginx/html/CentOS-YUM/Aliyun/version_7/64bit/
echo "centos7 is sync complate"
/usr/bin/sed -i "s/7/6/g" `grep 7 -rl /etc/yum.repos.d/CentOS-Base.repo`

reposync -np /usr/share/nginx/html/CentOS-YUM/Aliyun/version_6/64bit/
echo "centos6 is sync complate"
/usr/bin/sed -i "s/6/7/g" `grep 6 -rl /etc/yum.repos.d/CentOS-Base.repo`

配置客户端yum

# 备份原来的yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo  epel-testing.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo  epel.repo
[root@localhost yum.repos.d]# mkdir back
[root@localhost yum.repos.d]# mv CentOS-* back/
[root@localhost yum.repos.d]# mv epel* back/
[root@localhost yum.repos.d]# ls
back
[root@localhost yum.repos.d]#
[root@localhost yum.repos.d]# vim CentOS-Base.repo   # 需要6,就使用6,需要7,就使用7,也可以使用yum-plugin-priorities工具来控制优先级,加上priority=1(2|3|4都可以)来控制优先级
[Aliyun_7_base]
name=source_from_localserver
baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_7/64bit/base/Packages
gpgcheck=0
enable=1

[Aliyun_7_extras]
name=source_from_localserver
baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_7/64bit/extras/Packages
gpgcheck=0
enable=1

[Aliyun_7_updates]
name=source_from_localserver
baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_7/64bit/updates/Packages
gpgcheck=0
enable=1

# [Aliyun_6_base]
# name=source_from_localserver
# baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_6/4bit/base/Packages
# gpgcheck=0
# enable=1
#
# [Aliyun_6_extras]
# name=source_from_localserver
# baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_6/74bit/extras/Packages
# gpgcheck=0
# enable=1
#
# [Aliyun_6_updates]
# name=source_from_localserver
# baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_6/74bit/updates/Packages
# gpgcheck=0
# enable=1
[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache
# 安装软件来测试一下
[root@localhost yum.repos.d]# yum -y install net-tools
Loaded plugins: fastestmirror, priorities
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package            Arch            Version                             Repository              Size
=====================================================================================================
Installing:
 net-tools          x86_64          2.0-0.25.20131004git.el7            Aliyun_7_base          306 k

Transaction Summary
=====================================================================================================
Install  1 Package

Total download size: 306 k
Installed size: 917 k
Downloading packages:
net-tools-2.0-0.25.20131004git.el7.x86_64.rpm                                 | 306 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                                         1/1
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                                         1/1

Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7

Complete!

安装完成,Repository里面显示,是从Aliyun_7_base内获取的,到此,yum源仓库(阿里源)部署完成

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335