操作系统 |
CentOS release 6.5 (Final) |
服务器ip |
192.168.1.129 |
nginx版本 |
1.9.1-->1.9.7 |
pcre版本 |
8.30 |
openssl版本 |
1.0.2a |
zlib版本 |
1.2.7 |
源码解压目录 |
/usr/local/src |
源码安装目录 |
/usr/local/softname |
nginx安装目录 |
/usr/local/nginx-1.9.1/ |
安装前准备
[root@lb ~]# iptables -F ##清空防火墙
[root@lb ~]# iptables -X
[root@lb ~]# iptables -Z
[root@lb ~]# /etc/init.d/iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
[root@lb ~]# setenforce 0 ##临时关闭selinux
[root@lb ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config ##永久关闭selinux
[root@lb ~]# yum -y install vim ntpdate wget gcc gcc-c++ patch unzip ##安装必要的软件
[root@lb ~]# ntpdate 0.pool.ntp.org ##同步时间服务器
[root@lb ~]# hwclock
安装pcre
[root@lb ~]# tar xvfz pcre-8.30.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/pcre-8.30
[root@lb pcre-8.30]# ./configure --prefix=/usr/local/pcre-8.30
[root@lb pcre-8.30]# make && make install
安装openssl
[root@lb ~]# tar xvfz openssl-1.0.2a.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/openssl-1.0.2a
[root@lb openssl-1.0.2a]# ./config --prefix=/usr/local/openssl-1.0.2a
[root@lb openssl-1.0.2a]# make && make install
安装zlib
[root@lb ~]# tar xvfz zlib-1.2.7.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/zlib-1.2.7/
[root@lb zlib-1.2.7]# ./configure --prefix=/usr/local/zlib-1.2.7
[root@lb zlib-1.2.7]# make && make install
安装nginx
[root@lb ~]# groupadd www
[root@lb ~]# useradd -g www -s /sbin/nologin www
[root@lb ~]# unzip nginx_upstream_check_module-master.zip
[root@lb ~]# tar xvfz nginx-1.9.1.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/nginx-1.9.1/src ##为nginx打补丁使其支持代理后端服务器健康检查
[root@lb src]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.9.2+.patch
patching file http/modules/ngx_http_upstream_hash_module.c
patching file http/modules/ngx_http_upstream_ip_hash_module.c
patching file http/modules/ngx_http_upstream_least_conn_module.c
patching file http/ngx_http_upstream_round_robin.c
patching file http/ngx_http_upstream_round_robin.h
[root@lb src]# cd /usr/local/src/nginx-1.9.1/
[root@lb nginx-1.9.1]# ./configure \ ##编译安装
--prefix=/usr/local/nginx-1.9.1 \ ##指定nginx安装目录
--sbin-path=/usr/local/nginx-1.9.1/sbin/nginx \ ##可执行文件目录
--conf-path=/etc/nginx/nginx.conf \ ##主配置文件
--error-log-path=/var/log/nginx/error.log \ ##错误日志文件
--http-log-path=/var/log/nginx/access.log \ ##访问日志文件
--pid-path=/var/run/nginx/nginx.pid \ ##进程PID文件
--lock-path=/var/lock/nginx.lock \ ##锁文件
--user=www \ ##运行nginx非特权用户
--group=www \ ##运行nginx非特权用户组
--with-http_ssl_module \ ##支持https请求
--with-http_realip_module \ ##允许从请求标头更改客户端的IP地址值
--with-http_gzip_static_module \ ##在线实时压缩输出数据流
--with-http_stub_status_module \ ##获取Nginx自上次启动以来的工作状态
--with-pcre=/usr/local/src/pcre-8.30 \ ##pcre库文件目录,指源码解压目录
--with-zlib=/usr/local/src/zlib-1.2.7 \ ##zlib库文件目录,指源码解压目录
--with-openssl=/usr/local/src/openssl-1.0.2a \ ##openssl库文件目录,指源码解压目录
--add-module=/root/nginx_upstream_check_module-master ##支持代理后端服务器健康状态检查
# make && make install
编写nginx控制脚本
[root@lb nginx-1.9.1]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile: /var/run/nginx/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-1.9.1/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile="/var/lock/nginx.lock"
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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
[root@lb nginx-1.9.1]# chmod +x /etc/init.d/nginx
[root@lb nginx-1.9.1]# chkconfig nginx on
[root@lb nginx-1.9.1]# chkconfig --list | grep nginx
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@lb nginx-1.9.1]# /etc/init.d/nginx start
[root@lb nginx-1.9.1]# netstat -tnlp | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29070/nginx
安装nginx.vim 使nginx配置文件支持语法检查
[root@lb nginx-1.9.1]# mkdir -p ~/.vim/syntax
[root@lb nginx-1.9.1]# mv /root/nginx.vim ~/.vim/syntax
[root@lb nginx-1.9.1]# cat >> ~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/* set ft=nginx
EOF
平滑升级nginx版本
[root@lb nginx-1.9.1]# /usr/local/nginx-1.9.1/sbin/nginx -V ##查看nginx编译参数
nginx version: nginx/1.9.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.2a 19 Mar 2015
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.9.1 --sbin-path=/usr/local/nginx-1.9.1/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=www --group=www --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.30 --with-zlib=/usr/local/src/zlib-1.2.7 --with-openssl=/usr/local/src/openssl-1.0.2a
[root@lb ~]# tar xvfz nginx-1.9.7.tar.gz -C /usr/local/src/
[root@lb ~]# cd /usr/local/src/nginx-1.9.7/src/ ##为nginx打补丁使其支持代理后端服务器健康检查
[root@lb src]# patch -p1 < /root/nginx_upstream_check_module-master/check_1.9.2+.patch
patching file http/modules/ngx_http_upstream_hash_module.c
patching file http/modules/ngx_http_upstream_ip_hash_module.c
patching file http/modules/ngx_http_upstream_least_conn_module.c
patching file http/ngx_http_upstream_round_robin.c
patching file http/ngx_http_upstream_round_robin.h
[root@lb src]# cd /usr/local/src/nginx-1.9.7/
[root@lb nginx-1.9.7]# ./configure \
--prefix=/usr/local/nginx-1.9.1 \
--sbin-path=/usr/local/nginx-1.9.1/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre=/usr/local/src/pcre-8.30 \
--with-zlib=/usr/local/src/zlib-1.2.7 \
--with-openssl=/usr/local/src/openssl-1.0.2a \
--add-module=/root/nginx_upstream_check_module-master
[root@lb nginx-1.9.7]# make ##执行完make之后就不用make install了
[root@lb nginx-1.9.7]# mv /usr/local/nginx-1.9.1/sbin/nginx /usr/local/nginx-1.9.1/sbin/nginx.old
[root@lb nginx-1.9.7]# cp objs/nginx /usr/local/nginx-1.9.1/sbin/nginx
[root@lb nginx-1.9.7]# /usr/local/nginx-1.9.1/sbin/nginx -t ##测试复制过来的文件是否生效
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb nginx-1.9.7]# /etc/init.d/nginx reload
[root@lb nginx-1.9.7]# /usr/local/nginx-1.9.1/sbin/nginx -V ##查看nginx版本
nginx version: nginx/1.9.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.2a 19 Mar 2015
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.9.1 --sbin-path=/usr/local/nginx-1.9.1/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=www --group=www --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.30 --with-zlib=/usr/local/src/zlib-1.2.7 --with-openssl=/usr/local/src/openssl-1.0.2a --add-module=/root/nginx_upstream_check_module-master