环境:

  系统: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/

Nginx使用(配置开机启动)的更多相关文章

  1. nginx配置开机启动及配置sudo授权启动

    1.https://www.cnblogs.com/whatmiss/p/7091220.html        配置开机启动nginx 2.sudo授权其它用户启动 (1)root用户编辑 visu ...

  2. Linux下配置Nginx(在root的/etc/rc.local里配置开机启动功能http://tengine.taobao.org/)

    上面是下载的包下载地址 http://tengine.taobao.org/download_cn.html nginx官网http://nginx.org/ 下一步 下一步 其中remote为重要属 ...

  3. yum 安装nginx(配置开机启动)

    yum install -y nginx 通过yum安装的时候提示下面的错误 [root@localhost yum.repos.d]# yum install nginx 已加载插件:fastest ...

  4. linux nginx,php开机启动

    nginx开机启动 1.首先,在linux系统的/etc/init.d/目录下创建nginx文件 vim /etc/init.d/nginx 2.加入脚本 #!/bin/bash # nginx St ...

  5. centos LAMP第二部分apache配置 下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转 配置apache的访问日志 配置静态文件缓存 配置防盗链 访问控制 apache rewrite 配置开机启动apache tcpdump 第二十节课

    centos    LAMP第二部分apache配置  下载discuz!配置第一个虚拟主机 安装Discuz! 用户认证 配置域名跳转  配置apache的访问日志  配置静态文件缓存  配置防盗链 ...

  6. Linux开机启动顺序启动顺序及配置开机启动

    Linux:开机启动顺序启动顺序及配置开机启动 开机启动顺序 1.加载内核 2.启动 init(/etc/inittab) pid=1 3.系统初始化 /etc/rc.d/rc.sysinit 4.运 ...

  7. OS X下安装Redis及配置开机启动

    1.下载redis源码包redis-3.0.5.tar(此步骤可在图形界面下操作) 2.解压源码包 tar zxvf redis-3.0.5.tar 3.编译源码并安装 #进入源码目录 cd redi ...

  8. Centos 配置开机启动脚本启动 docker 容器

    Centos 配置开机启动脚本启动 docker 容器 Intro 我们的 Centos 服务器上部署了好多个 docker 容器,因故重启的时候就会导致还得手动去手动重启这些 docker 容器,为 ...

  9. 【RabbitMQ】 RabbitMQ配置开机启动

    环境 系统:Linux(CentOS 7.2) Erlang环境:21.1(安装参考[Erlang]源码安装) RabbitMQ:3.7.9(安装参考[RabbitMQ] RabbitMQ安装) 配置 ...

随机推荐

  1. css兼容性写法大全

    淘宝初始化代码 body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset ...

  2. JFinal Web开发学习(一)开启HelloWorld

    初次接触JFinal框架,接触过MVC思想,接触过Spring. JFinal官网: http://www.jfinal.com/ 之前很嫌弃JavaWeb开发,主要原因是繁琐的xml配置. 官方推荐 ...

  3. python 数据类型 之 利用 dict 模仿 switch语句功能

    Python本身并不提供Switch的语法功能,为了能够解决类似switch分支需求的问题,我们可以使用字典代替实现. 解决思路: 利用字典取值的get方法的容错性,处理switch语句中的defau ...

  4. Spring 监听

    Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...

  5. C# mysql 插入数据,中文乱码

    用C#操作mysql时, 插入数据中文都是乱码,只显示问号,数据库本身使用的是utf-8字符. 网上百度一下有两种解决办法: 一种是在执行语句前面设置,如:MySQLCommand mCommand ...

  6. Android.API.Context.getFilesDir()

    1. Context.getFilesDir() http://developer.android.com/reference/android/content/Context.html#getFile ...

  7. spring+mybatis+mina+logback框架搭建

    第一次接触spring,之前从来没有学过spring,所以算是赶鸭子上架,花了差不多一个星期来搭建,中间遇到各种各样的问题,一度觉得这个框架搭建非常麻烦,没有一点技术含量,纯粹就是配置,很低级!但随着 ...

  8. 09. pt-fingerprint

    vim pt-fingerprint.txt select name, password from user where id=5;select name, password from user wh ...

  9. mysql bigint ,int , smallint,tinyint 的范围

    bigint  8字节 64位 int 4字节 32位 smallint 2字节 16位 tinyint 1字节8位 .. 范围  -128 到 127  , 如果是无符号 ,则返回 位 0-255 ...

  10. 如何用xx-net上youtube

    1.下载https://github.com/XX-net/XX-Net/blob/master/code/default/download.md   里面的稳定版本 2.下载chrome.百度chr ...