博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx 开机启动
阅读量:7053 次
发布时间:2019-06-28

本文共 2635 字,大约阅读时间需要 8 分钟。

hot3.png

本文摘自:

http://www.01happy.com/centos-nginx-shell-chkconfig/

 

在安装完nginx后,重新启动需要“kill -HUP nginx进程编号”来进行重新加载,显然十分不方便。如果能像apache一样,直接通过脚本进行管理就方便多了。

nginx官方早就想好了,也提供了这个脚本,地址:http://wiki.nginx.org/RedHatNginxInitScript。这里将管理脚本收录在这里:

启动脚本有坑,nginx reload 和stop的时候是直接用killproc 干掉进程的,应该用nginx -s reload 和nginx -s stop 完整脚本如下

#!/bin/sh## nginx - this script starts and stops the nginx daemin## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /www/wdlinux/nginx/conf/nginx.conf# pidfile: /www/wdlinux/nginx/logs/nginx.pid# Url http://www.wdlinux.cn# Last Updated 2010.06.01# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/tengine/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/tengine/conf/nginx.conf"#NGINX_PID="/www/wdlinux/nginx/logs/nginx.pid"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxstart() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo ##service php-fpm start [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: " $nginx -s stop echo_success retval=$? echo ##service php-fpm stop [ $retval -eq 0 ] && rm -f $lockfile return $retval}restart() { stop start}reload() { configtest || return $? echo -n $"Reloading $prog: " $nginx -s reload RETVAL=$? echo}force_reload() { restart}configtest() { $nginx -t -c $NGINX_CONF_FILE}rh_status() { status $prog}rh_status_q() { rh_status >/dev/null 2>&1}case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac

将以上脚本保存到/etc/init.d/nginx文件,并修改两个地方:

  • nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
  • NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。

保存后,就可以通过该脚本对nginx服务进行管理了:

$ /etc/init.d/nginx start$ /etc/init.d/nginx stop$ /etc/init.d/nginx reload

使用chkconfig进行管理

上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便,比如要设置nginx开机启动等。这时可以使用chkconfig来设置。

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

chkconfig --add /etc/init.d/nginx

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

$ service nginx start$ service nginx stop$ service nginx reload

设置终端模式开机启动:

$ chkconfig --level 3 nginx on

 

转载于:https://my.oschina.net/lxphemy/blog/717594

你可能感兴趣的文章
怎么搞差分约束?
查看>>
git基本的使用原理
查看>>
Ubuntu机器学习python实战(一)k-近邻算法
查看>>
Reachability(判断网络是否连接)
查看>>
sql奇特的语句
查看>>
安卓之文件结构
查看>>
java 线程
查看>>
嵌入式,代码调试----GDB扫盲
查看>>
IDEA 关于maven项目引入css ,js,image文件 路径的问题
查看>>
进度一
查看>>
composer 安装Laravel
查看>>
项目2.0上线,回想过后杂谈总结基础回顾一番
查看>>
冲刺一 (day 3)
查看>>
Beep使用
查看>>
关于php网络爬虫phpspider。
查看>>
OpenGL的glRotatef旋转变换函数详解
查看>>
c#中 ==与equals有什么区别
查看>>
Oracle Group By ROLLUP-SubTotal
查看>>
PHP 正则表达式
查看>>
Computer Graphics Research Software
查看>>