肯定有人纳闷,为啥单独把CentOS6下的nginx进行编译? 那是因为Centos6由于已经过时不再更新,而CentOS7官方还在持续更新,没必要重复造轮子,centos7可以参阅此连接: nginx-centos7-rpm
安装rpmbuild:
yum install rpm-build zlib-devel openssl-devel gcc perl-devel pam-devel unzip -y mkdir -p /root/rpmbuild/{SOURCES,SPECS}
下载相应二进制包到SOURCES目录:
cd /root/rpmbuild/SOURCES wget https://nginx.org/download/nginx-1.20.1.tar.gz
如果需要利用service来控制Nginx,SOURCES路径中还需要准备一个nginx启动程序:
vi nginx:
#!/bin/sh # # 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: /etc/nginx/nginx.conf # config: /usr/local/nginx/conf/nginx.conf # config: /etc/sysconfig/nginx ## pidfile: /var/run/nginx/nginx.pid # pidfile: /usr/local/nginx/logs/nginx.pid # 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) #NGINX_CONF_FILE="/etc/nginx/nginx.conf" 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' -` 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() { [ -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 } 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) rh_status_q && 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
确保SOURCES目录中存在两个文件,一个tar.gz,一个nginx文件。
接下来进入SPEC目录中,编写nginx.spec:
cd /root/rpmbuild/SPECS/
vi nginx.spec
Name:nginx Version:1.21.0 Release: 1 Summary: Nginx is a web server software. License:GPL URL: https://nginx.org Source0:nginx-1.21.0.tar.gz Source1: nginx %description nginx [engine x] is an HTTP and reverse proxy server. %post useradd -s /sbin/nologin nginx -M %prep tar -xf %{_sourcedir}/nginx-1.21.0.tar.gz -C %{_builddir} %build cd %{_builddir}/nginx-1.21.0/ ./configure \ --user=nginx \ --prefix=/usr \ --conf-path=/etc/nginx/nginx.conf \ --modules-path=/etc/nginx/modules/ \ --pid-path=/run/nginx.pid \ --lock-path=/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ %install cd %{_builddir}/nginx-1.21.0/ make install DESTDIR=%{buildroot}/ INSTALLDIRS=vendor %{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}/etc/rc.d/init.d/nginx %files /etc/nginx/* /usr/html/*html /usr/sbin/nginx /var/log/nginx %attr(0755,root,root) /etc/rc.d/init.d/nginx %changelog
注意,如果对配置文件不熟悉,请勿进行改动。
修改完配置后,我们进行编译:
rpmbuild -ba nginx.spec
编译后出现以下内容,无报错的话,表示编译成功。
+ /usr/bin/install -p -D -m 0755 /root/rpmbuild/SOURCES/nginx /root/rpmbuild/BUILDROOT/nginx-1.21.0-1.x86_64/etc/rc.d/init.d/nginx + /usr/lib/rpm/check-buildroot + /usr/lib/rpm/redhat/brp-compress + /usr/lib/rpm/redhat/brp-strip /usr/bin/strip + /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip + /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump + /usr/lib/rpm/brp-python-bytecompile /usr/bin/python + /usr/lib/rpm/redhat/brp-python-hardlink + /usr/lib/rpm/redhat/brp-java-repack-jars Processing files: nginx-1.21.0-1.x86_64 Requires(interp): /bin/sh Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 Requires(post): /bin/sh Requires: /bin/sh libc.so.6()(64bit) libc.so.6(GLIBC_2.10)(64bit) libc.so.6(GLIBC_2.2.5)(64bit) libc.so.6(GLIBC_2.3)(64bit) libc.so.6(GLIBC_2.3.2)(64bit) libc.so.6(GLIBC_2.3.4)(64bit) libc.so.6(GLIBC_2.4)(64bit) libc.so.6(GLIBC_2.7)(64bit) libcrypt.so.1()(64bit) libcrypt.so.1(GLIBC_2.2.5)(64bit) libdl.so.2()(64bit) libdl.so.2(GLIBC_2.2.5)(64bit) libpcre.so.0()(64bit) libpthread.so.0()(64bit) libpthread.so.0(GLIBC_2.2.5)(64bit) librt.so.1()(64bit) librt.so.1(GLIBC_2.2.5)(64bit) libz.so.1()(64bit) rtld(GNU_HASH) Checking for unpackaged file(s): /usr/lib/rpm/check-files /root/rpmbuild/BUILDROOT/nginx-1.21.0-1.x86_64 Wrote: /root/rpmbuild/SRPMS/nginx-1.21.0-1.src.rpm Wrote: /root/rpmbuild/RPMS/x86_64/nginx-1.21.0-1.x86_64.rpm Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.RqKdzg + umask 022 + cd /root/rpmbuild/BUILD + /bin/rm -rf /root/rpmbuild/BUILDROOT/nginx-1.21.0-1.x86_64 + exit 0
rpm包存储路径为:
/root/rpmbuild/RPMS/x86_64/nginx-1.21.0-1.x86_64.rpm
源码包存储路径为:
/root/rpmbuild/SRPMS/nginx-1.21.0-1.src.rpm
注意:
如果需要对nginx设置开机自启,我们需要将nginx文件添加到chkconfig列表中:
chkconfig --add /etc/init.d/nginx
再进行开机项添加:
chkconfig nginx on
如果需要编译其他版本,对文件中的相应版本号,以及源码包进行替换即可。不再赘述。
博客提供一个编译好的nginx-1.21.0-el6.x86_64版本分享:nginx-1.21.0-el6.x86_64.rpm