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. 【可持久化0/1Trie】【P4735】最大异或和

    Description 给定一个长度为 \(n\) 的序列 \(A\),有 \(m\) 次操作,每次要么在序列尾部再添加一个数,将序列长度 \(n\) 加一,要么给进行一次查询,给定查询参数 \(l, ...

  2. Python常用经典案例

    Python循环语句: 函数: 异常处理:  类和继承: 相信初学Python的我们对于好多语句都还不熟悉,经常会遇到不知道以前c语言上面的语句转换成Python语句是怎么样的,会出现错误的情况,因此 ...

  3. 第02组 Alpha冲刺(1/4)

    队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:进行组员分工 GitHub签入记录 接下来的计划:构思游戏实现 还剩下哪些任务:敲代码 燃尽图 遇到的困难:任务分配的不及时,导致很 ...

  4. “/usr/local/lib/libosipparser2.so.7: could not read symbols: Invalid operation” 异常解决

    编译c代码报错如下 /usr/bin/ld: /tmp/ccl8nBND.o: undefined reference to symbol 'osip_message_get_body' /usr/b ...

  5. window 运行spark报错

    Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties // :: ERROR Shell: F ...

  6. asbDio (asbDio.dll)

    using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices ...

  7. Net Core的流控服务

    统一流控服务开源:基于.Net Core的流控服务   先前有一篇博文,梳理了流控服务的场景.业界做法和常用算法 统一流控服务开源-1:场景&业界做法&算法篇 最近完成了流控服务的开发 ...

  8. MySQL查询指定表的字段名称

    1,查询表中所有字段(横排):SELECT GROUP_CONCAT( COLUMN_NAME SEPARATOR "," ) FROM information_schema.CO ...

  9. The Preliminary Contest for ICPC Asia Nanjing 2019/2019南京网络赛——题解

    (施工中……已更新DF) 比赛传送门:https://www.jisuanke.com/contest/3004 D. Robots(期望dp) 题意 给一个DAG,保证入度为$0$的点只有$1$,出 ...

  10. Java并发编程: CountDownLatch、CyclicBarrier和 Semaphore

    java 1.5提供了一些非常有用的辅助类来帮助并发编程,比如CountDownLatch,CyclicBarrier和Semaphore. 1.CountDownLatch –主线程阻塞等待,最后完 ...