1、本地环境

[root@dev ~]#cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)

2、在/etc/init.d创建nginx文件,并添加如下内容

[root@dev ~]# 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
# 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:.*--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 $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

3、保存文件后对文件设置权限

chmod a+x nginx

4、把脚本添加到系统服务

chkconfig --add /etc/init.d/nginx

5、验证系统服务

service nginx start //开户nginx
service nginx stop //关闭nginx
  • 以上部分将nginx添加到了系统服务,下面将nginx添加到开机自启中

6、添加到开机自启

chkconfig nginx on

7、可能遇到的问题

systemd: Starting nginx - high performance web server...
nginx: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: nginx: configuration file /etc/nginx/nginx.conf test is successful
systemd: PID file /run/nginx.pid not readable (yet?) after start.
systemd: nginx.service start operation timed out. Terminating.
systemd: Failed to start nginx - high performance web server.
systemd: Unit nginx.service entered failed state.
systemd: nginx.service failed. 解决:
修改nginx.conf中pid文件的路径为/var/run/nginx.pid

Linux下设置Nginx开机自启的更多相关文章

  1. ElasticSearch(十四):Linux下设置ElasticSearch 开机自启

    一.创建脚本文件 在  /etc/init.d  目录下,创建脚本文件 elasticsearch # cd /etc/init.d/ # vim elasticsearch 将以下内容写入文件中(其 ...

  2. Linux(CentOS)下设置nginx开机自动启动(2个办法)

    首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vim /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...

  3. linux 上设置mysql开机自启

    此方式是通过安装包安装的,如果是yum安装的rpm包,可参考yum安装MySQL8.0 三个月之前安装的mysql,记得是设置了开机自启,但是今天再次进入的时候发现,无法登录,报错如下 原因是mysq ...

  4. Linux(CentOS)系统下设置nginx开机自启动

    Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动.首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:vi ...

  5. Linux下设置svn开机自启动

    方式一:centos 7 设置svn开机自启动 使用新的systemctl 服务命令  (笔者成功验证过,该方式可行) [root@iZjvr37lpviqbkZ init.d]# vi /lib/s ...

  6. Linux下设置Tomcat开机自启动

    --未验证 第一步:在/etc/init.d下新建一个文件tomcat(需要root操作权限) vi /etc/init.d/tomcat 然后点击"i"写下如下代码,tomcat ...

  7. Linux下设置Tomcat开机启动

    1.进入/etc/rc.d/init.d,新建文件tomcat,并让它成为可执行文件 chmod 755 tomcat. #!/bin/bash # # /etc/rc.d/init.d/tomcat ...

  8. centos6.9下设置nginx服务开机自动启动

    首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vi /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...

  9. centos7下安装、配置Nginx、设置Nginx开机自启动

    测试环境: [root@centos-linux ~]# cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core) [root@cento ...

随机推荐

  1. 基于zookeeper-3.5.5安装hadoop-3.1.2

    目录 目录 1 1. 前言 3 2. 缩略语 3 3. 安装步骤 4 4. 下载安装包 4 5. 机器规划 4 6. 设置批量操作参数 5 7. 环境准备 5 7.1. 修改最大可打开文件数 5 7. ...

  2. 【树状数组】【P5069】[Ynoi2015]纵使日薄西山

    Description 给定一个长度为 \(n\) 的非负整数序列 \(\{a_n\}\),\(q\) 次操作,每次要么单点修改序列某个值,要么查询整个序列需要操作多少次才能变成全 \(0\). 一次 ...

  3. PHP读取文件内容的五种方式

    -----第一种方法-----fread()-------- <?php $file_path = "test.txt"; if(file_exists($file_path ...

  4. mac 下使用nasm

    #安装nasm brew install nasm #创建文件 vi hello.asm 写入如下内容 msg: db "hello world!", 0x0a len: equ ...

  5. 解决github clone慢的问题

    github clone非常慢,解决方法,首先要有vpn 参考 https://www.zhihu.com/question/27159393 第一种方法 这种是没有vpn的方法,测试从10k到 几十 ...

  6. 详解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    转载(https://www.jb51.net/article/130560.htm) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  7. linux -------- 使用xshell ,winscp 连接linux 以及一些问题解决

    1. 安装主要就是默认确定 2.连接VM上的centos 系统 设置连接模式 3.通过 ping 虚拟机上的linux系统的IP地址 4.使用xshell 进行连接 5.输入linux 的用户名和密码 ...

  8. [转帖]Mysql各版本介绍及下载

    Mysql各版本介绍及下载 http://blog.itpub.net/12679300/viewspace-1251661/ 原创 MySQL 作者:wzq609 时间:2014-08-15 10: ...

  9. 小程序接口无法传递session校验验证码

    今天在写接口的时候发现一个问题,我用apiaaz测试一切正常,但是从小程序接口请求验证码,一直验证失败. 最开始用的图形验证码,查阅了不少资料,最后怀疑是cookie的问题,解决无果,换成了短信验证码 ...

  10. Python实现Newton和lagrange插值

    一.介绍Newton和lagrange插值:给出一组数据进行Newton和lagrange插值,同时将结果用plot呈现出来1.首先是Lagrange插值:根据插值的方法,先对每次的结果求积,在对结果 ...