一、简述:

Nginx在编译安装完成后不会注册为系统服务,所以需要手工添加系统服务。
本文介绍在CentOS 6和CentOS 7 下添加nginx系统开机启动服务的方法。

二、Centos7 添加nginx开机启动

建立服务管理文件:

  • 新建/usr/lib/systemd/system/nginx.service文件,并添加以下内容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #nginx服务配置到该文件中
    #服务描述性的配置
    [Unit]
    Description=nginx
    Documentation=http://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target
    #服务关键配置
    [Service]
    Type=forking
    #pid文件位置一定要与nginx实际配置文件中的pid配置路径一致,这个很重要,否则会服务启动失败
    PIDFile=/usr/local/nginx/logs/nginx.pid
    #启动前检测 nginx配置文件是否存在问题
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    #启动
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    #重启
    ExecReload=/bin/kill -s HUP $MAINPID
    #关闭
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    [Install]
    WantedBy=multi-user.target

使用systemd进行管理服务

  • 启动nginx服务

    1
    systemctl start nginx.service
  • 查看nginx服务状态

    1
    systemctl status nginx.service
  • 设置nginx.service开机启动

    1
    systemctl enable nginx.service
  • 测试重新加载

    1
    systemctl reload nginx.service

三、CentOS 6 添加nginx开机启动:

建立服务管理文件:

  • 新建 /etc/init.d/nginx 并添加以下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    #!/bin/bash
    # nginx Startup script for the Nginx HTTP Server
    # it is v.1.2.1 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、nginx_config、nginx_pid一定要根据自己的实际配置保持一致,这个很重要,否则会服务启动失败
    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
  • 添加执行权限

    1
    chmod 755 /etc/init.d/nginx

CentOS6 下的nginx服务管理

  • 添加nginx服务

    1
    chkconfig --add nginx
  • 设置nginx开机启动

    1
    chkconfig nginx 35 on
  • 查看添加的服务

    1
    chkconfig --list |grep nginx
  • 启动nginx服务

    1
    service nginx start
  • 查看nginx服务状态

    1
    service nginx status