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
在脚本中,需要关注的有: 2.设置可执行 shell>chmod a+x /etc/init.d/nginx3.配置自启动 shell>chkconfig --level 2345 nginx on4.检查与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 statusnginx (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 (责任编辑:IT) |