企业内部若是使用本身的Yum服务器,不但占用带宽少、速度更快,并且能够更加灵活方便的自定义配置,能有效提高平常工做效率。php
1、基本概念linux
1. RPMnginx
全称是The RPM Package Manager。用于在CentOS系统中安装/卸载软件。centos
2. Yumbash
全称是Yellow Dog Updater Modified。用于管理RPM包,完成安装/卸载/升级,重要的是可以处理包之间的依赖关系。服务器
2、同步外部yum源ide
第一步固然是创建内网的yum源,基本上yum源就是一个静态的http服务,yum client根据repo文件的配置读取远程数据,下载rpm包到本地进行安装。具体安装步骤再也不赘述,彻底参考【3】便可。工具
固然我使用的源有所不一样,主要包括如下几个源:网站
CentOS 6 Base: rsync://mirrors.ustc.edu.cn/centos/6/os/ CentOS 6 Update: rsync://mirrors.ustc.edu.cn/centos/6/updates/ CentOS 6 Epel: rsync://mirrors.ustc.edu.cn/epel/6/ Percona: rsync://rsync.percona.com/rsync/centos/6/os/x86_64/
2、自定义yum源ui
上面是同步外部的yum源到内网,那么如何定义本身的yum源呢?这个其实也很是简单。
1. 在上述的数据文件夹下新建目录,结构相似于
my_yum/ ├── Centos-5 │ └── repodata └── Centos-6 ├── Packages └── repodata
2. 将经常使用的RPM文件放入Packages文件夹,执行
createrepo -p -d -o Centos-6 Centos-6
3. 建立my_yum.repo文件,其中
baseurl=http://yum-server.xxx.com/my_yum/Centos-$releasever/
3、自定义rpm文件
实际应用中,还有许多程序是没有rpm安装包的,必须经过编译安装并自定义配置,这部分相对复杂一些。下面以tengine 2.1.0的打包过程为例。
1. RPM打包
1)安装RPM打包工具
yum -y install rpm-build
2)依照 rpmbuild 规范设定一个目录结构。
rpmbuild/ ├── BUILD ├── BUILDROOT ├── RPMS ├── SOURCES ├── SPECS └── SRPMS
3)将源代码和附带文件放在目录中合适的位置。
下载tengine-2.1.0.tar.gz到SOURCES目录
4)建立 spec 文件。
#This is the spec file for tengine Name: tengine Version: 2.1.0 Release: 1 Summary: Tengine from Taobao Inc. Group: Applications/Productivity License: BSD URL: http://tengine.taobao.org/ Source0: tengine-2.1.0.tar.gz BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) BuildRequires: libevent BuildRequires: libevent-devel BuildRequires: gcc %description Tengine from Taobao Inc. %prep %setup -q %build ./configure --with-http_ssl_module --with-http_stub_status_module make %install rm -rf %{buildroot} make install DESTDIR=%{buildroot} install -m 755 -d %{buildroot}/%{_bindir} ln -s /usr/local/nginx/sbin/nginx %{buildroot}/%{_bindir}/nginx %clean #rm -rf %{buildroot} %files %defattr(-,root,root,-) /usr/local/nginx /%{_bindir}/* %doc %changelog
4)编译 RPM。
rpmbuild -v -bb --clean SPECS/tengine-2.1.0.spec
当编译顺利完成,生成的rpm文件会放置在RPMS目录,便可同步到本身的yum源进行管理。
2. 添加自定义配置
从spec文件能够看出,RPM打包的过程就是经过脚本执行configure->make->make install,最后将编译好的文件整合到一个rpm文件中。那么若是要自定义配置,只须要在make install以后用本身的配置文件覆盖原始配置。
1)编写配置文件
按照正常语法编写tengine的配置文件,我修改了整个conf文件的结构,放在SOURCES/tengine_conf文件夹中。
2)spec文件Source1
在Source0下面添加一行
Source1: tengine_conf
这样在spec文件中就会到SOURCES文件夹寻找tengine_conf
3)spec文件install
在%install节中添加
rm -rf %{buildroot}/usr/local/nginx/conf cp -r %{SOURCE1} %{buildroot}/usr/local/nginx/conf
删除make install生成的conf,用tengine_conf代替
这样就能生成一个自定义配置的rpm文件了,经过修改Version号和Release号能够方便的升级/变动。主要是理解RPM打包的原理,注意一些内部宏变量的使用,注意相对路径和绝对路径的问题。建议反复阅读【5】。
【1】rpm - 官方网站
【2】yum - 官方网站