小知识:Nginx设置成服务并开机自动启动的配置

在/etc/init.d下创建文件nginx

?
1
[root@localhost ~]# vim /etc/init.d/nginx

其内容参考nginx官方文档

需要注意的配置:

?
1
2
3
nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。

保存后设置文件的执行权限

?
1
[root@localhost ~]# chmod a+x /etc/init.d/nginx

至此就可以通过下面指令控制启动停止

?
1
2
/etc/init.d/nginx start
/etc/init.d/nginx stop

上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便。

先将nginx服务加入chkconfig管理列表:

?
1
[root@localhost ~]# chkconfig –add /etc/init.d/nginx

加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

?
1
2
3
service nginx start
service nginx stop
service nginx restart

最后设置开机自动启动

?
1
[root@localhost ~]# chkconfig nginx on
?
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/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 $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

引用:https://blog.csdn.net/buyueliuying/article/details/75353863

到此这篇关于Nginx设置成服务并开机自动启动的文章就介绍到这了,更多相关Nginx开机自动启动内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/javalinux/p/15839175.html

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux中使用命令more,less,cat查看文件内容

2023-3-18 4:00:57

建站知识

小知识:bash脚本中将密码传递给ssh/scp命令方法详解

2023-3-18 4:09:09

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索