Ubuntu14.04配置nginx开机自启动项

前端之家收集整理的这篇文章主要介绍了Ubuntu14.04配置nginx开机自启动项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 创建/etc/init.d/Nginx文件

  1. #! /bin/sh
  2. # Author: rui ding
  3. # Modified: Geoffrey Grosenbach http://www.linuxidc.com
  4. # Modified: Clement NEDELCU
  5. # Reproduced with express authorization from its contributors
  6. set -e
  7. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  8. DESC="Nginx daemon"
  9. NAME=Nginx
  10. DAEMON=/usr/local/Nginx/sbin/$NAME
  11. SCRIPTNAME=/etc/init.d/$NAME
  12.  
  13.  
  14. # If the daemon file is not found,terminate the script.
  15. test -x $DAEMON || exit 0
  16.  
  17. d_start() {
  18. $DAEMON || echo -n " already running"
  19. }
  20.  
  21. d_stop() {
  22. $DAEMON s quit || echo -n " not running"
  23. }
  24.  
  25. d_reload() {
  26. $DAEMON s reload || echo -n " could not reload"
  27. }
  28.  
  29. case "$1" in
  30. start)
  31. echo -n "Starting $DESC: $NAME"
  32. d_start
  33. echo "."
  34. ;;
  35. stop)
  36. echo -n "Stopping $DESC: $NAME"
  37. d_stop
  38. echo "."
  39. ;;
  40. reload)
  41. echo -n "Reloading $DESC configuration..."
  42. d_reload
  43. echo "reloaded."
  44. ;;
  45. restart)
  46. echo -n "Restarting $DESC: $NAME"
  47. d_stop
  48. # Sleep for two seconds before starting again,this should give the
  49. # Nginx daemon some time to perform a graceful stop.
  50. sleep 2
  51. d_start
  52. echo "."
  53. ;;
  54. *)
  55. echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
  56. exit 3
  57. ;;
  58. esac
  59. exit 0

增加可执行权限

chmod a+x /etc/init.d/Nginx


2.利用sysv-rc-conf命令将其在对应rc?.d目录下建立一个软链接

sysv-rc-conf Nginx on

该命令会在rc2.d ~ rc5.d目录下都建立了一个Nginx的软链接

这里需要特别说明的是,Ubuntu系统下没有RedHat系统下的chkconfig命令。
但Ubuntu有一个类似的命令:sysv-rc-conf

通过apt-get命令完成sysv-rc-conf软件的安装。


学习资料

Linux系统的运行级别有7个,分别对应的:

  • 0: 关机
  • 1: 单用户(维护)
  • 2~5: 多用户
  • 6: 重启

可以通过runlevel命令来查看当前系统的运行等级:

      • 1
      • 2
        • 1
        • 2
        wds@wds-VirtualBox:~$ runlevel
      • N 2

      其中第一个表示上一次的运行等级,N表示没有上一次运行等级的记录;第二个表示当前运行等级,这里为2.

      linux中所有开机自启动项目运行脚本都放在/etc/init.d/目录下;同时在/etc/目录下有rc?.d目录,分别对应了7中不同的运行级别:

      1. 1
      2. 2
      3. 3
      4. 4
      5. 5
      6. 6
      7. 7
      8. 8
      9. 9
      10. 10
          • 1
          • 2
          • 3
          • 4
          • 5
          • 6
          • 7
          • 8
          • 9
          • 10
          wds@wds-VirtualBox:/$ ls /etc/ | grep ^rc
        • rc0.d
        • rc1.d
        • rc2.d
        • rc3.d
        • rc4.d
        • rc5.d
        • rc6.d
        • rc.local
        • rcS.d

        这里rc2.d目录就对应了我们系统当前的运行等级。

        其中里面的一些文件其实都是/etc/init.d/目录下文件的软链接:

        1. 1
        2. 2
        3. 3
        4. 4
        5. 5
        6. 6
        7. 7
        8. 8
        9. 9
        10. 10
        11. 11
        12. 12
        13. 13
        14. 14
            • 1
            • 2
            • 3
            • 4
            • 5
            • 6
            • 7
            • 8
            • 9
            • 10
            • 11
            • 12
            • 13
            • 14
            wds@wds-VirtualBox:/etc/rc2.d$ ls -ltr
          • total 4
          • -rw-r--r-- 1 root root 677 3 13 2014 README
          • lrwxrwxrwx 1 root root 18 12 8 19:49 S99rc.local -> ../init.d/rclocal
          • lrwxrwxrwx 49 S99ondemand .d/ondemand
          • lrwxrwxrwx 49 S70pppd-dns .d/pppd-dns
          • lrwxrwxrwx 19 49 S70dns-clean .d/dns-clean
          • lrwxrwxrwx 15 49 S50saned .d/saned
          • lrwxrwxrwx 27 49 S20speech-dispatcher .d/speech-dispatcher
          • lrwxrwxrwx 49 S20rsync .d/rsync
          • lrwxrwxrwx 20 49 S20kerneloops .d/kerneloops
          • lrwxrwxrwx 21 9 17:25 S99grub-common .d/grub-common
          • lrwxrwxrwx 45 S20Nginx .d/Nginx
          • lrwxrwxrwx 17 47 S20PHP-fpm .d/PHP-fpm

          整个开机自启动项的流程如下:

          1. 开机后,系统获得当前的运行等级(例如这里的等级为2);
          2. 运行/etc/rc?.d目录下的所有可执行文件(这里运行/etc/rc2.d/目录下所有的软链接。这些软链接的源文件都保存在/etc/init.d/目录下)。

          因此我们只需要在/etc/init.d/完成启动Nginx进程的脚本,然后在/etc/rc2.d/做对应的软链接即可。

          猜你在找的Ubuntu相关文章