/ 中存储网

Linux系统如何配置Nginx服务自动启动

2013-09-01 14:28:31 来源:kejihao

Nginx作为基本的服务,需要在Linux启动时实现自启动。通过编写脚本注册为服务即可,并且由于Nginx与keepalived实现了HA,所以需要关注两者自启动的顺序。

1.脚本

shell>vi /etc/init.d/nginx

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig: 2345 15 85

# 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

# 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

# Attention! Where nginx exists

nginx="/usr/local/nginx/sbin/nginx"

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

prog=$(basename ${nginx})

lockfile=/var/lock/subsys/nginx

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

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

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart)

restart

;;

reload)

reload

;;

status)

status $prog

RETVAL=$?

;;

*)

echo $"Usage: $0 {start|stop|status|restart|reload}"

exit 2

esac

exit $RETVAL

在脚本中,需要关注的有:

chkconfig: 2345 15 85 是指定了chkconfig将其加入到自启动服务时的runlevel,启动顺序和关闭顺序。这里在2345下on,启动顺序为15,85为关闭顺序。

这里由于搭建了keepalived+Nginx做高可用,keepalived的自启动顺序为21,而keepalived会检测Nginx是否活动,即keepalived依赖于Nginx,所以启动顺序应该为先Nginx,后keepalived,通过chkconfig的指定顺序实现。

2.设置可执行

shell>chmod a+x /etc/init.d/nginx

3.配置自启动

shell>chkconfig --level 2345 nginx on

4.检查与keepalived的启动顺序

shell>ls -lrtR /etc/rc.d | grep -E 'keepalived|nginx'

-rwxr-xr-x. 1 root root 1288 Mar 28 20:18 keepalived

-rwxr-xr-x. 1 root root 1649 May 23 13:48 nginx

lrwxrwxrwx. 1 root root 20 May 22 15:40 S21keepalived -> ../init.d/keepalived

lrwxrwxrwx. 1 root root 15 May 23 13:53 S15nginx -> ../init.d/nginx

lrwxrwxrwx. 1 root root 20 May 22 15:40 S21keepalived -> ../init.d/keepalived

lrwxrwxrwx. 1 root root 15 May 23 13:53 S15nginx -> ../init.d/nginx

lrwxrwxrwx. 1 root root 20 May 22 15:40 S21keepalived -> ../init.d/keepalived

lrwxrwxrwx. 1 root root 15 May 23 13:53 S15nginx -> ../init.d/nginx

lrwxrwxrwx. 1 root root 20 May 22 15:40 S21keepalived -> ../init.d/keepalived

lrwxrwxrwx. 1 root root 15 May 23 13:53 S15nginx -> ../init.d/nginx

均是nginx先启动,OK

5.基本功能检查

shell>service nginx status

nginx (pid 13286 13285 13284 13283 13282 13281 13280 13279 13277 13276 13275 13274 13273) 正在运行...

shell>service nginx stop

停止 nginx: [确定]

shell>service nginx status

nginx 已停

shell>service nginx restart

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

停止 nginx: [失败]

正在启动 nginx: [确定]

shell>service nginx status

nginx (pid 13391 13390 13389 13388 13387 13386 13385 13384 13382 13381 13380 13379 13378) 正在运行...

shell>service nginx reload

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新载入 nginx: [确定]

shell>service nginx status

nginx (pid 13435 13434 13433 13432 13431 13430 13429 13428 13427 13426 13425 13424 13378) 正在运行...

检查无误,OK