Centos6.9定制rpm包、搭建yum仓库

目录

1 保留yum安装软件时下载下来的rpm包

只需修改yum配置文件即可。下载下来的rpm包会自动保存在/var/cache/yum/目录下

[root@web01 ~]# sed -i 's#keepcache=0#keepcache=1#g' /etc/yum.conf
[root@web01 ~]# yum install sl -y
[root@web01 ~]# tree /var/cache/yum/x86_64/6/epel/
/var/cache/yum/x86_64/6/epel/
├── 83f14a3d88054141092be5da12eb18cb1baabd7d52e1ae38bfc98d3d5005490c-primary.sqlite
├── cachecookie
├── packages
│   └── sl-5.02-1.el6.x86_64.rpm
└── repomd.xml

1 directory, 4 files
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2 编译安装Nginx

# Download tar.gz package
cd /home/oldboy/tools
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz

# Yum install Dependent package
yum install openssl openssl-devel -y
yum install pcre pcre-devel -y
rpm -qa pcre pcre-devel

# Useradd www
useradd www -s /sbin/nologin -M

# Compile and install
tar -xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.6.3/
make && make install

# Create a soft link
ln -s /application/nginx-1.6.3/ /application/nginx

# Start nginx
/application/nginx/sbin/nginx

# Check port 80
lsof -i :80
  • 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

3 安装fpm工具

1 首先安装依赖包

yum -y install zlib-devel curl-devel openssl-devel httpd-devel apr-devel apr-util-devel mysql-devel
  • 1
  • 2

2 下载ruby,fpm是ruby写的,因此系统环境需要ruby。不推荐yum安装ruby,在测试的时候会出现一些问题。

[root@web01 tools]# wget https://ruby.taobao.org/mirrors/ruby/ruby-2.3.0.tar.gz
[root@web01 tools]# tar -xf ruby-2.3.0.tar.gz
[root@web01 tools]# cd ruby-2.3.0
[root@web01 ruby-2.3.0]# ./configure
[root@web01 ruby-2.3.0]# make && make install
[root@web01 ruby-2.3.0]# ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
[root@web01 ~]# whereis gem
gem: /usr/local/bin/gem
[root@web01 ~]# /usr/local/bin/gem install fpm
[root@web01 ~]# fpm -v
1.9.3

# 添加阿里云的Rubygems仓库,默认的源为外国源,访问下载速度比较慢,在添加之前先移除原生的Ruby仓库
[root@web01 ~]# gem sources
*** CURRENT SOURCES ***

https://rubygems.org/ 
[root@web01 ~]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources

# 添加阿里仓库
[root@web01 ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
[root@web01 ~]# gem sources
*** CURRENT SOURCES ***

http://mirrors.aliyun.com/rubygems/
# 添加fpm[root@web01 ~]# gem install fpm -v 1.3.3
指定安装fpm1.3.3版本的软件,fpm这个工具升级挺频繁,而且在每次升级后会出现各种各样的问题。但是老版本的fpm工具能够满足我们的要求,因此就用这个版本了。
  • 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

3 fpm常用参数

-s:指定源类型
-t:指定目标类型,即想要制作为什么包
-n:指定包的名字
-v:指定包的版本号
-C:指定打包的相对路径
-d:指定依赖于哪些包
-f:第二次打包时目录下如果有同名安装包存在,则覆盖它
-p:输出的安装包的目录,不想放在当前目录下就需要指定
--post-install:软件包安装完成之后所要运行的脚本;同--offer-install
--pre-install:软件包安装完成之前所要运行的脚本;同--before-install
--post-uninstall:软件包卸载完成之后所要运行的脚本;同--offer-remove
--pre-uninstall:软件包卸载完成之前所要运行的脚本;同—before-remove
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4 fpm支持的源类型包

dir:将目录打包成所需要的类型,可以用于源码编译安装的软件包 
rpm:对rpm进行转换 
gem:对rubygem包进行转换 
python:将Python模块打包成相应的类型

5 fpm支持的目标类型包

rpm:转换为rpm包 
deb:转换为deb包 
solaris:转换为solaris包 
puppet:转换为puppet包

4 将Nginx打包

# 注意--post-install不能放在最后,选项顺序最好不要改变
fpm -s dir -t rpm -n nginx -v 1.6.3 -d 'pcre-devel,openssl-devel' --post-install /server/scripts/nginx_rpm.sh -f /application/nginx-1.6.3/

# nginx_rpm.sh
cat >>/server/scripts/nginx_rpm.sh<<EOF
useradd -s /sbin/nologin -M www
ln -s /application/nginx-1.6.3/ /application/nginx
ln -s /application/nginx/sbin/nginx  /usr/local/sbin/
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5 其它机器测试

# scp把rpm包传送过去
[root@web01 ~]# scp nginx-1.6.3-1.x86_64.rpm [email protected]:/home/oldboy/tools

# 测试
[root@lb01 tools]# yum localinstall nginx-1.6.3-1.x86_64.rpm -y
  • 1
  • 2
  • 3
  • 4
  • 5

6 yum仓库搭建

1 安装createrepo

[root@web02 ~]# yum install createrepo -y
  • 1

2 创建yum仓库目录并初始化repodata索引文件

[root@web02 ~]# mkdir -p /application/yum/centos6.9/x86_64/
[root@web02 ~]# cd /application/yum/centos6.9/x86_64/    # 拷贝定制的rpm包到此目录下
[root@web02 x86_64]# createrepo -pdo /application/yum/centos6.9/x86_64/ /application/yum/centos6.9/x86_64/
  • 1
  • 2
  • 3

3 提供yum服务

# 这里用python来提供web服务,也可用apache或nginx来提供web服务
[root@web02 x86_64]# python -m SimpleHTTPServer 80 &>/dev/null &   # python的httpd模块
[root@web02 x86_64]# lsof -i :80
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
python  24680 root    3u  IPv4  32493      0t0  TCP *:http (LISTEN)
  • 1
  • 2
  • 3
  • 4
  • 5

4 如果有新的rpm包放入必须更新

[root@web02 x86_64]# createrepo --update /application/yum/centos6.9/x86_64/
  • 1

5 客户端修改yum源

cat >>/etc/yum.repo.d/<<EOF
[only]
name=Server
baseurl=http://10.0.0.7        
# baseurl指定yum仓库IP,如果不是80端口需要加端口
enabled=1
gpgcheck=0
EOF

# 客户端以安装nginx为例
# 清除本地缓存
[root@lb02 ~]# yum clean all

# 显示本地yum源列表
[root@lb02 ~]# yum --enablerepo=only --disablerepo=base,extras,updates,epel list   
# 这里需要注意--enablerepo为local.repo文件中定义的only

# 安装nginx为例#
yum --enablerepo=local --disablerepo=base,extras,updates,epel install nginx -y
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

6 yum的一些用法

# 只下载包不安装包
1、yumdownloader pcre-devel openssl-devel
2、yum --downloadonly pcre-devel openssl-devel

# yum本地安装rpm包
yum local install pcre-devel openssl-devel

# yum开启某些个源,关闭某些源安装软件包
yum --enablerepo=local --disablerepo=base,extras,updates,epel install pcre-devel openssl-devel

# yum安装不做GPG-check
yum install --nogpgcheck pcre-devel openssl-devel

# 查看yum历史记录
yum history

# 列出启用的软件库
yum repolist
yum repolist all # 列出所有的软件库,包括禁用的也列出
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

7 错误排查

# 若在客户端安装nginx的时候报以下错误是因为缺少依赖包的缘故,把其下载下来并更新yum仓库即可。
yum仓库:createrepo --update /application/yum/centos6.9/x86_64/
客户端:yum clean all

Error: Package: nginx-1.6.3-1.x86_64 (only)
           Requires: pcre-devel
Error: Package: nginx-1.6.3-1.x86_64 (only)
           Requires: openssl-devel 

# 需要注意配置文件书写是否有误
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

7 镜像同步公网yum源

上面只是将自己制作的rpm包,放入yum源。但还有一种需求,平时安装软件都是从公网下载的,占用带宽,因此可以直接使用公网yum源的repodata。 
上游yum源必须要支持rsync协议,否则不能使用rsync进行同步。 
http://mirrors.ustc.edu.cn/status/

# CentOS官方标准源:rsync://mirrors.ustc.edu.cn/centos/
# epel源:rsync://mirrors.ustc.edu.cn/epel/
# 同步命令:
# 使用rsync同步yum源,为了节省带宽、磁盘和下载时间,这里只同步了CentOS6的rpm包,这样所有的rpm包只占用了21G,全部同步需要300G左右。

# 同步base源,小技巧,我们安装系统的光盘镜像含有部分rpm包,大概3G,这些就不用重新下载。
/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/os/x86_64/ /data/yum_data/centos/6/os/x86_64/

/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/extras/x86_64/ /data/yum_data/centos/6/extras/x86_64/

/usr/bin/rsync -av rsync://mirrors.ustc.edu.cn/centos/6/updates/x86_64/ /data/yum_data/centos/6/updates/x86_64/

# epel源
/usr/bin/rsync -av --exclude=debug rsync://mirrors.ustc.edu.cn/epel/6/x86_64/ /data/yum_data/epel/6/x86_64/

# 使用内网yum源方法
# 可以自建一个内网dns,如果没有,可使用hosts解析。
echo '192.168.0.200 mirrors.aliyun.com' >>/etc/hosts

# 结果展示
[root@m01 data]# du -sh yum_data    
21G     yum_data
[root@m01 data]# tree -L 3 yum_data/
yum_data/
├── centos
│   ├── 6
│   │   ├── extras
│   │   ├── os
│   │   └── updates
│   └── RPM-GPG-KEY-CentOS-6
├── epel
│   └── 6
│       └── x86_64

转载至https://blog.csdn.net/mr_rsq/article/details/79846461


猜你喜欢

转载自blog.csdn.net/vic_qxz/article/details/80606109