Linux 搭建私有 yum 仓库

Posted superkx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux 搭建私有 yum 仓库相关的知识,希望对你有一定的参考价值。

以不息为体,以日新为道。

本文主要介绍如何在 CentOS8.5 编译 http2.4,并在其搭建私有 yum 仓库,通过 http 服务对外提供 yum 仓库服务,以便让其他机器通过该仓库下载软件。

准备环境

准备4台虚拟机,分别如下:

  • 一台 YUM 仓库服务器:CentOS8.5(IP:192.168.68.100)
  • 部署 HTTP 服务
  • 三台客户端:(用于验证 YUM 仓库是否部署成功)
  • CentOS6.9
  • CentOS7.9
  • CentOS8.5

基本流程

  1. 部署 HTTP 服务:
  • 在 yum 仓库服务器部署 HTTP 服务,用于提供 HTTP 服务,以便客户端可以通过 HTTP 协议访问该 yum 仓库。
  1. 部署 yum 仓库:
  • 在 yum 仓库服务器准备各个 CentOS 发行版的软件包,本文通过挂载各发行版的镜像到服务器进行实现(在实际环境中建议将各版本软件包拷贝到仓库服务器)。
  1. 客户端配置 yum 源:
  • 客户端配置 yum 源,指向私有仓库地址;然后进行验证。


操作步骤

部署 HTTP 服务

本文通过编译 apache httpd2.4 版本为例进行演示。 httpd 编译安装需要准备3个源码包(httpd、apr、apr-util),本文以以下几个版本为例进行编译:

  1. 登录到 yum 仓库服务器,下载编译环境所需要的依赖包:
yum -y install gcc make pcre-devel openssl-devel expat-devel wget
  1. 下载 httpd、apr、apr-util 源码包:
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz -P /usr/local/src
wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz -P /usr/local/src
wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.52.tar.gz -P /usr/local/src
  1. 解压各源码包:
[root@centos8.5-68.100 ~]#cd /usr/local/src
[root@centos8.5-68.100 src]#tar xvf apr-1.7.0.tar.gz
[root@centos8.5-68.100 src]#tar xvf apr-util-1.6.1.tar.gz
[root@centos8.5-68.100 src]#tar xvf httpd-2.4.52.tar.gz
  1. 将 apr 和 apr-util 源码与 httpd 源码合并:
[root@centos8.5-68.100 src]#mv apr-1.7.0 httpd-2.4.52/srclib/apr
[root@centos8.5-68.100 src]#mv apr-util-1.6.1 httpd-2.4.52/srclib/apr-util
[root@centos8.5-68.100 src]#cd httpd-2.4.51/srclib/
[root@centos8.5-68.100 srclib]#ll
total 12
drwxr-xr-x 27 1001 1001 4096 Apr 2 2019 apr
drwxr-xr-x 20 1001 1001 4096 Oct 18 2017 apr-util
-rw-r--r-- 1 504 games 121 Feb 11 2005 Makefile.in
  1. 执行编译:
[root@centos8.5-68.100 ~]#cd /usr/local/src/httpd-2.4.52/
[root@centos8.5-68.100 httpd-2.4.52]#./configure \\
--prefix=/apps/httpd24 \\
--enable-so \\
--enable-ssl \\
--enable-cgi \\
--enable-rewrite \\
--with-zlib \\
--with-pcre \\
--with-included-apr \\
--enable-modules=most \\
--enable-mpms-shared=all \\
--with-mpm=event
[root@centos8.5-68.100 httpd-2.4.52]#make -j 2 && make install
  1. 创建 httpd 专用用户:
[root@centos8.5-68.100 httpd-2.4.52]#groupadd -r -g 80 apache
[root@centos8.5-68.100 httpd-2.4.52]#useradd -r -u 80 -g apache -s /sbin/nologin apache
  1. 配置环境变量:
[root@centos8.5-68.100 httpd-2.4.52]#echo PATH=/apps/httpd24/bin:$PATH > /etc/profile.d/httpd.sh
[root@centos8.5-68.100 httpd-2.4.52]#. /etc/profile.d/httpd.sh
[root@centos8.5-68.100 httpd-2.4.52]#httpd -v
Server version: Apache/2.4.51 (Unix)
Server built: Jan 10 2022 23:34:50
  1. 创建 httpd service 文件:
#[root@centos8.5-68.100 httpd-2.4.52]#vim /lib/systemd/system/httpd24.service
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=forking
#EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/apps/httpd24/bin/apachectl start
#ExecStart=/apps/httpd24/bin/httpd $OPTIONS -k start
ExecReload=/apps/httpd24/bin/apachectl graceful
#ExecReload=/apps/httpd24/bin/httpd $OPTIONS -k graceful
ExecStop=/apps/httpd24/bin/apachectl stop
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. 设置开机启动 httpd:
[root@centos8.5-68.100 httpd-2.4.52]#systemctl daemon-reload
[root@centos8.5-68.100 httpd-2.4.52]#systemctl enable --now httpd24
[root@centos8.5-68.100 httpd-2.4.52]#systemctl status httpd24.service
● httpd24.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd24.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2022-01-16 17:52:38 CST; 2s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 37887 ExecStart=/apps/httpd24/bin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 37900 (httpd)
Tasks: 1 (limit: 11229)
Memory: 1.3M
CGroup: /system.slice/httpd24.service
└─37900 /apps/httpd24/bin/httpd -k start

Jan 16 17:52:12 centos8.5-68.100 systemd[1]: Starting The Apache HTTP Server...
Jan 16 17:52:38 centos8.5-68.100 systemd[1]: Started The Apache HTTP Server.
  1. 在浏览器输入地址进行访问,验证是否可以正常访问:

Linux

如上图,说明 httpd 部署成功。


部署 yum 仓库

本文通过挂载 CentOS6、7、8镜像的方式,来为仓库提供软件包。

  1. 在虚拟机分别挂载 CentOS6、7、8的镜像,如下图:

Linux

  1. 执行以下创建各版本名称的文件夹,以及进行挂载(挂载的时候要将对应的镜像挂载到对应的目录下):
#1.创建挂载目录
[root@centos8.5-68.100 conf]#mkdir /apps/httpd24/htdocs/CentOS6,7,8

#2.查看虚拟光驱
[root@centos8.5-68.100 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 10.1G 0 rom
sr1 11:1 1 3.7G 0 rom
sr2 11:2 1 4.4G 0 rom

#3.挂载镜像到对应的目录(注意挂载的时候要将对应的镜像挂载到对应的目录下):
[root@centos8.5-68.100 ~]#mount /dev/sr0 /apps/httpd24/htdocs/CentOS8
mount: /apps/httpd24/htdocs/CentOS8: WARNING: device write-protected, mounted read-only.

[root@centos8.5-68.100 ~]#mount /dev/sr1 /apps/httpd24/htdocs/CentOS6
mount: /apps/httpd24/htdocs/CentOS6: WARNING: device write-protected, mounted read-only.

[root@centos8.5-68.100 ~]#mount /dev/sr2 /apps/httpd24/htdocs/CentOS7
mount: /apps/httpd24/htdocs/CentOS7: WARNING: device write-protected, mounted read-only.

#4.查看是否挂载成功:
[root@centos8.5-68.100 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 878M 0 878M 0% /dev
tmpfs 896M 0 896M 0% /dev/shm
tmpfs 896M 8.7M 887M 1% /run
tmpfs 896M 0 896M 0% /sys/fs/cgroup
/dev/nvme0n1p2 60G 2.4G 58G 4% /
/dev/nvme0n1p5 75G 568M 75G 1% /data
/dev/nvme0n1p1 976M 169M 741M 19% /boot
tmpfs 179M 0 179M 0% /run/user/0
/dev/sr0 11G 11G 0 100% /apps/httpd24/htdocs/CentOS8
/dev/sr1 3.7G 3.7G 0 100% /apps/httpd24/htdocs/CentOS6
/dev/sr2 4.4G 4.4G 0 100% /apps/httpd24/htdocs/CentOS7

#5.上述挂载只是临时生效,如需永久生效,需要将挂载项配置到 /etc/fstab 文件中,如下:
[root@centos8.5-68.100 ~]#vim /etc/fstab
......
/dev/sr0 /apps/httpd24/htdocs/CentOS8 iso9660 ro 0 0
/dev/sr1 /apps/httpd24/htdocs/CentOS6 iso9660 ro 0 0
/dev/sr2 /apps/httpd24/htdocs/CentOS7 iso9660 ro 0 0
  1. 可以在浏览器输入 yum 仓库服务器地址,看是否成功列出各版本软件包。
  2. 编辑 yum 文件,修改 yum 路径为本地 yum 源,让 yum 仓库服务器自身走本地 yum 源下载软件包:
#1.备份之前的 yum 文件
[root@centos8.5-68.100 ~]#cd /etc/yum.repos.d/
[root@centos8.5-68.100 yum.repos.d]#mkdir bak
[root@centos8.5-68.100 yum.repos.d]#mv * ./bak

#2.创建 yum 文件,注意需要以 repo 后缀名结尾,内容如下:
[root@centos8.5-68.100 yum.repos.d]#vim all.repo
[AppStream]
name=AppStream-centos8
baseurl=file:///apps/httpd24/htdocs/CentOS8/AppStream
enabled=1
gpgcheck=0

[Base]
name=Base-centos8
baseurl=file:///apps/httpd24/htdocs/CentOS8/BaseOS
enabled=1
gpgcheck=0
  1. 至此 yum 仓库自身的本地 yum 源已配置完成。可以执行以下命令进行验证,如果 repo 的名称是显示的是上面文件定义的并且可以正常下载软件,说明成功:
[root@centos8.5-68.100 yum.repos.d]#yum repolist
repo id repo name
AppStream AppStream-centos8
Base Base-centos8

#或者执行 yum list 查看是否可以列出软件包
[root@centos8.5-68.100 yum.repos.d]#yum list

至此,本地私有 yum 仓库部署完成,接下来可以在各客户端配置 yum 进行测试。


客户端配置 yum 源

  1. 分别登录到 CentOS6、7、8系统,然后修改 yum 文件(修改过程跟上述部署 yum 仓库章节中的第4步类似,这里就不再赘述),最终修改内容如下:

CentOS6:

[root@centos6 yum.repos.d]#cat centos6.repo 
[Base]
name=Base-centos6
baseurl=http://192.168.68.100/CentOS6/
enabled=1
gpgcheck=0

CentOS7:

[root@centos7 yum.repos.d]#cat centos7.repo 
[Base]
name=Base-centos7
baseurl=http://192.168.68.100/CentOS7/
enabled=1
gpgcheck=0

CentOS8:

[root@centos8 yum.repos.d]#cat centos8.repo 
[AppStream]
name=AppStream-centos8
baseurl=http://192.168.68.100/CentOS8/AppStream/
enabled=1
gpgcheck=0

[Base]
name=Base-centos8
baseurl=http://192.168.68.100/CentOS8/BaseOS/
enabled=1
gpgcheck=0
  1. 尝试下载软件进行验证(以客户端 CentOS7 为例):
#1.查看是否可以列出仓库详细
[root@centos7 yum.repos.d]#yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id repo name status
Base Base-centos7 4,070
repolist: 4,070

#2.尝试下载软件包
[root@centos7 yum.repos.d]#yum install bind -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Base | 3.6 kB 00:00:00
Resolving Dependencies
--> Running transaction check
........
Dependency Installed:
GeoIP.x86_64 yum管理——搭建iso镜像私有yum源仓库

实验:安装centos-6,开启网卡,搭建私有yum仓库

Linux创建私有yum源仓库

Centos7同步阿里yum仓库 搭建yum私有仓库

###Centos7同步阿里yum仓库 搭建yum私有仓库

Docker的私有镜像仓库搭建