编译安装nginx

为何要编译安装呢,nginx的模块都是静态编译,为了方便我们方便的添加module

我们可以先安装一个 EPEL repo,可以到这个站点来检查 http://dl.fedoraproject.org/pub/epel/

可以用以下的命令安装EPEL repo

rpm -ivh http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm

先安装nginx的required package

yum install gc gcc gcc-c++ pcre-devel zlib-devel make wget openssl-devel libxml2-devel libxslt-devel gd-devel perl-ExtUtils-Embed GeoIP-devel gperftools gperftools-devel libatomic_ops-devel

新建nginx用户,赋予nobody的shell,回头我们会在Nginx Web Server中用到,页可以不新建,在下面的一个nginx初始化脚本中,启动服务会新建这个用户,

useradd nginx
usermod -s /sbin/nologin nginx

下载nginx source,然后进入安装文件

./configure --help

查看帮助

然后

 –prefix=/usr/share/nginx –sbin-path=/usr/sbin/nginx –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/tmp/client_body –http-proxy-temp-path=/var/lib/nginx/tmp/proxy –http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi –http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi –http-scgi-temp-path=/var/lib/nginx/tmp/scgi –pid-path=/var/run/nginx.pid –lock-path=/var/lock/subsys/nginx –user=nginx –group=nginx –with-file-aio –with-ipv6 –with-http_ssl_module –with-http_realip_module –with-http_addition_module –with-http_auth_request_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_mp4_module –with-http_gunzip_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-mail –with-mail_ssl_module –with-pcre –with-google_perftools_module –with-debug –add-module=/root/headers-more-nginx-module –with-ld-opt=’ -Wl,-E’

其中headers-more-nginx-module是一个第三方模块,需要下载

最后

make && make install

启动nginx

/usr/sbin/nginx -c /etc/nginx/nginx.conf

查看nginx进程

ps -ef|grep nginx

停止nginx

kill -9 PID-Of-Nginx

例如

[root@localhost ~]# /usr/sbin/nginx -c /etc/nginx/nginx.conf [root@localhost ~]# [root@localhost ~]# ps -ef|grep nginx
root 5071 1 0 07:13 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 5072 5071 0 07:13 ? 00:00:00 nginx: worker process
root 5074 1879 0 07:13 pts/0 00:00:00 grep --color=auto nginx [root@localhost ~]# [root@localhost ~]# [root@localhost ~]# kill -9 5071 5072

Nginx对进程的控制能力很强

QUIT 表示处理完请求后,关闭进程

HUP 表示重新加载配置,也是就是关闭原有进程,并开启新的进程,此操作不会中断用户的访问请求,因此可以通过此信号平滑重启Nginx

USR1 用于Nginx的日志切换,也就是从新打开一个日志文件,例如每天每天要生成一个新的日志,可以使用这个信号来控制。

USR2 用户平滑升级程序,比如要升级Nginx到新版本,可以在终端用户请求的情况下,升级到新版本。

例如要不间断服务的重新启动nginx可以这样做

kill -HUP `cat /var/run/nginx.pid`

编译安装nginx结束后,可以将下面的init script保存到/etc/init.d/nginx 中,然后 chmod 755 /etc/init.d/nginx,这个脚本会自创建nginx的运行用户,例如本例中是user为nginx,具体逻辑看代码,作为管理nginx常驻进程的脚本,例如nginx 的随机启动,可也设置 chkconfig nginx on 或者 systemct1 enable 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:      /etc/sysconfig/nginx
# pidfile:     /var/run/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/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/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() {
    [ -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

当需要将正运行的Nginx升级,可以在不中断服务的情况下进行,具体步骤如下:

1、使用新的可执行程序替换旧的可执行程序。下载新的Nginx,重新编译到旧版本的安装路径中。重编译之前,先备份一下旧的可执行文件。

 

2、执行以下指令,他将存储有旧版本主进程ID的文件重命名为.oldbin:

kill -USR2 旧版本的Nginx主进程号

一般情况下是这样的:kill -USR2 `cat /usr/local/nginx/nginx.pid`

 可以用 ls /usr/local/nginx/logs来查看是否改名

3、执行新版本的Nginx可执行程序。

ulimit -SHn 65535

/usr/local/nginx/sbin/nginx

 

4、此时新旧版本的Nginx会同时运行,共同处理请求。要逐步停止旧版本的Nginx,必须发送WINCH信号给旧的主进程。然后,他的工作进程将从容关闭。

kill -WINCH 旧版本的Nginx主进程号

 

5、一段时间后,旧的工作进程处理完了所有的请求后退出,仅由新的进程来处理输入请求了。可用下面的命令查看:

ps -ef | grep nginx

 

6、现在可以决定使用新版本还是恢复到旧版本:

kill -HUP 旧的主进程号 :Nginx在不重载配置文件的情况下启动他的工作进程

kill -QUIT 新的主进程号  :从容关闭其工作进程

kill -TERM 新的主进程号 :强制退出

kill 新的主进程号或旧的主进程号:如果因为某些原因新的工作进程不能退出,则向其发送kill信号

 

新的主进程退出后,旧的主进程会移除.oldbin后缀,恢复为他的.pid文件,这样,一切就都恢复为升级之前了。
如果尝试升级成功,而自己又希望保留新版本时,可发送QUIT信号给旧的主进程,使其退出而只留下新的进程运行:kill -QUIT 旧主进程号

Avatar photo

About Blackford

这是个最好的时代,这是个最坏的时代,这是个充满希望的春天,这是个令人绝望的冬天,我们前面什么都有,我们前面什么都没有。梦想,让我们一次次的走远,又一次次的回头,一个关于人生的梦想还在不断奔跑,带着喜悦和疼痛,不过一切才刚刚开始,并且直到今天也远远没有结束
This entry was posted in 架构运维. Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用*标注