经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
Nginx设置成服务并开机自动启动的配置
来源:jb51  时间:2022/1/24 15:45:20  对本文有异议

在/etc/init.d下创建文件nginx

  1. [root@localhost ~]# vim /etc/init.d/nginx

其内容参考nginx官方文档

需要注意的配置:

  1. nginx=”/usr/local/nginx/sbin/nginx //修改成nginx执行程序的路径。
  2.  
  3. NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf //修改成nginx.conf文件的路径。

保存后设置文件的执行权限

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

至此就可以通过下面指令控制启动停止

  1. /etc/init.d/nginx start
  2. /etc/init.d/nginx stop

上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便。

先将nginx服务加入chkconfig管理列表:

  1. [root@localhost ~]# chkconfig --add /etc/init.d/nginx

加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

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

最后设置开机自动启动

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

引用:https://blog.csdn.net/buyueliuying/article/details/75353863

到此这篇关于Nginx设置成服务并开机自动启动的文章就介绍到这了,更多相关Nginx开机自动启动内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号