Nginx 学习日志(六)Linux 下设置开机自启动

本贴最后更新于 1311 天前,其中的信息可能已经事过景迁

最近升级了下 linux 服务器配置,重启之后,发现要敲一堆命令行,挺麻烦的,能开启自启动就好了

参考资料:在 Linux 中利用 Service 命令添加系统服务及开机自启动
Linux 下设置 nginx 开机自动启动

1 编写服务启动脚本

编写一个脚本,然后把它放在/etc/init.d 这个目录下,再用 service + 脚本名字 运行即可
linux 下可以通过 vim 创建脚本然后添加脚本内容,我个人比较喜欢直接在 windows 上创建好,然后用 winscp 上传到服务器上面(需要注意 windows 和 linux 特殊字符的问题)

vim /etc/init.d/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


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

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

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

pidfile="/usr/local/nginx/logs/nginx.pid"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      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
    fi
}

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 $prog -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

2 主要配置修改

pidfile="/usr/local/nginx/logs/nginx.pid"   //nginx.pid所在位置
nginx="/usr/local/nginx/sbin/nginx"       //nginx 执行程序所在位置
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" //nginx 配置文件所在位置

如果找不到,或者忘记了上述文件所在位置,可以通过命令 find 查找

find / -name nginx

3 权限授予

chmod a+x /etc/init.d/nginx //a表示所有,+表示添加,x表示可执行

4 service 启动并设置自启动

service nginx start 启动
service nginx restart  重启
service nginx stop 停止

如果执行没有问题就可以通过 chkconfig 命令设置开机启动

chkconfig --add nginx   //先执行
chkconfig nginx on     //后执行

最后可以通过 chkconfig --list 查看自启动程序

5 问题总结

1 启动 Nginx 时一直卡着不动在 Starting nginx (via systemctl): 但是实际上还是启动成功了。
查看文件提示:PID file /usr/local/nginx/logs/nginx.pid not readable (yet?) after start.
大概率是因为 pidfile="/usr/local/nginx/logs/nginx.pid" 这个文件位置和 nginx.conf 里面的 pid 文件位置不对导致的。

另一种处理方法:(未验证)
在/usr/lib/systemd/system/nginx.service 中添加入下内容
ExecStartPost=/bin/sleep 0.1

2 启动 nginx 失败 提示 not bind()
简单的端口占用问题,找到端口,关闭就好了。

6 扩展(docker 自启动)

见资料:docker 设置容器开启自启动(无须写脚本)

  • NGINX

    NGINX 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 NGINX 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本 0.1.0 发布于 2004 年 10 月 4 日。

    311 引用 • 546 回帖 • 38 关注
  • Linux

    Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 Unix 的多用户、多任务、支持多线程和多 CPU 的操作系统。它能运行主要的 Unix 工具软件、应用程序和网络协议,并支持 32 位和 64 位硬件。Linux 继承了 Unix 以网络为核心的设计思想,是一个性能稳定的多用户网络操作系统。

    915 引用 • 931 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...