zoukankan      html  css  js  c++  java
  • 添加Nginx为系统服务(设置开机启动)

    在本节中,我们将创建一个脚本,将Nginx守护进程转换为实际的系统服务。 这有两个作用:守护程序可以使用标准命令控制,更重要的是,它可以在系统启动时自动启动,并在系统关闭时停止。

    System V scripts

    <br>
    大多数基于Linux的操作系统使用System-V风格的init守护进程。 换句话说,他们的启动过程由一个称为init的守护进程管理。
    这个守护进程基于运行级别的原理运行,它代表计算机的状态。 这里是一个表表示各种运行级别:

    运行级别 状态
    0 系统关闭
    1 单用户模式(救援模式)
    2 多用户模式,不支持NFS
    3 完全多用户模式
    4 未使用
    5 图形界面模式
    6 系统重新启动

    关于init脚本

    <br>
    init脚本(也称为服务启动脚本或甚至sysv脚本)是一个符合某种标准的shell脚本。 该脚本通过诸如开始,停止和其他等命令来控制守护进程应用程序。首先,当计算机启动时,init守护程序将使用start参数运行脚本。 另一种可能性是通过从shell手动执行脚本:

    1. [root@example.com ~]# service nginx start

    或者如果您的系统没有service命令:

    1. [root@example.com ~]# /etc/init.d/nginx start

    Nginx init脚本

    <br>
    /etc/init.d/nginx:
    基于Debian的发行版本

    1. #! /bin/sh
    2.  
    3. ### BEGIN INIT INFO
    4. # Provides:          nginx
    5. # Required-Start:    $all
    6. # Required-Stop:     $all
    7. # Default-Start:     2 3 4 5
    8. # Default-Stop:      0 1 6
    9. # Short-Description: starts the nginx web server
    10. # Description:       starts nginx using start-stop-daemon
    11. ### END INIT INFO
    12.  
    13. PATH=/opt/bin:/opt/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    14. DAEMON=/opt/sbin/nginx
    15. NAME=nginx
    16. DESC=nginx
    17.  
    18. test -x $DAEMON || exit 0
    19.  
    20. # Include nginx defaults if available
    21. if [ -f /etc/default/nginx ] ; then
    22.         . /etc/default/nginx
    23. fi
    24.  
    25. set -e
    26.  
    27. case "$1" in
    28.   start)
    29.         echo -n "Starting $DESC: "
    30.         start-stop-daemon --start --quiet --pidfile /var/run/nginx.pid
    31.                 --exec $DAEMON -- $DAEMON_OPTS
    32.         echo "$NAME."
    33.         ;;
    34.   stop)
    35.         echo -n "Stopping $DESC: "
    36.         start-stop-daemon --stop --quiet --pidfile /var/run/nginx.pid
    37.                 --exec $DAEMON
    38.         echo "$NAME."
    39.         ;;
    40.   restart|force-reload)
    41.         echo -n "Restarting $DESC: "
    42.         start-stop-daemon --stop --quiet --pidfile
    43.                 /var/run/nginx.pid --exec $DAEMON
    44.         sleep 1
    45.         start-stop-daemon --start --quiet --pidfile
    46.                 /var/run/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
    47.         echo "$NAME."
    48.         ;;
    49.   reload)
    50.       echo -n "Reloading $DESC configuration: "
    51.       start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/nginx.pid
    52.           --exec $DAEMON
    53.       echo "$NAME."
    54.       ;;
    55.   *)
    56.         N=/etc/init.d/$NAME
    57.         echo "Usage: $N {start|stop|restart|force-reload}" >&2
    58.         exit 1
    59.         ;;
    60. esac
    61.  
    62. exit 0

    基于Red Hat的发行版本

    1. #!/bin/sh
    2. #
    3. # nginx - this script starts and stops the nginx daemon
    4. #
    5. # chkconfig:   - 85 15
    6. # description:  NGINX is an HTTP(S) server, HTTP(S) reverse
    7. #               proxy and IMAP/POP3 proxy server
    8. # processname: nginx
    9. # config:      /etc/nginx/nginx.conf
    10. # config:      /etc/sysconfig/nginx
    11. # pidfile:     /var/run/nginx.pid
    12.  
    13. # Source function library.
    14. . /etc/rc.d/init.d/functions
    15.  
    16. # Source networking configuration.
    17. . /etc/sysconfig/network
    18.  
    19. # Check that networking is up.
    20. [ "$NETWORKING" = "no" ] && exit 0
    21.  
    22. nginx="/usr/sbin/nginx"
    23. prog=$(basename $nginx)
    24.  
    25. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    26.  
    27. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    28.  
    29. lockfile=/var/lock/subsys/nginx
    30.  
    31. make_dirs() {
    32.    # make required directories
    33.    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=([^ ]*).*/1/g' -`
    34.    if [ -z "`grep $user /etc/passwd`" ]; then
    35.        useradd -M -s /bin/nologin $user
    36.    fi
    37.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
    38.    for opt in $options; do
    39.        if [ `echo $opt | grep '.*-temp-path'` ]; then
    40.            value=`echo $opt | cut -d "=" -f 2`
    41.            if [ ! -d "$value" ]; then
    42.                # echo "creating" $value
    43.                mkdir -p $value && chown -R $user $value
    44.            fi
    45.        fi
    46.    done
    47. }
    48.  
    49. start() {
    50.     [ -x $nginx ] || exit 5
    51.     [ -f $NGINX_CONF_FILE ] || exit 6
    52.     make_dirs
    53.     echo -n $"Starting $prog: "
    54.     daemon $nginx -c $NGINX_CONF_FILE
    55.     retval=$?
    56.     echo
    57.     [ $retval -eq 0 ] && touch $lockfile
    58.     return $retval
    59. }
    60.  
    61. stop() {
    62.     echo -n $"Stopping $prog: "
    63.     killproc $prog -QUIT
    64.     retval=$?
    65.     echo
    66.     [ $retval -eq 0 ] && rm -f $lockfile
    67.     return $retval
    68. }
    69.  
    70. restart() {
    71.     configtest || return $?
    72.     stop
    73.     sleep 1
    74.     start
    75. }
    76.  
    77. reload() {
    78.     configtest || return $?
    79.     echo -n $"Reloading $prog: "
    80.     killproc $nginx -HUP
    81.     RETVAL=$?
    82.     echo
    83. }
    84.  
    85. force_reload() {
    86.     restart
    87. }
    88.  
    89. configtest() {
    90.   $nginx -t -c $NGINX_CONF_FILE
    91. }
    92.  
    93. rh_status() {
    94.     status $prog
    95. }
    96.  
    97. rh_status_q() {
    98.     rh_status >/dev/null 2>&1
    99. }
    100.  
    101. case "$1" in
    102.     start)
    103.         rh_status_q && exit 0
    104.         $1
    105.         ;;
    106.     stop)
    107.         rh_status_q || exit 0
    108.         $1
    109.         ;;
    110.     restart|configtest)
    111.         $1
    112.         ;;
    113.     reload)
    114.         rh_status_q || exit 7
    115.         $1
    116.         ;;
    117.     force-reload)
    118.         force_reload
    119.         ;;
    120.     status)
    121.         rh_status
    122.         ;;
    123.     condrestart|try-restart)
    124.         rh_status_q || exit 0
    125.             ;;
    126.     *)
    127.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
    128.         exit 2
    129. esac

    注意修改脚本中的路径。

    安装Nginx init脚本

    <br>
    添加执行权限:

    1. [root@example.com ~]# chmod +x /etc/init.d/nginx

    Debian-based发行版本:

    1. [root@example.com ~]# update-rc.d -f nginx defaults

    Red Hat–based发行版本:

      1. [root@example.com ~]# chkconfig nginx on
  • 相关阅读:
    TextBox 只有下划线
    can't find web control library(web控件库)
    DropDownListSalesAC”有一个无效 SelectedValue,因为它不在项目列表中。
    IDE、SATA、SCSI、SAS、FC、SSD 硬盘类型
    如何打印1px表格
    CSS控制打印 分页
    Virtual Server could not open its emulated Ethernet switch driver. To fix this problem, reenable the Virtual Server Emulated Et
    Xml中SelectSingleNode方法中的xpath用法
    热带水果莫入冰箱?水果存放冰箱大法
    探索Asp.net的Postback机制
  • 原文地址:https://www.cnblogs.com/felixzh/p/6283809.html
Copyright © 2011-2022 走看看