环境:

  系统:CentOS 6.5 Final

  安装目录:/usr/local/nginx

Nginx开机自启:

①编写shell实现控制

vi /etc/init.d/nginx

添加内容:

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

更改访问权限:

chmod a+x /etc/init.d/nginx

测试效果:

[root@localhost ~]# /etc/init.d/nginx stop
停止 nginx: [确定]
[root@localhost ~]# /etc/init.d/nginx start
正在启动 nginx: [确定]
[root@localhost ~]# /etc/init.d/nginx restart
停止 nginx: [确定]
正在启动 nginx: [确定]
[root@localhost ~]# /etc/init.d/nginx stop
停止 nginx: [确定]
[root@localhost ~]# /etc/init.d/nginx sto
Usage: nginx {start|stop|restart|reload|status|help}
[root@localhost ~]# /etc/init.d/nginx restart
停止 nginx: [失败]
正在启动 nginx: [确定]

②使用该shell配置开机启动

法一:

vi /etc/rc.local

添加一行

/etc/init.d/nginx start 

保存并退出,重启生效。

法二:

chkconfig --add nginx //添加索引
chkconfig --level nginx on //开机启动

重启生效。

③添加自定义命令(使用nginx代替/etc/init.d/nginx,启动nginx再也不用输那么长命令了)

vi  /root/.bashrc

追加

alias nginx="/etc/init.d/nginx"

重启生效。

参考:

Linux下的Nginx安装(开机自启动):http://www.cnblogs.com/meteoric_cry/archive/2011/01/27/1945882.html

Linux下chkconfig命令详解:http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html

nginx documentation:http://nginx.org/en/docs/

最新文章

  1. Win10 UI入门 圆形控件
  2. RANSAC算法笔记
  3. 关于DOM对象与JQuery对象的那些事
  4. android scrollview 实现上下左右滚动方法
  5. Ubuntu Kylin15下PHP环境的搭建(LAMP)
  6. PHP检测终端设备是平板、手机还是电脑
  7. An AVPlayerItem cannot be associated with more than one instance of AVPlayer错误
  8. 阶乘之和 & 程序运行时间 & 算法分析
  9. zw版【转发·台湾nvp系列Delphi例程】HALCON ClipRegion
  10. JavaScript 继承方式详解
  11. c语言知识点总结(摘自head first c)
  12. crtmpserver流媒体服务器的介绍与搭建
  13. java中的反射机制_____
  14. 使用NPOI生成Excel级联列表
  15. 对Java中多态,封装,继承的认识(重要)
  16. Jupyter Notebook的快捷键
  17. Functional programming idiom
  18. vue--公告轮播
  19. noip第4课作业
  20. javascript计算字符串长度

热门文章

  1. AngularJS——第1章 简介
  2. 5-Error:failed to find Build Tools revision 28.0.0 rc1解决方案
  3. SAP请求号的传输
  4. selenium无界面chromedriver
  5. 4.Mysql中的运算符
  6. [转载]RPM中SPEC常用路径以及宏变量
  7. 在开发node.js中,关于使用VS2013插件出现一直读取资源的问题
  8. Ubuntu下安装VS code
  9. shell脚本返回字符串
  10. EXISTS 和 IN 的区别