Linux下Nginx开关服务,正常方式是这样({nginx}-为Nginx安装路径):

  1. {nginx}/sbin/nginx #启动
  2. {nginx}/sbin/nginx -s stop #停止

这种方式带来很多不便,因此介绍一种快捷启动关闭Nginx的方式。

在/etc/init.d下创建nginx启动脚本文件:

  1. vim /etc/init.d/nginx

i进入编辑状态,粘贴以下代码后保存:
将/usr/local/nginx/替换为自身nginx的安装路径。

  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. # Source function library.
  13. . /etc/rc.d/init.d/functions
  14. # Source networking configuration.
  15. . /etc/sysconfig/network
  16. # Check that networking is up.
  17. [ "$NETWORKING" = "no" ] && exit 0
  18. nginx="/usr/local/nginx/sbin/nginx"
  19. prog=$(basename $nginx)
  20. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
  21. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
  22. lockfile=/var/lock/subsys/nginx
  23. start() {
  24. [ -x $nginx ] || exit 5
  25. [ -f $NGINX_CONF_FILE ] || exit 6
  26. echo -n $"Starting $prog: "
  27. daemon $nginx -c $NGINX_CONF_FILE
  28. retval=$?
  29. echo
  30. [ $retval -eq 0 ] && touch $lockfile
  31. return $retval
  32. }
  33. stop() {
  34. echo -n $"Stopping $prog: "
  35. killproc $prog -QUIT
  36. retval=$?
  37. echo
  38. [ $retval -eq 0 ] && rm -f $lockfile
  39. return $retval
  40. killall -9 nginx
  41. }
  42. restart() {
  43. configtest || return $?
  44. stop
  45. sleep 1
  46. start
  47. }
  48. reload() {
  49. configtest || return $?
  50. echo -n $"Reloading $prog: "
  51. killproc $nginx -HUP
  52. RETVAL=$?
  53. echo
  54. }
  55. force_reload() {
  56. restart
  57. }
  58. configtest() {
  59. $nginx -t -c $NGINX_CONF_FILE
  60. }
  61. rh_status() {
  62. status $prog
  63. }
  64. rh_status_q() {
  65. rh_status >/dev/null 2>&1
  66. }
  67. case "$1" in
  68. start)
  69. rh_status_q && exit 0
  70. $1
  71. ;;
  72. stop)
  73. rh_status_q || exit 0
  74. $1
  75. ;;
  76. restart|configtest)
  77. $1
  78. ;;
  79. reload)
  80. rh_status_q || exit 7
  81. $1
  82. ;;
  83. force-reload)
  84. force_reload
  85. ;;
  86. status)
  87. rh_status
  88. ;;
  89. condrestart|try-restart)
  90. rh_status_q || exit 0
  91. ;;
  92. *)
  93. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  94. exit 2
  95. esac

修改脚本权限:

  1. chmod 755 nginx

将脚本文件加入到chkconfig中:

  1. chkconfig --add nginx

设置nginx开机在3和5级别自动启动:

  1. chkconfig --level 35 nginx on

创建软连接:

  1. cd /usr/bin
  2. ln -s /etc/init.d/nginx

愉快的玩耍吧!
这里边的命令都可以执行:
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
以下是示例:

    1. nginx start
    2. nginx stop
    3. nginx restart

最新文章

  1. tornado学习笔记11 Web应用中模板(Template)使用应用实践
  2. 关于 Xcode bitcode 错误
  3. 圆角button
  4. virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web
  5. python练习程序(c100经典例11)
  6. 解决方案:Error:Execution failed for task ':app:compileDebugAidl'. > aidl is missing
  7. Android开发:TextView真正可控、不需要焦点的水平滚动--这才是真正的跑马灯
  8. 发现一个时隐时现的bug!
  9. 内存测试工具memtester
  10. NSArray数组的学习总结
  11. j2ee基础(1)servlet的生命周期
  12. flask 状态保持session和上下文session的区别
  13. geohash编码算法在LBS中的应用
  14. 详解scss的继承、占位符和混合宏
  15. tomcat源码 Connector
  16. iOS 直播推流 - 搭建基于RTMP的本地Nginx服务器
  17. Long.parseLong(String s) 其中s必须是数字形式的字符串,才能运用该函数转化为长整型。
  18. Node.js中流程控制
  19. SQLServer客户端连接工具(支持2000,20005,2008)
  20. springboot定时任务,去掉指定日期

热门文章

  1. java中间缓存变量机制
  2. java.lang.Comparable 接口 详解
  3. python学习笔记(1)--python特点
  4. 如何在DataTemplate中绑定RadioButton的Checked事件
  5. shiro使用ajax登陆实现,success但页面无法跳转的问题
  6. Lodop如何打印直线
  7. 51nod2383
  8. redis知识汇总
  9. 【数学建模】day09-聚类分析
  10. 了解AutoCAD对象层次结构 —— 5 —— 块表