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

Nginx快捷启动配置的更多相关文章

  1. CentOS下nginx+php的配置及nginx开机启动配置

    关闭防火墙 (不然外链接是访问不了 apache) service iptables stop 关闭安全系统 SELinux( 不然报403 访问页面错误 ) 1.Nginx安装主要在于配置文件的修改 ...

  2. windows nginx 快捷启动关闭批处理脚本

    :: 关闭回显,即执行本脚本时不显示执行路径和命令,直接显示结果 @echo off rem @author luwuer color f8 set NGINX_DIR=D:\nginx-1.12.2 ...

  3. nginx入门与实战 安装 启动 配置nginx Nginx状态信息(status)配置 正向代理 反向代理 nginx语法之location详解

    nginx入门与实战 网站服务 想必我们大多数人都是通过访问网站而开始接触互联网的吧.我们平时访问的网站服务 就是 Web 网络服务,一般是指允许用户通过浏览器访问到互联网中各种资源的服务. Web ...

  4. Nginx使用(配置开机启动)

    环境: 系统:CentOS 6.5 Final 安装目录:/usr/local/nginx Nginx开机自启: ①编写shell实现控制 vi /etc/init.d/nginx 添加内容: #!/ ...

  5. 04-Uwsgi配置启动Nginx虚拟主机配置

    一.虚拟环境的安装 1.安装虚拟环境 sudo pip3 install virtualenv 2.安装virtualenvwrapper sudo pip3 install virtualenvwr ...

  6. 【Linux】宝塔上 GitLab数据迁移修改配置后nginx无法启动

    背景: 服务器A 向 服务器B 迁移数据,修改配置重启.发现gitlab的 nginx 无法启动. 查找原因 gitlab-ctl tail 错误信息: 网上查了查,似乎是宝塔的问题, 原因应该是为了 ...

  7. 安装nginx并安全地配置和启动

    一.安装nginx >>参考文章<< 安装教程,看代码&注释 # .sh # 如果centos服务器是最低安装,则先安装weget yum install -y wge ...

  8. nginx学习与配置-安装与启动关闭管理

    nginx服务器的安装 安装准备: nginx依赖于pcre库,要先安装pcre yum install pcre pcre-devel cd /usr/local/src/ wget wget ht ...

  9. Nginx使用默认配置启动异常处理

    Ps1:错误问题:nginx: [error] OpenEvent("Global\ngx_reload_5988") failed (2: The system cannot f ...

随机推荐

  1. java学习之—链表(2)

    /** * 双端链表操作 * Create by Administrator * 2018/6/14 0014 * 下午 2:05 **/ class Link1 { public long dDat ...

  2. Prism框架研究(二)

    首先在介绍本节内容之前,首先来看看官方文档来如何描述Prism 应用的初始化吧!A Prism application requires registration and configuration ...

  3. mysql从入门到精通

    解决MySQL Got a packet bigger than 'max_allowed_packet' bytes 问题在一次性向mysql插入的数据过大,可在my.ini中修改max_allow ...

  4. vue-cli: preset预设

    preset:预设 vue create demo01 过程中,会保存预设,自动保存着 .vuerc 文件中 .vuerc 文件的位置:C:\Users\Administrator C:\Users\ ...

  5. SQL Server 数据库try catch 存储过程

    SQL Server 在生产环境中这样写存储过程的坑都避免了吗? 原文链接: http://www.cnblogs.com/chenmh/p/7856777.html 概述 最近因为业务的需求写了一段 ...

  6. How to install macOS Sierra on Skylake

    create usb installer sudo /Applications/Install\ macOS\ Sierra.app/contents/resources/createinstallm ...

  7. 【C/C++】实现牛顿迭代

    #include<bits/stdc++.h> using namespace std; /*(x+2)^2 +1 -2(x-1)^2+7 */ double f(double x){ - ...

  8. JSON 解析 (二)—— Jackson的使用

    Jackson是基于Java语言的一种JSON和Java对象的数据处理工具.功能上简单易用,性能上根据目前主流转换工具比较,Jackson相对比较高效. <dependency> < ...

  9. Go语言类型(布尔、整型、数组、切片、map等)

    1.基本类型 布尔类型:bool 注意:布尔类型不能接受其他类型的赋值,不支持自动或强制的类型转换. 整型:int8.byte(uint8).int16.int.uint.uintptr int.ui ...

  10. kubernetes 构架