定制RPM包及yum仓库搭建(以安装nginx为例)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定制RPM包及yum仓库搭建(以安装nginx为例)相关的知识,希望对你有一定的参考价值。
一、前提条件
#下面通过虚拟机准备了一台服务端,一台客户端,基于外网IP测试,以安装nginx为例。
#两台机器公共配置
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[[email protected] ~]# uname -m
x86_64
[[email protected] ~]# uname -r
2.6.32-573.el6.x86_64
#服务端配置
[[email protected] ~]# ifconfig eth0|awk -F "[ :]+"‘NR==2{print $4}‘
10.0.0.61
[[email protected] ~]# hostname
m01
#客户端配置
[[email protected] ~]# ifconfig eth0|awk -F ‘[ :]+‘ ‘NR==2{print$4}‘
10.0.0.8
[[email protected] ~]# hostname
web01
#RPM介绍
http://www.ibm.com/developerworks/cn/linux/l-rpm/
二、定制化RPM包
下面是服务端的配置,只要没有标明都是服务端操作
2、1FPM安装
#fpm是ruby写的,因此系统环境需要ruby,且ruby版本号大于1.8.5
[[email protected] ~]# rpm -qa ruby rubygems ruby-devel
#安装ruby模块
[[email protected] ~]# yum -y install ruby rubygems ruby-devel
[[email protected] ~]# rpm -qa ruby rubygems ruby-devel
rubygems-1.3.7-5.el6.noarch
ruby-1.8.7.374-4.el6_6.x86_64
ruby-devel-1.8.7.374-4.el6_6.x86_64
#添加阿里云的rubygems仓库,外国的源太慢
[[email protected] ~]# gem sources -ahttp://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
#移除原生的Ruby仓库
[[email protected] ~]# gem sources --remove http://rubygems.org/
http://rubygems.org/ removed from sources
#安装fpm
#指定安装fpm1.3.3版本的软件,fpm这个工具升级很频繁,而且在每次升级后会出现各种各样的问题。但是老版本的fpm工具能够满足我们的需求,因此就用这个版本了。
#这里一定要指定fpm的版本,不然会报错,报错原因可追溯到版本频繁升级的问题导致。
[[email protected] ~]# gem install fpm -v 1.3.3
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Successfully installed json-1.8.3
Successfully installed cabin-0.8.1
Successfully installed backports-3.6.8
Successfully installed arr-pm-0.0.10
Successfully installed clamp-0.6.5
Successfully installed ffi-1.9.10
Successfully installed childprocess-0.5.9
Successfully installed fpm-1.3.3
8 gems installed
Installing ri documentation for json-1.8.3...
Installing ri documentation for cabin-0.8.1...
Installing ri documentation for backports-3.6.8...
Installing ri documentation for arr-pm-0.0.10...
Installing ri documentation for clamp-0.6.5...
Installing ri documentation for ffi-1.9.10...
Installing ri documentation for childprocess-0.5.9...
Installing ri documentation for fpm-1.3.3...
Installing RDoc documentation for json-1.8.3...
Installing RDoc documentation for cabin-0.8.1...
Installing RDoc documentation for backports-3.6.8...
Installing RDoc documentation for arr-pm-0.0.10...
Installing RDoc documentation for clamp-0.6.5...
Installing RDoc documentation for ffi-1.9.10...
Installing RDoc documentation for childprocess-0.5.9...
Installing RDoc documentation for fpm-1.3.3...
# rdoc和ri为ruby程序生成文档
[[email protected] ~]# fpm --help|wc -l
175
#最好做下快照,方便还原和测试
2、2定制nginx的RPM包
2、2、1准备操作
#准备好nginx安装包,统一放在/application/tools/目录下。
[[email protected] /]# mkdir -p /application/tools/
[[email protected] /]# cd /application/tools/
[[email protected] tools]# pwd
/application/tools
#上传或下载nginx安装包
#上传使用rz -y命令即可。
#下面使用下载方式获取nginx安装包
[[email protected] tools]# wget -qhttp://nginx.org/download/nginx-1.6.3.tar.gz
[[email protected] tools]# ls -l nginx-1.6.3.tar.gz
-rw-r--r-- 1 root root 805253 Apr 8 2015nginx-1.6.3.tar.gz
#开启yum缓存, /etc/yum.conf的解释在4、2
[[email protected] tools]# cp /etc/yum.conf/etc/yum.conf.ori.20160630
[[email protected] tools]# sed ‘s#keepcache=0#keepcache=1#g‘/etc/yum.conf
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=1
#主要的是上面两条
[[email protected] tools]# sed -i ‘s#keepcache=0#keepcache=1#g‘ /etc/yum.conf
[[email protected] tools]# grep "keepcache=1"/etc/yum.conf
keepcache=1
#清空本机已有yum缓存
[[email protected] tools]# find /var/cache/ -type f -name"*rpm"
[[email protected] tools]# find /var/cache/ -type f -name"*rpm"|xargs rm -f
#最好做下快照,方便还原和测试
-----------------------------------------------------------
#设置yum源为阿里的配置
[[email protected] tools]# ls -l /etc/yum.repos.d/CentOS-Base.repo
-rw-r--r--. 1 root root 1991 Aug 4 2015/etc/yum.repos.d/CentOS-Base.repo
[[email protected] tools]# cp /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.ori.20160630
[[email protected] tools]# ls -l/etc/yum.repos.d/CentOS-Base.repo.ori.20160630
-rw-r--r-- 1 root root 1991 Jun 29 22:23/etc/yum.repos.d/CentOS-Base.repo.ori.20160630
[[email protected] tools]# wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo
[[email protected] tools]# ls -l /etc/yum.repos.d/CentOS-Base.repo
-rw-r--r--. 1 root root 2572 May 15 2015 /etc/yum.repos.d/CentOS-Base.repo
#可以通过baseurl过滤一下查看是否配置成功
[[email protected] tools]# grep "baseurl"/etc/yum.repos.d/CentOS-Base.repo
# remarked out baseurl= line instead.
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
-----------------------------------------------------------
2、2、2安装nginx
[[email protected] tools]# rpm -qa pcre-devel openssl-devel
[[email protected] tools]# yum -y install pcre-devel openssl-devel
[[email protected] nginx-1.6.3]# rpm -qa pcre-developenssl-devel
openssl-devel-1.0.1e-48.el6_8.1.x86_64
pcre-devel-7.8-7.el6.x86_64
-----------------------------------------------------------
#查看yum缓存文件,/var/cache/下的rpm文件千万不要删除,切记!
[[email protected] tools]# find /var/cache/ -type f -name"*rpm"
/var/cache/yum/x86_64/6/updates/packages/openssl-1.0.1e-48.el6_8.1.x86_64.rpm
/var/cache/yum/x86_64/6/updates/packages/openssl-devel-1.0.1e-48.el6_8.1.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/zlib-devel-1.2.3-29.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libselinux-2.0.94-7.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/pcre-devel-7.8-7.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libsepol-devel-2.0.41-4.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libselinux-utils-2.0.94-7.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libcom_err-devel-1.41.12-22.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/libselinux-devel-2.0.94-7.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/krb5-libs-1.10.3-57.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/krb5-devel-1.10.3-57.el6.x86_64.rpm
/var/cache/yum/x86_64/6/base/packages/keyutils-libs-devel-1.4-5.el6.x86_64.rpm
[[email protected] ~]# ls /tmp/
[[email protected] ~]# find /var/cache/ -type f -name"*rpm"|xargs cp -t /tmp/
[[email protected] ~]# ls /tmp/
keyutils-libs-devel-1.4-5.el6.x86_64.rpm libselinux-2.0.94-7.el6.x86_64.rpm openssl-1.0.1e-48.el6_8.1.x86_64.rpm
krb5-devel-1.10.3-57.el6.x86_64.rpm libselinux-devel-2.0.94-7.el6.x86_64.rpm openssl-devel-1.0.1e-48.el6_8.1.x86_64.rpm
krb5-libs-1.10.3-57.el6.x86_64.rpm libselinux-utils-2.0.94-7.el6.x86_64.rpm pcre-devel-7.8-7.el6.x86_64.rpm
libcom_err-devel-1.41.12-22.el6.x86_64.rpm libsepol-devel-2.0.41-4.el6.x86_64.rpm zlib-devel-1.2.3-29.el6.x86_64.rpm
#下述在安装rpm包的时候会用到
[[email protected] ~]# cd /tmp/ && tar zcfnginx_yum.tar.gz *.rpm
[[email protected] tmp]# ls -l nginx_yum.tar.gz
-rw-r--r-- 1 root root 4442080 Jun 29 22:31nginx_yum.tar.gz
[[email protected] tmp]# sz nginx_yum.tar.gz
#D:\Program\SecureCRT\download\ nginx_yum.tar.gz
-----------------------------------------------------------
[[email protected] nginx-1.6.3]# useradd nginx -M -s /sbin/nologin
[[email protected] nginx-1.6.3]# tail -1 /etc/passwd
nginx:x:502:502::/home/nginx:/sbin/nologin
[[email protected] tools]# pwd
/application/tools
[[email protected] tools]# ls -l nginx-1.6.3.tar.gz
-rw-r--r-- 1 root root 805253 Apr 8 2015nginx-1.6.3.tar.gz
[[email protected] tools]# tar zxf nginx-1.6.3.tar.gz
[[email protected] tools]# cd nginx-1.6.3
[[email protected] nginx-1.6.3]# ll
total 624
drwxr-xr-x 6 1001 1001 4096 Jun 29 22:26 auto
-rw-r--r-- 1 1001 1001 236608 Apr 7 2015CHANGES
-rw-r--r-- 1 1001 1001 360501 Apr 7 2015CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 Jun 29 22:26 conf
-rwxr-xr-x 1 1001 1001 2369 Apr 7 2015 configure
drwxr-xr-x 4 1001 1001 4096 Jun 29 22:26 contrib
drwxr-xr-x 2 1001 1001 4096 Jun 29 22:26 html
-rw-r--r-- 1 1001 1001 1397 Apr 7 2015 LICENSE
drwxr-xr-x 2 1001 1001 4096 Jun 29 22:26 man
-rw-r--r-- 1 1001 1001 49 Apr 7 2015 README
drwxr-xr-x 8 1001 1001 4096 Jun 29 22:26 src
#安装nginx
[[email protected] nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3/ --with-http_stub_status_module
[[email protected] nginx-1.6.3]# make && make install
[[email protected] nginx-1.6.3]# cd ..
[[email protected] tools]# ll
total 792
drwxr-xr-x 9 1001 1001 4096 Jun 29 22:33 nginx-1.6.3
-rw-r--r-- 1 root root 805253 Apr 8 2015nginx-1.6.3.tar.gz
[[email protected] tools]# pwd
/application/tools
[[email protected] tools]# ln -s /application/nginx-1.6.3//application/nginx
-----------------------------------------------------------
#创建rpm包要执行的脚本
[[email protected] tmp]# mkdir -p /server/scripts/
[[email protected] tmp]# cd /server/scripts/
[[email protected] scripts]# cat nginx_rpm.sh
#!/bin/bash
useradd nginx -M -s /sbin/nologin
ln -s /application/nginx-1.6.3/ /application/nginx
#做一次快照
#通过fpm命令打包rpm
[[email protected] tools]# 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/
no value for epoch is set, defaulting to nil{:level=>:warn}
no value for epoch is set, defaulting to nil{:level=>:warn}
Created package {:path=>"nginx-1.6.3-1.x86_64.rpm"}
[[email protected] tools]# ls -l nginx-1.6.3-1.x86_64.rpm
-rw-r--r-- 1 root root 1046824 Jun 29 22:39nginx-1.6.3-1.x86_64.rpm
[[email protected] tools]# sz nginx-1.6.3-1.x86_64.rpm
# D:\Program\SecureCRT\download\nginx-1.6.3-1.x86_64.rpm
-----------------------------------------------------------
#查看rpm包信息
[[email protected] tools]# rpm -qpi nginx-1.6.3-1.x86_64.rpm
Name :nginx Relocations:/
Version :1.6.3 Vendor:[email protected]
Release :1 Build Date: Wed 29 Jun2016 10:39:39 PM CST
Install Date: (not installed) Build Host: m01
Group :default Source RPM:nginx-1.6.3-1.src.rpm
Size :3179846 License:unknown
Signature :(none)
Packager :<[email protected]>
URL :http://example.com/no-uri-given
Summary : nodescription given
Description :
no description given
#查看rpm包内容
[[email protected] tools]# rpm -qpl nginx-1.6.3-1.x86_64.rpm
/application/nginx-1.6.3/conf/fastcgi.conf
/application/nginx-1.6.3/conf/fastcgi.conf.default
/application/nginx-1.6.3/conf/fastcgi_params
/application/nginx-1.6.3/conf/fastcgi_params.default
/application/nginx-1.6.3/conf/koi-utf
/application/nginx-1.6.3/conf/koi-win
/application/nginx-1.6.3/conf/mime.types
/application/nginx-1.6.3/conf/mime.types.default
/application/nginx-1.6.3/conf/nginx.conf
/application/nginx-1.6.3/conf/nginx.conf.default
/application/nginx-1.6.3/conf/scgi_params
/application/nginx-1.6.3/conf/scgi_params.default
/application/nginx-1.6.3/conf/uwsgi_params
/application/nginx-1.6.3/conf/uwsgi_params.default
/application/nginx-1.6.3/conf/win-utf
/application/nginx-1.6.3/html/50x.html
/application/nginx-1.6.3/html/index.html
/application/nginx-1.6.3/logs
/application/nginx-1.6.3/sbin/nginx
#查看rpm的包依赖
[[email protected] tools]# rpm -qpR nginx-1.6.3-1.x86_64.rpm
pcre-devel
openssl-devel
/bin/sh
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(CompressedFileNames) <= 3.0.4-1
#查看rpm自带的执行脚本,执行脚本不是以文件的形式存在rpm包中
[[email protected] tools]# rpm -qp --scriptsnginx-1.6.3-1.x86_64.rpm
postinstall scriptlet (using /bin/sh):
#!/bin/bash
useradd nginx -M -s /sbin/nologin
ln -s /application/nginx-1.6.3/ /application/nginx
三、安装rpm包的三种方法(重点是第三种yum仓库)
3、1第一种,yum命令安装rpm包
[[email protected] tools]# yum localinstallnginx-1.6.3-1.x86_64.rpm
#这个命令会自动先从网络安装rpm包的依赖,然后再安装nginx的rpm包。
3、2第二种,rpm命令安装
[[email protected] tools]# rpm -ivh nginx-1.6.3-1.x86_64.rpm
error: Failed dependencies:
pcre-develis needed by nginx-1.6.2-1.x86_64
openssl-devel is needed by nginx-1.6.2-1.x86_64
但会报如上依赖错误,需要先yum安装依赖才能安装rpm包。
解决方案:
1、以第一种yum命令安装rpm包的方式
2、
[[email protected] ~]# rpm -ivh openssl-devel-1.0.1e-48.el6_8.1.x86_64.rpm
[[email protected] ~]# rpm -ivh pcre-devel-7.8-7.el6.x86_64.rpm
3、如果本机已有所有的依赖包(对应上述中的nginx_yum.tar.gz),那么执行以下操作:
[[email protected] ~]# tar zxf nginx_yum.tar.gz
[[email protected] ~]#rpm -ivh --aid *rpm
#如果报下述错误
Preparing... ########################################### [100%]
file/usr/lib64/libkadm5clnt_mit.so.8.0 from install ofkrb5-libs-1.10.3-42z1.el6_7.x86_64 conflicts with file from packagekrb5-libs-1.10.3-42.el6.x86_64
file/usr/lib64/libkadm5srv_mit.so.8.0 from install of krb5-libs-1.10.3-42z1.el6_7.x86_64conflicts with file from package krb5-libs-1.10.3-42.el6.x86_64
file/usr/bin/openssl from install of openssl-1.0.1e-42.el6_7.4.x86_64 conflictswith file from package openssl-1.0.1e-42.el6.x86_64
file/usr/lib64/.libcrypto.so.1.0.1e.hmac from install ofopenssl-1.0.1e-42.el6_7.4.x86_64 conflicts with file from packageopenssl-1.0.1e-42.el6.x86_64
file/usr/lib64/.libssl.so.1.0.1e.hmac from install ofopenssl-1.0.1e-42.el6_7.4.x86_64 conflicts with file from packageopenssl-1.0.1e-42.el6.x86_64
………………
报错原因:rpm包冲突,使用U参数,因为有些包本地有,需要更新。
[[email protected] ~]# rpm -Uvh --aid *rpm
3、3第三种方法,搭建yum仓库(重点)
3、3、1服务端配置
3、3、1、1创建yum仓库
[[email protected] tools]# ls -ld /application/yum/centos6/x86_64
ls: cannot access /application/yum/centos6/x86_64: Nosuch file or directory
[[email protected] tools]# mkdir /application/yum/centos6/x86_64
mkdir: cannot create directory`/application/yum/centos6/x86_64‘: No such file or directory
[[email protected] tools]# mkdir -p /application/yum/centos6/x86_64
[[email protected] tools]# cd /application/yum/centos6/x86_64
[[email protected] x86_64]# pwd
/application/yum/centos6/x86_64
[[email protected] x86_64]# rz -y
#上传rpm包到此目录,此目录下面还可以包括文件夹
#D:\Program\SecureCRT\download\nginx-1.6.3-1.x86_64.rpm
#也可以直接拿过来前面生成的nginx-1.6.3-1.x86_64.rpm
[[email protected] x86_64]# cp/application/tools/nginx-1.6.3-1.x86_64.rpm /application/yum/centos6/x86_64/
[[email protected] x86_64]# ls -l nginx-1.6.3-1.x86_64.rpm
-rw-r--r-- 1 root root 1046824 Jun 29 23:04nginx-1.6.3-1.x86_64.rpm
3、3、1、2安装createrepo
[[email protected] x86_64]# rpm -qa createrepo
[[email protected] x86_64]# yum -y install createrepo
3、3、1、3初始化repodata索引文件
[[email protected] x86_64]# createrepo -pdo/application/yum/centos6/x86_64/ /application/yum/centos6/x86_64/
Spawning worker 0 with 1 pkgs
Workers Finished
Gathering worker results
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
3、3、1、4提供yum服务
#可以用Apache或nginx提供web服务,但用Python的http模块更简单,适用于内网环境
#先检查nginx服务是否开启
[[email protected] x86_64]# ps -ef|grep nginx
root 8370 1969 0 23:08 pts/1 00:00:00 grep nginx
[[email protected] x86_64]# ps -ef|grep 80
root 8372 1969 0 23:08 pts/1 00:00:00 grep 80
[[email protected] x86_64]# netstat -lntup|grep nginx
#如果开启,那么要先关闭, 以上是关于定制RPM包及yum仓库搭建(以安装nginx为例)的主要内容,如果未能解决你的问题,请参考以下文章