记录学习笔记、分享资源工具、交流技术思想、提升工作效率

centos6.3制作RPM包实例

运维 xiaomudk 7年前 (2015-07-04) 3475次浏览 0个评论

一、实例: pcre和nginx的rpm包制作

本次实验,系统为centos6.3(x86_64),以pcre和nginx为例。来展示rpm包的制作流程。

1.先安装所需要的软件包

# yum install -y make gcc gcc-c++ autoconf automake rpm-build rpm*

2.pcre的rpm实例

先准备pcre源码包:pcre-8.35.tar.gz,下载地址: http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz

1).切换到家目录下

# cd ~

2).构建rpm的编译目录结构:

# rpmdev-setuptree
# tree rpmbuild/
rpmbuild/
├── BUILD
├── RPMS
├── SOURCES
├── SPECS
└── SRPMS
#

rpmdev-setuptree 会自动生成目录结构

3).下载源码包放在rpmbuild/SOURCES中

# cd rpmbuild/SOURCES/
# wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz

4).在rpmbuild/SPECS下生成spec文件

# cd ../SPECS/
# rpmdev-newspec -o pcre-8.35.spec

5).编辑spec文件

# vim pcre-8.35.spec
Name:           pcre
Version:        8.35
Release:        1%{?dist}
Summary:        Perl-compatible regular expression library

Group:          Development/Libraries
License:        BSD
URL:            http://www.pcre.org/
Source0:        %{name}-%{version}.tar.gz

%description

%prep
%setup -q

%build
%configure
make %{?_smp_mflags}

%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT

%clean
rm -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root,-)
%doc
%{_bindir}/*
%{_libdir}/*
%{_includedir}/*
%{_datadir}/*

%post
ln -sf %{_libdir}/libpcre.so.1 %{_libdir}/libpcre.so.0 #安装过pcre,创建这个软链

%postun
rm -rf %{_libdir}/libpcre.so.0        #卸载pcre过后,把这个软链删掉

%changelog

6).根据spec文件生成rpm包

# pwd
/root/rpmbuild/SPECS
# rpmbuild -bb pcre-8.35.spec

如果执行过程中出现contains a standard rpath '/usr/lib64' in [/usr/lib64]这个错误,则使用以下命令来生成rpm包

# QA_RPATHS=$[ 0x0001|0x0010 ] rpmbuild -bb pcre-8.35.spec        #这个错误不重要,所以改成警告给忽略掉

执行成功过后,在~/rpmbuild/RPMS/x86_64目录下,就可以看到生成好的rpm包了:

# cd ~/rpmbuild/RPMS/x86_64/;ls
pcre-8.35-1.el6.x86_64.rpm

7).安装测试

# rpm -ivh pcre-8.35-1.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:pcre                   ########################################### [100%]

安装成功!

3.nginx的rpm实例

由于nginx有现在的spec.rpm包,我们直接拿过来修改下,打成rpm包即可

1).切换到源码目录

# cd ~/rpmbuild/SOURCES

2).下载spec.rpm包放在rpmbuild/SOURCES中

# wget http://nginx.org/packages/centos/6/SRPMS/nginx-1.2.8-1.el6.ngx.src.rpm

3).解压rpm包

# rpm2cpio nginx-1.2.8-1.el6.ngx.src.rpm|cpio -idv

4).修改nginx.spec文件

先把nginx.spec移动到SPECS目录下

# mv nginx.spec ../SPECS
# cd ../SPECS

主要把suse部分,和编译nginx debug模式的配置去掉,修改了下nginx的启动用户
如果不符合自己的要求,可以自行修改

#
%define nginx_home %{_localstatedir}/cache/nginx
%define nginx_user www
%define nginx_group www

Summary: nginx is a high performance web server
Name: nginx
Version: 1.2.8
Release: 1%{?dist}.ngx
Vendor: nginx inc.
URL: http://nginx.org/

Source0: http://nginx.org/download/%{name}-%{version}.tar.gz
Source1: logrotate
Source2: nginx.init
Source3: nginx.sysconf
Source4: nginx.conf
Source5: nginx.vh.default.conf
Source6: nginx.vh.example_ssl.conf

License: 2-clause BSD-like license
Group: System Environment/Daemons

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: zlib-devel
BuildRequires: pcre   #依赖我们上面制作的rpm包
BuildRequires: perl
BuildRequires: openssl-devel
Requires: initscripts >= 8.36
Requires(pre): shadow-utils
Requires(post): chkconfig
Provides: webserver

%description
nginx [engine x] is an HTTP and reverse proxy server, as well as
a mail proxy server

%prep
%setup -q

%build
./configure \
        --prefix=%{_sysconfdir}/nginx \
        --sbin-path=%{_sbindir}/nginx \
        --conf-path=%{_sysconfdir}/nginx/nginx.conf \
        --error-log-path=%{_localstatedir}/log/nginx/error.log \
        --http-log-path=%{_localstatedir}/log/nginx/access.log \
        --pid-path=%{_localstatedir}/run/nginx.pid \
        --lock-path=%{_localstatedir}/run/nginx.lock \
        --http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \
        --http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \
        --user=%{nginx_user} \
        --group=%{nginx_group} \
        --with-http_ssl_module \
        --with-http_realip_module \
        --with-http_addition_module \
        --with-http_sub_module \
        --with-http_dav_module \
        --with-http_flv_module \
        --with-http_mp4_module \
        --with-http_gzip_static_module \
        --with-http_random_index_module \
        --with-http_secure_link_module \
        --with-http_stub_status_module \
        --with-mail \
        --with-mail_ssl_module \
        --with-file-aio \
        --with-ipv6 \
        --with-cc-opt="%{optflags} $(pcre-config --cflags)" \
        $*
make %{?_smp_mflags}

%install
%{__rm} -rf $RPM_BUILD_ROOT
%{__make} DESTDIR=$RPM_BUILD_ROOT install

%{__mkdir} -p $RPM_BUILD_ROOT%{_datadir}/nginx
%{__mv} $RPM_BUILD_ROOT%{_sysconfdir}/nginx/html $RPM_BUILD_ROOT%{_datadir}/nginx/

%{__rm} -f $RPM_BUILD_ROOT%{_sysconfdir}/nginx/*.default
%{__rm} -f $RPM_BUILD_ROOT%{_sysconfdir}/nginx/fastcgi.conf

%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/log/nginx
%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/run/nginx
%{__mkdir} -p $RPM_BUILD_ROOT%{_localstatedir}/cache/nginx

%{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d
%{__rm} $RPM_BUILD_ROOT%{_sysconfdir}/nginx/nginx.conf
%{__install} -m 644 -p %{SOURCE4} \
   $RPM_BUILD_ROOT%{_sysconfdir}/nginx/nginx.conf
%{__install} -m 644 -p %{SOURCE5} \
   $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/default.conf
%{__install} -m 644 -p %{SOURCE6} \
   $RPM_BUILD_ROOT%{_sysconfdir}/nginx/conf.d/example_ssl.conf

%{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig
%{__install} -m 644 -p %{SOURCE3} \
   $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig/nginx

# install SYSV init stuff
%{__mkdir} -p $RPM_BUILD_ROOT%{_initrddir}
%{__install} -m755 %{SOURCE2} \
   $RPM_BUILD_ROOT%{_initrddir}/nginx

# install log rotation stuff
%{__mkdir} -p $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d
%{__install} -m 644 -p %{SOURCE1} \
   $RPM_BUILD_ROOT%{_sysconfdir}/logrotate.d/nginx

%clean
%{__rm} -rf $RPM_BUILD_ROOT

%files
%defattr(-,root,root)

%{_sbindir}/nginx

%dir %{_sysconfdir}/nginx
%dir %{_sysconfdir}/nginx/conf.d

%config(noreplace) %{_sysconfdir}/nginx/nginx.conf
%config(noreplace) %{_sysconfdir}/nginx/conf.d/default.conf
%config(noreplace) %{_sysconfdir}/nginx/conf.d/example_ssl.conf
%config(noreplace) %{_sysconfdir}/nginx/mime.types
%config(noreplace) %{_sysconfdir}/nginx/fastcgi_params
%config(noreplace) %{_sysconfdir}/nginx/scgi_params
%config(noreplace) %{_sysconfdir}/nginx/uwsgi_params
%config(noreplace) %{_sysconfdir}/nginx/koi-utf
%config(noreplace) %{_sysconfdir}/nginx/koi-win
%config(noreplace) %{_sysconfdir}/nginx/win-utf

%config(noreplace) %{_sysconfdir}/logrotate.d/nginx
%config(noreplace) %{_sysconfdir}/sysconfig/nginx
%{_initrddir}/nginx

%dir %{_datadir}/nginx
%dir %{_datadir}/nginx/html
%{_datadir}/nginx/html/*

%attr(0755,root,root) %dir %{_localstatedir}/cache/nginx
%attr(0755,root,root) %dir %{_localstatedir}/log/nginx

%pre
# Add the "nginx" user
getent group %{nginx_group} >/dev/null || groupadd -r %{nginx_group}
getent passwd %{nginx_user} >/dev/null || \
    useradd -r -g %{nginx_group} -s /sbin/nologin \
    -d %{nginx_home} -c "nginx user"  %{nginx_user}
exit 0

%post
# Register the nginx service
if [ $1 -eq 1 ]; then
    /sbin/chkconfig --add nginx
    # print site info
    cat <<BANNER
----------------------------------------------------------------------

Thanks for using NGINX!

Check out our community web site:
* http://nginx.org/en/support.html

If you have questions about commercial support for NGINX please visit:
* http://www.nginx.com/support.html

----------------------------------------------------------------------
BANNER

    # Touch and set permisions on default log files on installation

    if [ -d %{_localstatedir}/log/nginx ]; then
        if [ ! -e %{_localstatedir}/log/nginx/access.log ]; then
            touch %{_localstatedir}/log/nginx/access.log
            %{__chmod} 640 %{_localstatedir}/log/nginx/access.log
            %{__chown} %{nginx_user}:%{nginx_group} %{_localstatedir}/log/nginx/access.log     #修改成www用户和组
        fi

        if [ ! -e %{_localstatedir}/log/nginx/error.log ]; then
            touch %{_localstatedir}/log/nginx/error.log
            %{__chmod} 640 %{_localstatedir}/log/nginx/error.log
            %{__chown} %{nginx_user}:%{nginx_group} %{_localstatedir}/log/nginx/error.log      #修改成www用户和组
        fi
    fi
fi

%preun
if [ $1 -eq 0 ]; then
    /sbin/service nginx stop > /dev/null 2>&1
    /sbin/chkconfig --del nginx
fi

%postun
if [ $1 -ge 1 ]; then
    /sbin/service nginx upgrade &>/dev/null || :
fi

%changelog
* Tue Apr  2 2013 Sergey Budnevitch <[email protected]>
- set permissions on default log files at installation
- 1.2.8

把nginx.conf的配置文件里的用户也修改一下

# sed -i 's/^user.*;/user www www;/' ~/rpmbuild/SOURCES/nginx.conf

5).根据spec文件生成rpm包

# pwd
/root/rpmbuild/SPECS
# rpmbuild -bb nginx.spec

执行成功过后,在~/rpmbuild/RPMS/x86_64目录下,就可以看到生成好的rpm包了:

# cd ~/rpmbuild/RPMS/x86_64/;ls
nginx-1.2.8-1.el6.ngx.x86_64.rpm  pcre-8.35-1.el6.x86_64.rpm

6).安装测试

安装nginx

# rpm -ivh nginx-1.2.8-1.el6.ngx.x86_64.rpm
Preparing...                ########################################### [100%]
   1:nginx                  ########################################### [100%]
----------------------------------------------------------------------

Thanks for using NGINX!

Check out our community web site:
* http://nginx.org/en/support.html

If you have questions about commercial support for NGINX please visit:
* http://www.nginx.com/support.html

----------------------------------------------------------------------

启动nginx

# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]

访问测试

# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

OK,nginx安装成功!


本网站采用知识共享署名-相同方式共享 4.0 国际许可协议进行授权
转载请注明原文链接:centos6.3制作RPM包实例
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址