nginx设置开机启动

在添加nginx服务之后,大家会希望开机伴随启动nginx,避免手动路径输入启动;

nginx官方提供了启动脚本:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

我这里使用centos系统,首先,进入/etc/init.d/目录,新添加nginx文件,把上述连接中的内容粘贴到nginx文件中,

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse #               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
   for opt in $options; do
       if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    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 2
esac

需要修改两处地方:

  第一:nginx="/usr/sbin/nginx"
     这里修改成你nginx安装时启动文件放在的路径,比如我nginx安装在/usr/local/nginx中,对应启动文件在
     /usr/local/nginx/sbin/nginx,所以把nginx启动路径换成 nginx="/usr/local/nginx/sbin/nginx"

  第二:NGINX_CONF_FILE="/etc/nginx/nginx.conf"     这里同上面一样,我的nginx配置文件也放在 /usr/local/nginx安装目录中,具体在/usr/local/nginx/conf/nginx.conf     所以把nginx配置路径换成 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

保存nginx文件,然后使用chkconfid进行管理,如下

先将nginx服务加入chkconfig管理列表
chkconfig --add /etc/init.d/nginx

加完上述之后,就可以使用service对nginx进行启动,停止和重启等操作了。

#service nginx start
#service nginx stop
#service nginx reload
...

如果在 service nginx start时伴随提示useradd使用有误等提示,那么可以编辑 刚才创建的nginx文件,

定位到 start()方法,把里面的 make_dirs那行注释掉,保存退出,重新启动一次就OK了。

时间: 2024-08-24 07:11:47

nginx设置开机启动的相关文章

编译安装Nginx //设置nginx自动开机启动

Nginx 安装 系统平台:CentOS release 6.6 (Final) 64位. 设置nginx 自动开机启动 :chkconfig --level 235 nginx on chkconfig 提供了一个维护/etc/rc[0~6] d 文件夹的命令行工具,它减轻了系统直接管理这些文件夹中的符号连接的负担.chkconfig主要包括5个原始功能:为系统管理增加新的服务.为系统管理移除服务.列出单签服务的启动信息.改变服务的启动信息和检查特殊服务的启动状态.当单独运行chkconfig

使用本脚本可以自动批量完成中间节点环境的部署工作,包括:Nginx编译安装、添加程序管理脚本、设置开机启动、反向代理配置、证书分发、添加iptables规则等

使用本脚本可以自动批量完成中间节点环境的部署工作,包括:Nginx编译安装.添加程序管理脚本.设置开机启动.反向代理配置.证书分发.添加iptables规则等.脚本支持自定义nginx安装版本.设置编译模块.配置监听端口等. 1. Nginx Role规则说明 本脚本用于中间节点(Nginx反向代理)环境的自动化配置,主要内容包括: 安装基础依赖环境: 创建nginx启动用户(支持自定义用户): 下载nginx安装文件(可自定义nginx版本): 解压安装文件: 执行编译安装(可自定义编译参数和

centos7 nginx 加入开机启动

设置nginx开机启动 vi /etc/rc.d/init.d/nginx  #编辑启动文件添加下面内容 ############################################################ #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HT

centos7设置开机启动命令

1.设置开机启动 systemctl enable xxx.service   如:systemctl enable nginx.service 2.设置禁止开机启动 systemctl disable xxx.service 如:systemctl disable nginx.service 3.查看服务状态信息 systemctl status xxx.service 如:systemctl status nginx.service 4.仅显示活动状态 systemctl is-active

centos7下nginx,tomcaat开机启动(新)

一. 写在前面 centos7建议使用systemctl来管理服务的自启动,它能够满足之前service和chkconfig的功能 1.systemd有系统和用户区分:系统(/user/lib/systemd/system/).用户(/etc/lib/systemd/user/) 2.一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面 3.创建service文件 [Unit] Description=nginx - high performance web

centos7安装docker并设置开机启动

版本要求:查看内核版本,需大于3.10 [[email protected] ~]# uname -r 3.10.0-327.10.1.el7.x86_64 更新内核:如果是生产机器务必慎重更新内核,避免出现不必要的问题. sudo yum update 安装docker [[email protected] ~]# curl -sSL https://get.docker.com/ | sh + sh -c 'sleep 3; yum -y -q install docker-engine'

如何给自己编写的程序隐蔽的设置开机启动

比如说,我们自己编写了一个C#的程序,想设置为开机自动启动.当然我们可以从网上找到方法,一般的做法就是修改注册表文件,找到开机启动项,然后把这个程序的路径写入到某个节点就可以.如下截图所示,这些都是开机后会自动启动的程序. 有时候我们可能会有一些很顽皮的想法,我们希望自己程序在别人的电脑上运行的时候,暗地里把自己设置为开机启动.现在就有一些问题,因为现在电脑上都装有杀毒软件,比如说360,你修改注册表开机启动项,属于敏感操作,会被安全卫士拦截,然后提示用户正在有软件试图修改开机启动项,询问用户是

apache与mysql添加进系统服务并设置开机启动

         apache与mysql添加进系统服务并设置开机启动 apache 1  cp /usr/local/apache/bin/apachectl /etc/init.d/httpd 2  chkconfig --add httpd 出现错误service httpd does not support chkconfig 打开 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面) # chkconfig: 345 85 15 # description

linux jexus 服务 设置开机启动

linux jexus 服务 设置开机启动 linux的服务开机设置一般在 /etc/init.d/里 而jexus的默认安装目录在 /usr/jexus里 启动文件为 jws 参数 有start stop restart 这里贡献一个刚写好的jexus的开启启动脚本 #!/bin/bash ### BEGIN INIT INFO # # Provides: jws # Required-Start: $local_fs $remote_fs # Required-Stop: $local_fs