编译安装完nginx后,默认情况下,你是不能像用命令行apt-get那样生成启动脚本的,也就是放在/etc/init.d上的脚本,要自己创建。

1. 复制一个启动脚本

创建一个文件/etc/init.d/nginx

内容如下:

  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: nginx
  4. # Required-Start: $local_fs $remote_fs $network $syslog $named
  5. # Required-Stop: $local_fs $remote_fs $network $syslog $named
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: starts the nginx web server
  9. # Description: starts nginx using start-stop-daemon
  10. ### END INIT INFO
  11. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  12. DAEMON=/usr/sbin/nginx
  13. NAME=nginx
  14. DESC=nginx
  15. # Include nginx defaults if available
  16. if [ -r /etc/default/nginx ]; then
  17. . /etc/default/nginx
  18. fi
  19. test -x $DAEMON || exit 0
  20. . /lib/init/vars.sh
  21. . /lib/lsb/init-functions
  22. # Try to extract nginx pidfile
  23. PID=$(cat /etc/nginx/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
  24. if [ -z "$PID" ]
  25. then
  26. PID=/run/nginx.pid
  27. fi
  28. # Check if the ULIMIT is set in /etc/default/nginx
  29. if [ -n "$ULIMIT" ]; then
  30. # Set the ulimits
  31. ulimit $ULIMIT
  32. fi
  33. #
  34. # Function that starts the daemon/service
  35. #
  36. do_start()
  37. {
  38. # Return
  39. # 0 if daemon has been started
  40. # 1 if daemon was already running
  41. # 2 if daemon could not be started
  42. start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
  43. || return 1
  44. start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
  45. $DAEMON_OPTS 2>/dev/null \
  46. || return 2
  47. }
  48. test_nginx_config() {
  49. $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
  50. }
  51. #
  52. # Function that stops the daemon/service
  53. #
  54. do_stop()
  55. {
  56. # Return
  57. # 0 if daemon has been stopped
  58. # 1 if daemon was already stopped
  59. # 2 if daemon could not be stopped
  60. # other if a failure occurred
  61. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PID --name $NAME
  62. RETVAL="$?"
  63. sleep 1
  64. return "$RETVAL"
  65. }
  66. #
  67. # Function that sends a SIGHUP to the daemon/service
  68. #
  69. do_reload() {
  70. start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
  71. return 0
  72. }
  73. #
  74. # Rotate log files
  75. #
  76. do_rotate() {
  77. start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
  78. return 0
  79. }
  80. #
  81. # Online upgrade nginx executable
  82. #
  83. # "Upgrading Executable on the Fly"
  84. # http://nginx.org/en/docs/control.html
  85. #
  86. do_upgrade() {
  87. # Return
  88. # 0 if nginx has been successfully upgraded
  89. # 1 if nginx is not running
  90. # 2 if the pid files were not created on time
  91. # 3 if the old master could not be killed
  92. if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
  93. # Wait for both old and new master to write their pid file
  94. while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
  95. cnt=`expr $cnt + 1`
  96. if [ $cnt -gt 10 ]; then
  97. return 2
  98. fi
  99. sleep 1
  100. done
  101. # Everything is ready, gracefully stop the old master
  102. if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
  103. return 0
  104. else
  105. return 3
  106. fi
  107. else
  108. return 1
  109. fi
  110. }
  111. case "$1" in
  112. start)
  113. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  114. do_start
  115. case "$?" in
  116. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  117. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  118. esac
  119. ;;
  120. stop)
  121. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  122. do_stop
  123. case "$?" in
  124. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  125. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  126. esac
  127. ;;
  128. restart)
  129. log_daemon_msg "Restarting $DESC" "$NAME"
  130. # Check configuration before stopping nginx
  131. if ! test_nginx_config; then
  132. log_end_msg 1 # Configuration error
  133. exit 0
  134. fi
  135. do_stop
  136. case "$?" in
  137. 0|1)
  138. do_start
  139. case "$?" in
  140. 0) log_end_msg 0 ;;
  141. 1) log_end_msg 1 ;; # Old process is still running
  142. *) log_end_msg 1 ;; # Failed to start
  143. esac
  144. ;;
  145. *)
  146. # Failed to stop
  147. log_end_msg 1
  148. ;;
  149. esac
  150. ;;
  151. reload|force-reload)
  152. log_daemon_msg "Reloading $DESC configuration" "$NAME"
  153. # Check configuration before reload nginx
  154. #
  155. # This is not entirely correct since the on-disk nginx binary
  156. # may differ from the in-memory one, but that's not common.
  157. # We prefer to check the configuration and return an error
  158. # to the administrator.
  159. if ! test_nginx_config; then
  160. log_end_msg 1 # Configuration error
  161. exit 0
  162. fi
  163. do_reload
  164. log_end_msg $?
  165. ;;
  166. configtest|testconfig)
  167. log_daemon_msg "Testing $DESC configuration"
  168. test_nginx_config
  169. log_end_msg $?
  170. ;;
  171. status)
  172. status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
  173. ;;
  174. upgrade)
  175. log_daemon_msg "Upgrading binary" "$NAME"
  176. do_upgrade
  177. log_end_msg 0
  178. ;;
  179. rotate)
  180. log_daemon_msg "Re-opening $DESC log files" "$NAME"
  181. do_rotate
  182. log_end_msg $?
  183. ;;
  184. *)
  185. echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2
  186. exit 3
  187. ;;
  188. esac
  189. :

如果有发现路径不一样的地方改过来就好了。

然后执行下面的指令。

  1. $ sudo chmod +x /etc/init.d/nginx

可以按照下面的方法使用这个脚本。

  1. $ sudo /etc/init.d/nginx start
  2. $ sudo /etc/init.d/nginx stop
  3. $ sudo /etc/init.d/nginx restart
  4. $ sudo /etc/init.d/nginx reload

or

  1. $ sudo service nginx start
  2. $ sudo service nginx stop
  3. $ sudo service nginx restart
  4. $ sudo service nginx reload

2. nginx-init-ubuntu

nginx-init-ubuntu这个库提供了nginx的启动脚本。

  1. $ sudo wget https://raw.githubusercontent.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
  2. $ sudo chmod +x /etc/init.d/nginx

同样道理,根据编译的参数,可以自行更改变量。

  1. NGINXPATH=${NGINXPATH:-/usr} # root path where installed
  2. DAEMON=${DAEMON:-$NGINXPATH/sbin/nginx} # path to daemon binary
  3. NGINX_CONF_FILE=${NGINX_CONF_FILE:-/etc/nginx/nginx.conf} # config file path
  4. PIDNAME=${PIDNAME:-"nginx"} # lets you do $PS-slave
  5. PIDFILE=${PIDFILE:-$PIDNAME.pid} # pid file
  6. PIDSPATH=${PIDSPATH:-/var/run}

3. 设置开机启动

如果是ubuntu系统,使用sysv-rc-conf这个工具来配置开机启动,这个工具类似于chkconfig。

安装。

  1. $ sudo apt-get install sysv-rc-conf

配置nginx在开机的时候启动。

  1. sudo sysv-rc-conf nginx on

查看nginx的运行等级的情况。

  1. $ sudo sysv-rc-conf --list nginx

完结。