原码安装nginx,启动脚本和加入开机自启

本文档介绍了如何在Linux系统上手动安装nginx,并通过创建启动脚本使其支持开机自启。详细步骤包括:编写并赋予脚本执行权限,将脚本复制到init.d目录,使用chkconfig添加服务,以及通过systemctl管理nginx服务的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

chkconfig: 345 85 15 (这个比较有意思,345代表在设置在那个level中是on的,如果一个都不想on,那就写一个横线"-",比如:chkconfig: - 85 15。后面两个数字当然代表S和K的默认排序号啦)都在/etc/rc(0~6).d 中的S85tomcat K15tomc
#!/bin/bash
#chkconfig: 2345 96 14
. /etc/init.d/functions
start(){
if [ -f /usr/local/nginx/logs/nginx.pid ]
then
echo_failure
echo "nginx is running"
else
/usr/local/nginx/sbin/nginx
echo_success
echo
fi
}
stop(){
if [ -f /usr/local/nginx/logs/nginx.pid ]
then
/usr/local/nginx/sbin/nginx -s stop
echo_success
echo
else
echo "nginx is stop"
echo_failure
echo
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
*)
echo "Useage: $0 |start|stop|restart"
esac

[root@wangying2 ~]#
[root@wangying2 ~]# bash nginx.sh restart
[root@wangying2 ~]# vim nginx.sh [ 确定 ]
[root@wangying2 ~]# bash nginx.sh restart
[ 确定 ]
[root@wangying2 ~]# bash nginx.sh start
nginx is running [失败]
[root@wangying2 ~]# bash nginx.sh stop
[root@wangying2 ~]# bash nginx.sh start
[ 确定 ]
[root@wangying2 ~]#
[root@wangying2 ~]# vim nginx.sh
[root@wangying2 ~]# bash nginx.sh start
nginx is running [失败]
[root@wangying2 ~]# bash nginx.sh stop
[ 确定 ]
[root@wangying2 ~]# bash nginx.sh stop
nginx is stop
[失败]
将写的脚本加入开机自启的服务中
[root@wangying2 ~]# chmod a+x nginx.sh 给脚本添加权限
[root@wangying2 ~]# cp -p nginx.sh /etc/init.d/nginx 将shell脚本复制到开启自启文件夹中
[root@wangying2 ~]# chkconfig --add nginx 将nginx加入到开机启动的服务中
[root@wangying2 ~]# systemctl daemon-reload 将开启自启的服务重载
[root@wangying2 ~]# systemctl start nginx
[root@wangying2 ~]# systemctl stop nginx
[root@wangying2 ~]# systemctl status nginx
● nginx.service - (null)
Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
Active: inactive (dead) since 五 2018-10-12 18:18:35 CST; 6s ago
Docs: man:systemd-sysv-generator(8)
Process: 12495 ExecStop=/etc/rc.d/init.d/nginx stop (code=exited, status=0/SUCCESS)
Process: 12480 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=0/SUCCESS)

10月 12 18:18:29 wangying2 systemd[1]: Starting (null)...
10月 12 18:18:29 wangying2 nginx[12480]: [ 确定 ]
10月 12 18:18:29 wangying2 systemd[1]: Started (null).
10月 12 18:18:35 wangying2 systemd[1]: Stopping (null)...
10月 12 18:18:35 wangying2 nginx[12495]: [ 确定 ]
10月 12 18:18:35 wangying2 systemd[1]: Stopped (null).
[root@wangying2 ~]#

注意点:

[root@wangying2 ~]# cat ping.sh
#!/bin/bash

for i in {0..255}
do
for j in {0..255}
do
if [ $i -eq 0 -a $j -eq 0 ] || [ $i -eq 255 -a $j -eq 255 ]
then
continue
else
( if /usr/bin/ping -c 1 172.25.$i.$j &> /dev/null
then
echo "172.25.$i.$j is ok"
fi ) &
fi
done
done
[root@wangying2 ~]#

[root@wangying2 ~]# chmod a+x ping.sh
[root@wangying2 ~]# cp ping.sh /etc/init.d/ping
[root@wangying2 ~]# chkconfig --add ping
服务 ping 不支持 chkconfig
[root@wangying2 ~]#
当脚本文件中没有
#chkconfig: 2345 96 14 这条语句解释时,就会出现上面的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值