centos 7 设置 nginx-1.11.10 开机启动

centos安装nginx通过编译安装的方式安装好后,把启动重启后每次都需要去 /usr/local/nginx/sbin/nginx 启动服务,很不方便,所以centos7设置nginx-1.11.10开机启动服务是很有用处的。大致流程是通过创建开机启动脚本,把nginx服务加入开机启动。

centos 7 编译安装 nginx-1.11.10的详细步骤

创建开机启动命令脚本文件:

bash
  1. vi /etc/init.d/nginx

在这个nginx文件中插入一下启动脚本代码,启动脚本代码来源网络复制,实测有效

bash
  1. #! /bin/bash
  2. # chkconfig: - 85 15
  3. PATH=/usr/local/nginx
  4. DESC="nginx daemon"
  5. NAME=nginx
  6. DAEMON=$PATH/sbin/$NAME
  7. CONFIGFILE=$PATH/conf/$NAME.conf
  8. PIDFILE=$PATH/logs/$NAME.pid
  9. scriptNAME=/etc/init.d/$NAME
  10. set -e
  11. [ -"$DAEMON" ] || exit 0
  12. do_start() {
  13. $DAEMON -c $CONFIGFILE || echo -"nginx already running"
  14. }
  15. do_stop() {
  16. $DAEMON -s stop || echo -"nginx not running"
  17. }
  18. do_reload() {
  19. $DAEMON -s reload || echo -"nginx can't reload"
  20. }
  21. case "$1" in
  22. start)
  23. echo -"Starting $DESC: $NAME"
  24. do_start
  25. echo "."
  26. ;;
  27. stop)
  28. echo -"Stopping $DESC: $NAME"
  29. do_stop
  30. echo "."
  31. ;;
  32. reload|graceful)
  33. echo -"Reloading $DESC configuration..."
  34. do_reload
  35. echo "."
  36. ;;
  37. restart)
  38. echo -"Restarting $DESC: $NAME"
  39. do_stop
  40. do_start
  41. echo "."
  42. ;;
  43. *)
  44. echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
  45. exit 3
  46. ;;
  47. esac
  48. exit 0

设置所有人都有对这个启动脚本nginx文件的执行权限

bash
  1. chmod a+/etc/init.d/nginx

把nginx加入系统服务中

bash
  1. chkconfig --add nginx

把服务设置为开机启动

bash
  1. chkconfig nginx on

reboot重启系统生效

bash
  1. #启动nginx服务
  2. systemctl start nginx.service
  3. #停止nginx服务
  4. systemctl stop nginx.service
  5. #重启nginx服务
  6.  
  7. #同时也可以通过下面的命令进行服务重启,停止操作
  8. service nginx restart
  9. service nginx start
  10. service nginx stop

如果服务启动的时候出现 Restarting nginx daemon: nginxnginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
nginx not running 的错误,通过nginx -c 参数指定配置文件即可解决

bash
  1. /usr/local/nginx/sbin/nginx -/usr/local/nginx/conf/nginx.conf

如果服务启动中出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的错误,可以先通过service nginx stop 停止服务,再启动就好。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值