一、安装软件
yum install rpmdevtools -y
二、RPM包制作
- 建立工作目录
rpmdev-setuptree #执行此条命令,会在用户家目录下生成以下几个目录
默认位置 | 宏代码 | 名称 | 用途 |
---|---|---|---|
BUILD | %_builddir | 构建目录 | 源码包解压至此并完成编译 |
RPMS | %_prmdir | 标准rpm包目录 | 生成/保存二进制 RPM 包 |
SOURCES | %_sourcedir | 源代码目录 | 保存源码包(如 .tar 包)和所有 patch 补丁 |
SPECS | %_specdir | spec文件目录 | 保存rpm包配置文件(.spec) |
SRPMS | %_srcrpmdir | 源代码rpm包目录 | 生成/保存源码 RPM 包(SRPM) |
BUILDROOT | %_buildrootdir | 最终安装目录 | 保存 %install 阶段安装的文件 |
二、把源码文件放置到 SOURCES 目录下
- nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user www;
worker_processes 4;
error_log logs/error.log;
pid logs/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
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 logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include mime.types;
default_type application/octet-stream;
client_header_buffer_size 32k;
large_client_header_buffers 4 64k;
client_max_body_size 1024m;
gzip on;
gzip_static on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 1k;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_proxied off;
gzip_vary on;
gzip_comp_level 4;
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include conf.d/*.conf;
}
- nginx 启动脚本
#!/bin/bash
#nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
pidfile="/usr/local/nginx/logs/nginx.pid"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
status $prog
if [ $? != 0 ]; then
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
#make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
fi
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
start && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
- fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
三、编写 spec 文件(SPECS 目录下)
# vim nginx.spec
Name: nginx
Version: 1.12.2
Release: 1%{?dist}
Summary: nginx-1.12.2.tar.gz
Group: Applications/Archiving
License: GPLv2
URL: http://www.baidu.com
# 所有文件都在 source 目录下
Source0: %{name}-%{version}.tar.gz
Source1: nginx
Source2: nginx.conf
Source3: fastcgi_params
Source4: nginx.logrotate
Source5: 80.conf
BuildRoot: %_topdir/BUILDROOT
BuildRequires: gcc,gcc-c++
Requires: openssl,openssl-devel,pcre,pcre-devel
%description
This is nginx-1.12.2.rpm!
%prep
%setup -q
%build
# 用 %configure 会出现 "--build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --target=x86_64-redhat-linux-gnu" 这个错误
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre
make %{?_smp_mflags} # 如果就多处理器的话make时并行编译
%install # 主要用于完成实际安装软件必须执行的命令
rm -rf %{buildroot}
make install DESTDIR=%{buildroot}
%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx
%{__install} -p -D %{SOURCE2} %{buildroot}/usr/local/nginx/conf/nginx.conf
%{__install} -p -D %{SOURCE3} %{buildroot}/usr/local/nginx/conf/fastcgi_params
%{__install} -p -D %{SOURCE4} %{buildroot}/etc/logrotate.d/nginx
%{__install} -p -D %{SOURCE5} %{buildroot}/usr/local/nginx/conf/conf.d/80.conf
%pre # rpm 安装前执行的操作
if [ $1 == 1 ];then # $1 有三个值,0表示卸载,1表示安装,2表示升级
# 判断是否存在www用户,以及/www目录,重复创建用户会使rpm包制作失败
awk '{print $1}' /etc/passwd | grep -q www || /usr/sbin/useradd www -s /sbin/nologin -M &>/dev/null
[ -d /www ] || mkdir /www
chown www.www /www -R
echo "Hello World!" >>/www/index.html
fi
%post 安装后执行的操作
if [ $1 == 1 ];then
/sbin/chkconfig --add %{name}
/sbin/chkconfig %{name} on
echo '# Add #下面主要是内核参数的优化
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535' >> /etc/sysctl.conf
sysctl -p 2>&1 /dev/null
fi
%preun # 卸载前执行的操作
if [ $1 == 0 ];then
/etc/init.d/nginx stop &>/dev/null
fi
%clean
rm -fr %{buildroot}
%files # rpm要包含的文件
%defattr(-,root,root,-) #设置默认权限
#将需要打包的文件或目录写到下面
/etc/logrotate.d/nginx
/usr/local/nginx/
%attr(0755,root,root) /etc/rc.d/init.d/nginx
%config(noreplace) /usr/local/nginx/conf/nginx.conf
%config(noreplace) /usr/local/nginx/conf/conf.d/80.conf
%config(noreplace) /usr/local/nginx/conf/fastcgi_params
%changelog
四、打包
rpmbuild
-ba 既生成src.rpm又生成二进制rpm
-bs 只生成src的rpm
-bb 只生二进制的rpm
-bp 执行到pre
-bc 执行到 build段
-bi 执行install段
-bl 检测有文件没包含
一般先rpmbuild -bp ,再-bc 再-bi 如果没问题,rpmbuild -bb制作rpm包