CentOS 7 Build Tengine RPM Package

Posted knmax

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS 7 Build Tengine RPM Package相关的知识,希望对你有一定的参考价值。

安装rpmbuild工具及tengine编译环境

yum -y install rpmdevtools
yum -y install rpm-build wget make m4 gcc-c++ autoconf automake lua-devel  pcre-devel  libxml2-devel gd-devel perl-ExtUtils-Embed libxslt-devel GeoIP-devel openssl-devel

建立普通用户,在普通用户下进行包的制作

这样防止制作包的过程中出错造成对系统的影响

useradd knmax
su - knmax

生成rpmbuild目录结构

这个也可以手动去创建目录,当然使用命令更方便

$ rpmdev-setuptree
$ tree rpmbuild/
rpmbuild/
├── BUILD   //打包过程中的工作目录
├── RPMS    //存放生成的二进制包
├── SOURCES  //打包资源,源码补丁文件
├── SPECS  //spec文档
└── SRPMS  //存放生成的源码包

注意:如果需要改变次默认位置,可以修改配置文件:~/.rpmmacros中变量_topdir对应的值即可

获取源码、配置文件、服务启动脚本、主页放入SOURCE目录

$ cd rpmbuild/SOURCES/
$ wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
$ ls rpmbuild/SOURCES/
index.html  init.nginx  nginx.conf  nginx.logrotate  tengine-2.2.0.tar.gz

文件内容

nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
    multi_accept on;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush     on;
    server_tokens off;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length  1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types      text/plain application/x-javascript text/css application/xml;
    gzip_vary on;

    charset utf-8;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    include /etc/nginx/conf.d/*.conf;
}

init.nginx

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

nginx.logrotate

/var/log/nginx/*log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 $(cat /var/run/nginx.pid 2>/dev/null) 2>/dev/null || :
    endscript
}

index.html

<h1>This is knmax_tengine test page</h1>

编写spec文件

$ vim SPECS/tengine.spec
%define nginx_user      nginx
%define nginx_group     %{nginx_user}
%define nginx_home      %{_localstatedir}/lib/nginx
%define nginx_home_tmp  %{nginx_home}/tmp
%define nginx_logdir    %{_localstatedir}/log/nginx
%define nginx_confdir   %{_sysconfdir}/nginx
%define nginx_datadir   /var/www
%define nginx_webroot   %{nginx_datadir}/html

Name:           tengine
Version:        2.2.0
Release:        1%{?dist}
Summary:        Small and High Performance HTTP and Reverse Proxy Server
Group:          System Environment/Daemons
License:        BSD
URL:            http://knmax.com
BuildRoot:      %{_tmppath}/nginx-%{version}-%{release}-root-%(%{__id_u} -n)
Packager:       Ron<[email protected]>

BuildRequires:  pcre-devel,zlib-devel,perl,perl(ExtUtils::Embed),libxslt-devel,GeoIP-devel,gd-devel
Requires:       pcre,openssl,GeoIP,gd, perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))
# for /usr/sbin/useradd
Requires(pre):      shadow-utils
#Requires(post):     chkconfig
Requires(post):     systemd
# for /sbin/service
#Requires(preun):    chkconfig, initscripts
#Requires(postun):   initscripts
Requires(preun):    systemd
Requires(postun):   systemd

Source0:    %{name}-%{version}.tar.gz
Source1:    init.nginx
Source2:    nginx.logrotate
Source3:        nginx.conf
Source100:      index.html
%description
Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3
proxy server

%prep
%setup -q

%build
export DESTDIR=%{buildroot}
./configure     --user=%{nginx_user}     --group=%{nginx_group}     --prefix=%{nginx_datadir}     --sbin-path=%{_sbindir}/nginx     --conf-path=%{nginx_confdir}/nginx.conf     --error-log-path=%{nginx_logdir}/error.log     --http-log-path=%{nginx_logdir}/access.log     --http-client-body-temp-path=%{nginx_home_tmp}/client_body     --http-proxy-temp-path=%{nginx_home_tmp}/proxy     --http-fastcgi-temp-path=%{nginx_home_tmp}/fastcgi     --http-uwsgi-temp-path=%{nginx_home_tmp}/uwsgi     --http-scgi-temp-path=%{nginx_home_tmp}/scgi     --pid-path=%{_localstatedir}/run/nginx.pid     --lock-path=%{_localstatedir}/lock/subsys/nginx     --with-http_ssl_module     --with-http_realip_module     --with-http_addition_module     --with-http_xslt_module     --with-http_image_filter_module     --with-http_geoip_module     --with-http_sub_module     --with-http_dav_module     --with-http_flv_module     --with-http_gzip_static_module     --with-http_random_index_module     --with-http_secure_link_module     --with-http_degradation_module     --with-http_stub_status_module     --with-http_perl_module     --with-file-aio     --with-cc-opt="%{optflags} $(pcre-config --cflags)"     --with-ld-opt="-Wl,-E" # so the perl module finds its symbols
make %{?_smp_mflags}



%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot} INSTALLDIRS=vendor
find %{buildroot} -type f -name .packlist -exec rm -f {} ;
find %{buildroot} -type f -name perllocal.pod -exec rm -f {} ;
find %{buildroot} -type f -empty -exec rm -f {} ;
find %{buildroot} -type f -exec chmod 0644 {} ;
find %{buildroot} -type f -name '*.so' -exec chmod 0755 {} ;
chmod 0755 %{buildroot}%{_sbindir}/nginx
#%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}%{_initrddir}/nginx
%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}%{_unitdir}/nginx.service
%{__install} -p -D -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/logrotate.d/nginx
%{__install} -p -d -m 0755 %{buildroot}%{nginx_confdir}/conf.d
%{__install} -p -d -m 0755 %{buildroot}%{nginx_home_tmp}
%{__install} -p -d -m 0755 %{buildroot}%{nginx_logdir}
%{__install} -p -d -m 0755 %{buildroot}%{nginx_webroot}
%{__install} -p -m 0644 %{SOURCE100} %{buildroot}%{nginx_webroot}
%{__install} -p -m 0755 %{SOURCE3} %{buildroot}%{nginx_confdir}

%clean
rm -rf %{buildroot}

%pre
if [ $1 == 1 ]; then
    %{_sbindir}/useradd -c "Nginx user" -s /bin/false  %{nginx_user} 2>/dev/null
    %{_bindir}/mkdir %{nginx_datadir} 2>/dev/null
fi

%post
if [ $1 == 1 ]; then
#    /sbin/chkconfig --add nginx
     %{_bindir}/systemctl daemon-reload >/dev/null 2>&1 || :
     %{_bindir}/systemctl enable nginx >/dev/null 2>&1 || :
     %{_bindir}/systemctl starx nginx >/dev/null 2>&1 || :
fi

%preun
if [ $1 = 0 ]; then
     %{_bindir}/systemctl stop nginx >/dev/null 2>&1 || :
     %{_bindir}/systemctl disabled nginx >/dev/null 2>&1 || :
fi

%postun
if [ $1 == 2 ]; then
     %{_bindir}/systemctl restart nginx || :
fi

%files
%defattr(-,root,root,-)
%doc LICENSE CHANGES README
%{nginx_datadir}/
%{_sbindir}/nginx
%{_mandir}/man3/nginx.3pm.gz
#%{_initrddir}/nginx
%{_unitdir}/nginx.service
%dir %{nginx_confdir}
%dir %{nginx_confdir}/conf.d
%dir %{nginx_logdir}
%config(noreplace) %{nginx_confdir}/win-utf
%config(noreplace) %{nginx_confdir}/nginx.conf.default
%config(noreplace) %{nginx_confdir}/mime.types.default
%config(noreplace) %{nginx_confdir}/fastcgi.conf
%config(noreplace) %{nginx_confdir}/fastcgi.conf.default
%config(noreplace) %{nginx_confdir}/fastcgi_params
%config(noreplace) %{nginx_confdir}/fastcgi_params.default
%config(noreplace) %{nginx_confdir}/scgi_params
%config(noreplace) %{nginx_confdir}/scgi_params.default
%config(noreplace) %{nginx_confdir}/uwsgi_params
%config(noreplace) %{nginx_confdir}/uwsgi_params.default
%config(noreplace) %{nginx_confdir}/koi-win
%config(noreplace) %{nginx_confdir}/koi-utf
%config(noreplace) %{nginx_confdir}/nginx.conf
%config(noreplace) %{nginx_confdir}/mime.types
%config(noreplace) %{nginx_confdir}/browsers
%config(noreplace) %{nginx_confdir}/module_stubs
%config(noreplace) %{_sysconfdir}/logrotate.d/nginx
%doc %{nginx_webroot}/index.html
%dir %{perl_vendorarch}/auto/nginx
%{perl_vendorarch}/nginx.pm
%{perl_vendorarch}/auto/nginx/nginx.so
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_webroot}
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home}
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home_tmp}

%changelog
 * Fri Jul 7 2017 <[email protected]> - 2.2.0
- initial package

常用路径及宏变量:https://fedoraproject.org/wiki/Packaging:RPMMacros?rd=Packaging/RPMMacros#RPM_directory_macros
也可以通过rpmbuild --showrc查看

打包生成rpm包

$ rpmbuild -ba SPECS/tengine.spec
$ ls RPMS/x86_64/tengine-*
RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm  RPMS/x86_64/tengine-debuginfo-2.2.0-1.el7.centos.x86_64.rpm
$ ls SRPMS/
tengine-2.2.0-1.el7.centos.src.rpm

安装rpm包

# rpm -ivh /home/knmax/rpmbuild/RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm
# rpm -qi tengine
Name        : tengine
Version     : 2.2.0
Release     : 1.el7.centos
Architecture: x86_64
Install Date: Sat 08 Jul 2017 12:27:57 AM CST
Group       : System Environment/Daemons
Size        : 1586558
License     : BSD
Signature   : (none)
Source RPM  : tengine-2.2.0-1.el7.centos.src.rpm
Build Date  : Sat 08 Jul 2017 12:26:48 AM CST
Build Host  : localhost
Relocations : (not relocatable)
Packager    : Ron<[email protected]>
URL         : http://knmax.com
Summary     : Small and High Performance HTTP and Reverse Proxy Server
Description :
Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3
proxy server

为rpm包签名

生成GPG签名密钥

# gpg --gen-key  一路操作下去就行

查看生成的密钥

# gpg --list-keys
/root/.gnupg/pubring.gpg
------------------------
pub   2048R/176DC0D0 2017-07-07 [expires: 2018-07-07]
uid                  KNAMX (GPG-RPM-KEY) <[email protected]>
sub   2048R/3F02B01C 2017-07-07 [expires: 2018-07-07]

导出公钥

# gpg --export -a "KNAMX" > RPM-GPG-KEY-KNMAX

编缉 .rpmmacros说明我们用哪一个密钥加密,我们用root加密的那就在/root下编辑

# vim ~/.rpmmacros
%_gpg_name KNAMX

为rpm包加签名

# yum install rpm-sign
# rpm --addsign /home/knmax/rpmbuild/RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm
Enter pass phrase:
Pass phrase is good.
/home/knmax/rpmbuild/RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm:

导入公钥,检查rpm包

# rpm --import RPM-GPG-KEY-KNMAX
# rpm --checksig /home/knmax/rpmbuild/RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm
/home/knmax/rpmbuild/RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm: rsa sha1 (md5) pgp md5 OK

重新安装软件包,查看信息

# rpm -e tengine
# rpm -ivh /home/knmax/rpmbuild/RPMS/x86_64/tengine-2.2.0-1.el7.centos.x86_64.rpm
# rpm -qi tengine
Name        : tengine
Version     : 2.2.0
Release     : 1.el7.centos
Architecture: x86_64
Install Date: Sat 08 Jul 2017 01:41:37 AM CST
Group       : System Environment/Daemons
Size        : 1586558
License     : BSD
Signature   : RSA/SHA1, Sat 08 Jul 2017 01:36:35 AM CST, Key ID 1893bb30176dc0d0
Source RPM  : tengine-2.2.0-1.el7.centos.src.rpm
Build Date  : Sat 08 Jul 2017 12:26:48 AM CST
Build Host  : localhost
Relocations : (not relocatable)
Packager    : Ron<[email protected]>
URL         : http://knmax.com
Summary     : Small and High Performance HTTP and Reverse Proxy Server
Description :
Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3
proxy server

以上是关于CentOS 7 Build Tengine RPM Package的主要内容,如果未能解决你的问题,请参考以下文章

CentOS 7.4 Tengine安装配置详解

CentOS 7.4 Tengine安装配置详解

编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)

CentOS 7.4 Tengine安装配置详解

CentOS 7.4 Tengine安装配置详解

CentOS 7.4 Tengine安装配置详解