转载请注明出处: CHATWEB
本文的链接地址: https://chatweb.com.cn/post-27.html
今天给伙伴们分享一下CentOS7下的YUM源服务器最全搭建步骤,希望看了有所收获。
「云原生运维实战派」作者,对云原生运维感兴趣,也保持时刻学习,后续会分享工作中用到的运维技术,在运维的路上得到支持和共同进步!
如果伙伴们看了文档觉得有用,欢迎大家关注我的公众号,获取相关文档。爱运维,爱生活。
一、环境要求
'环境准备,修改hostname,关闭防火墙,disabled selinux'
[root@edenluo ~]# hostnamectl set-hostname --static yum-server
[root@edenluo ~]# systemctl disable firewalld --now
[root@edenluo ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux
二、配置服务器端yum1、安装yum源工具
[root@edenluo ~]# yum -y install epel-release.noarch # nginx需要epel源
[root@edenluo ~]# yum -y install nginx # 安装nginx
[root@edenluo ~]# yum -y yum-utils # 安装repository管理工具
2、配置nginx
[root@edenluo nginx]# cd /etc/nginx/
[root@edenluo nginx]# cp nginx.conf{,.bak} # 备份!备份!备份!
[root@edenluo 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@edenluo 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@edenluo 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@edenluo nginx]# systemctl enable nginx.service --now # 启动nginx,设为开机自启
[root@edenluo 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
3、配置nginx页面目录
[root@edenluo nginx]# cd /usr/share/nginx/html/
[root@edenluo html]# mkdir -p CentOS-YUM/Aliyun/{version_8,version_7}/64bit
[root@edenluo html]# tree /usr/share/nginx/html/CentOS-YUM/
/usr/share/nginx/html/CentOS-YUM/
└── Aliyun
├── version_8
│ └── 64bit
└── version_7
└── 64bit
5 directories, 0 files
[root@edenluo html]# cd CentOS-YUM/
[root@edenluo 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_8</a><br/>
These packagers using for Centos 8<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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<head>
<title>Web Server</title>
<style rel="stylesheet" type="text/css">
html {
background-image:url(img/html-background.png);
background-color: white;
font-family: "DejaVu Sans", "Liberation Sans", sans-serif;
font-size: 0.85em;
line-height: 1.25em;
margin: 0 4% 0 4%;
}
body {
border: 10px solid #fff;
margin:0;
padding:0;
background: #fff;
}
/* Links */
a:link { border-bottom: 1px dotted #ccc; text-decoration: none; color: #204d92; }
a:hover { border-bottom:1px dotted #ccc; text-decoration: underline; color: green; }
a:active { border-bottom:1px dotted #ccc; text-decoration: underline; color: #204d92; }
a:visited { border-bottom:1px dotted #ccc; text-decoration: none; color: #204d92; }
a:visited:hover { border-bottom:1px dotted #ccc; text-decoration: underline; color: green; }
.logo a:link,
.logo a:hover,
.logo a:visited { border-bottom: none; }
.mainlinks a:link { border-bottom: 1px dotted #ddd; text-decoration: none; color: #eee; }
.mainlinks a:hover { border-bottom:1px dotted #ddd; text-decoration: underline; color: white; }
.mainlinks a:active { border-bottom:1px dotted #ddd; text-decoration: underline; color: white; }
.mainlinks a:visited { border-bottom:1px dotted #ddd; text-decoration: none; color: white; }
.mainlinks a:visited:hover { border-bottom:1px dotted #ddd; text-decoration: underline; color: white; }
/* User interface styles */
#header {
margin:0;
padding: 0.5em;
background: #204D8C url(img/header-background.png);
text-align: left;
}
.logo {
padding: 0;
/* For text only logo */
font-size: 1.4em;
line-height: 1em;
font-weight: bold;
}
.logo img {
vertical-align: middle;
padding-right: 1em;
}
.logo a {
color: #fff;
text-decoration: none;
}
p {
line-height:1.5em;
}
h1 {
margin-bottom: 0;
line-height: 1.9em; }
h2 {
margin-top: 0;
line-height: 1.7em; }
#content {
clear:both;
padding-left: 30px;
padding-right: 30px;
padding-bottom: 30px;
border-bottom: 5px solid #eee;
}
.mainlinks {
float: right;
margin-top: 0.5em;
text-align: right;
}
ul.mainlinks > li {
border-right: 1px dotted #ddd;
padding-right: 10px;
padding-left: 10px;
display: inline;
list-style: none;
}
ul.mainlinks > li.last,
ul.mainlinks > li.first {
border-right: none;
}
</style>
</head>
<body>
<div id="header">
<ul class="mainlinks">
<li> <a href="http://www.centos.org/">Home</a> </li>
<li> <a href="http://wiki.centos.org/">Wiki</a> </li>
<li> <a href="http://wiki.centos.org/GettingHelp/ListInfo">Mailing Lists</a></li>
<li> <a href="http://www.centos.org/download/mirrors/">Mirror List</a></li>
<li> <a href="http://wiki.centos.org/irc">IRC</a></li>
<li> <a href="https://www.centos.org/forums/">Forums</a></li>
<li> <a href="http://bugs.centos.org/">Bugs</a> </li>
<li class="last"> <a href="http://wiki.centos.org/Donate">Donate</a></li>
</ul>
<div class="logo">
<a href="http://www.centos.org/"><img src="img/centos-logo.png" border="0"></a>
</div>
</div>
<div id="content">
<h1>CentOS-YUM 所有的包</h1>
<h2>按照系统版本选择版本下载</h2>
<a href="http://192.168.96.250/share">CentOS8 版本</a><br/>
以上包适用于 Centos 8<br/>
<a href="http://192.168.96.250/share">CentOS7 版本</a><br/>
以上包适用于 Centos 7<br/>
<p style="font-weight:bolder;color:red;font-size:18px;">请替换该文件并填写以下内容:</p>
<p style="font-weight:bolder;color:blue;font-size:15px;">替换文件: /etc/yum.repos.d/CentOS-Base.repo</p>
<p><a href="http://www.centos.org/">CentOS 官网</a> </p>
<p>,</p>
<p> <a href="http://www.centos.org/download/mirrors/">mirror network</a>,<a href="http://wiki.centos.org/SpecialInterestGroup/">SIGs</a>,<a href="http://wiki.centos.org/">wiki</a>, <a href="http://wiki.centos.org/irc">IRC Chat</a>, <a href="http://wiki.centos.org/GettingHelp/ListInfo">Email Lists</a>, <a href="https://www.centos.org/forums/">Forums</a>, <a href="http://bugs.centos.org/">Bugs Database</a> , <a href="http://wiki.centos.org/FAQ/">FAQ</a>.</p>
</div>
</div>
</body>
</html1>
4、替换yum源文件
# 备份原来的官方yum源
[root@edenluo CentOS-YUM]# cd /etc/yum.repos.d/
[root@edenluo yum.repos.d]# mv ./* /tmp/
[root@edenluo yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo # 下载aliyun的centos7的yum源
[root@edenluo 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/8/g" `grep 7 -rl /etc/yum.repos.d/CentOS-Base.repo`
reposync -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_8/64bit/
/usr/bin/sed -i "s/8/7/g" `grep 6 -rl /etc/yum.repos.d/CentOS-Base.repo` #还原
[root@edenluo yum.repos.d]# chmod +x yum.reposync.sh # 要给执行权限
[root@edenluo 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@edenluo yum.repos.d]# sh yum.reposync.sh
# 等待同步完成
# 同步完成,查看文件大小,合计27G
[root@edenluo 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_8/64bit/base/Packages
9.0G Aliyun/version_8/64bit/base
616M Aliyun/version_8/64bit/extras/Packages
616M Aliyun/version_8/64bit/extras
3.6G Aliyun/version_8/64bit/updates/Packages
3.6G Aliyun/version_8/64bit/updates
14G Aliyun/version_8/64bit
14G Aliyun/version_8
27G Aliyun/
27G total
5、建立yum源仓库
'因为建仓最终的目的也是可供client来进行检索的,所以得每个Packages目录都要建成仓库,所以建仓的时候,目录指到最底层的Packages,而-np更新的时候只用指定到64bit的目录就可以了,否则会重复建立base、extras、updates三个目录进行下载
[root@edenluo ~]# 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@edenluo ~]# 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@edenluo ~]# 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@edenluo ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_8/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@edenluo ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_8/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@edenluo ~]# createrepo -p /usr/share/nginx/html/CentOS-YUM/Aliyun/version_8/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@edenluo ~]# tree -d /usr/share/nginx/html/CentOS-YUM/Aliyun/ # 建仓完成后,会自动生成一个repodata目录
/usr/share/nginx/html/CentOS-YUM/Aliyun/
├── version_8
│ └── 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/8/g" `grep 7 -rl /etc/yum.repos.d/CentOS-Base.repo`
reposync -np /usr/share/nginx/html/CentOS-YUM/Aliyun/version_8/64bit/
echo "centos6 is sync complate"
/usr/bin/sed -i "s/8/7/g" `grep 6 -rl /etc/yum.repos.d/CentOS-Base.repo`
三、配置客户端yum
# 备份原来的yum源
[root@edenluo ~]# cd /etc/yum.repos.d/
[root@edenluo 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@edenluo yum.repos.d]# mkdir back
[root@edenluo yum.repos.d]# mv CentOS-* back/
[root@edenluo yum.repos.d]# mv epel* back/
[root@edenluo yum.repos.d]# ls
back
[root@edenluo yum.repos.d]# vim CentOS-Base.repo # 需要8,就使用8,需要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_8_base]
# name=source_from_localserver
# baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_8/4bit/base/Packages
# gpgcheck=0
# enable=1
#
# [Aliyun_8_extras]
# name=source_from_localserver
# baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_8/74bit/extras/Packages
# gpgcheck=0
# enable=1
#
# [Aliyun_8_updates]
# name=source_from_localserver
# baseurl=http://192.168.57.133/CentOS-YUM/Aliyun/version_8/74bit/updates/Packages
# gpgcheck=0
# enable=1
[root@edenluo yum.repos.d]# yum clean all
[root@edenluo yum.repos.d]# yum makecache
# 安装软件来测试一下
[root@edenluo 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!
推荐阅读:
转载请注明出处: CHATWEB
本文的链接地址: https://chatweb.com.cn/post-27.html