我们安装软件一般有如下几种方式:
1、源码编译安装 典型的就是xxx.tar.gz,这种模式安装一般就是三板斧,./configure && make && make install
2、二进制文件安装 这种方式安装最为快速,直解压,运行可执行文件即可,典型的就是MySQL安装:
tar xf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz && mv mysql-5.7.13-linux-glibc2.5-x86_64 /usr/local/mysql && cd /usr/local/mysql/bin && ./mysql_install_db
3、RPM包安装 rpm包安装又有三种方式
1) rpm -ivh *.rpm
2) yum -y localinstall *.rpm
3)搭建内网yum仓库安装,稍后介绍
一般正规公司都会有自己的软件发布和部署规范,将要安装的软件都制作成rpm包,直接在初始化的时候就安装上去,所以这里我们研究下RPM包的安装方法.我是通过FPM工具来制作RPM包的.
FPM打包工具介绍:
FPM的github:https://github.com/jordansissel/fpm
FPM功能简单说就是将一种类型的包转换成另一种类型。
1. 支持的源类型包
dir 将目录打包成所需要的类型,可以用于源码编译安装的软件包
rpm 对rpm进行转换
gem 对rubygem包进行转换
python 将python模块打包成相应的类型
2 FPM参数
详细使用见fpm –help
常用参数
-s 指定源类型
-t 指定目标类型,即想要制作为什么包
-n 指定包的名字
-v 指定包的版本号
-C 指定打包的相对路径 Change directory to here before searching forfiles
-d 指定依赖于哪些包
-f 第二次打包时目录下如果有同名安装包存在,则覆盖它
-p 输出的安装包的目录,不想放在当前目录下就需要指定
--post-install 软件包安装完成之后所要运行的脚本;同--after-install
--pre-install 软件包安装完成之前所要运行的脚本;同--before-install
--post-uninstall 软件包卸载完成之后所要运行的脚本;同--after-remove
--pre-uninstall 软件包卸载完成之前所要运行的脚本;同--before-remove
3 FPM安装
fpm是ruby写的,因此系统环境需要ruby,且ruby版本号大于1.8.5。
# 安装ruby模块
yum -y install ruby rubygems ruby-devel gcc
# 添加阿里云的Rubygems仓库,外国的源慢
gem sources -a http://mirrors.aliyun.com/rubygems/
# 移除原生的Ruby仓库
gem sources --remove http://rubygems.org/
# 安装fpm
gem install fpm
我在安装fpm的时候出现错误,错误如下:
[root@localhost scripts]# gem install fpm
ERROR: Error installing fpm:
ruby-xz requires Ruby version >= 1.9.3.
[root@localhost scripts]#
解决办法有如下两种:
解决方法是:我用的是第二种
1: 更新ruby
2: 安装旧版本的fpm
[root@localhost scripts]# gem install fpm -v 1.4.0 ===>我不知道为什么要用1.4.0求解
Successfully installed fpm-1.4.0
1 gem installed
Installing ri documentation for fpm-1.4.0...
Installing RDoc documentation for fpm-1.4.0...
[root@localhost scripts]#
使用实例--实战定制nginx的RPM包
1、安装nginx
[root@localhost ~]#yum -y install pcre-devel openssl-devel
[root@localhost ~]#useradd nginx -M -s /sbin/nologin
[root@localhost ~]#tar xf nginx-1.6.3.tar.gz
[root@localhost ~]#cd nginx-1.6.3
[root@localhost ~]# ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@localhost ~]#make && make install && ln -s /application/nginx-1.6.3/ /application/nginx
2、编写安装完rpm包的脚本
[root@localhost ~]#mkdir /server/scripts -p
[root@localhost ~]#cd /server/scripts/
[root@localhost ~]#vim nginx_rpm.sh
#!/bin/bash
useradd nginx -M -s /sbin/nologin
ln -s /application/nginx-1.6.3/ /application/nginx
[root@localhost ~]#
3、打包成rpm各式
[root@localhost scripts]# 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"}
[root@localhost scripts]# ll nginx-1.6.3-1.x86_64.rpm
-rw-r--r-- 1 root root 2273770 Jul 28 03:29 nginx-1.6.3-1.x86_64.rpm
[root@localhost scripts]#
4、安装rpm包
RPM包的三种安装方式
① RPM命令安装
[root@localhost scripts]# rpm -ivh nginx-1.6.3-1.x86_64.rpm
Preparing... ########################################### [100%]
1:nginx ########################################### [100%]
[root@localhost scripts]#
②yum命令安装RPM包
[root@localhost scripts]# yum -y install nginx-1.6.3-1.x86_64.rpm
[root@localhost scripts]#
③搭建内网的yum仓库安装
安装步骤参考地址:http://life2death.blog.51cto.com/7550586/1708417
注意事项
相对路径:
[root@localhost application]# pwd
/application
[root@localhost application]# cd nginx
[root@localhost nginx]# ll
total 16
drwxr-xr-x 2 root root 4096 Jul 28 04:29 conf
drwxr-xr-x 2 root root 4096 Jul 28 04:29 html
drwxr-xr-x 2 root root 4096 Jul 28 03:29 logs
lrwxrwxrwx 1 root root 25 Jul 28 04:29 nginx-1.6.3 -> /application/nginx-1.6.3/
drwxr-xr-x 2 root root 4096 Jul 28 04:29 sbin
[root@localhost nginx]# fpm -s dir -t rpm -n nginx -v 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"}
[root@localhost nginx]# rpm -qpl nginx-1.6.3-1.x86_64.rpm
/conf/fastcgi.conf
/conf/fastcgi.conf.default
/conf/fastcgi_params
/conf/fastcgi_params.default
/conf/koi-utf
/conf/koi-win
/conf/mime.types
/conf/mime.types.default
/conf/nginx.conf
/conf/nginx.conf.default
/conf/scgi_params
/conf/scgi_params.default
/conf/uwsgi_params
/conf/uwsgi_params.default
/conf/win-utf
/html/50x.html
/html/index.html
/logs
/nginx-1.6.3
/sbin/nginx
[root@localhost nginx]#
绝对路径:
[root@localhost ~]# pwd
/root
[root@localhost ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 /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"}
[root@localhost ~]# 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/nginx-1.6.3
/application/nginx-1.6.3/nginx-1.6.3-1.x86_64.rpm
/application/nginx-1.6.3/sbin/nginx
[root@localhost ~]#
使用rpm -qpl 命令可以查看rpm包的内容。
注:fpm类似tar打包一样,只是fpm打的包能够被yum命令识别而已。
软连接问题
[root@localhost ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 /application/nginx
no value for epoch is set, defaulting to nil {:level=>:warn}
File already exists, refusing to continue: nginx-1.6.3-1.x86_64.rpm {:level=>:fatal}
[root@localhost ~]# fpm -s dir -t rpm -n nginx -v 1.6.3 -f /application/nginx
no value for epoch is set, defaulting to nil {:level=>:warn}
Force flag given. Overwriting package at nginx-1.6.3-1.x86_64.rpm {:level=>:warn}
no value for epoch is set, defaulting to nil {:level=>:warn}
Created package {:path=>"nginx-1.6.3-1.x86_64.rpm"}
看似成功,仔细一看其实是一个软链接文件
[root@localhost ~]# rpm -qpl nginx-1.6.3-1.x86_64.rpm
/application/nginx
[root@localhost ~]#
原因:目录结尾的/问题,类似rm删除软链接目录
参考知识点:http://life2death.blog.51cto.com/7550586/1708415