Nginx設(shè)置成服務(wù)并開機(jī)自動(dòng)啟動(dòng)的配置
在/etc/init.d下創(chuàng)建文件nginx
[root@localhost ~]# vim /etc/init.d/nginx
其內(nèi)容參考nginx官方文檔
需要注意的配置:
nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx執(zhí)行程序的路徑。 NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路徑。
保存后設(shè)置文件的執(zhí)行權(quán)限
[root@localhost ~]# chmod a+x /etc/init.d/nginx
至此就可以通過(guò)下面指令控制啟動(dòng)停止
/etc/init.d/nginx start /etc/init.d/nginx stop
上面的方法完成了用腳本管理nginx服務(wù)的功能,但是還是不太方便。
先將nginx服務(wù)加入chkconfig管理列表:
[root@localhost ~]# chkconfig --add /etc/init.d/nginx
加完這個(gè)之后,就可以使用service對(duì)nginx進(jìn)行啟動(dòng),重啟等操作了。
service nginx start service nginx stop service nginx restart
最后設(shè)置開機(jī)自動(dòng)啟動(dòng)
[root@localhost ~]# chkconfig nginx on
#!/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
到此這篇關(guān)于Nginx設(shè)置成服務(wù)并開機(jī)自動(dòng)啟動(dòng)的文章就介紹到這了,更多相關(guān)Nginx開機(jī)自動(dòng)啟動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx實(shí)現(xiàn)跨域使用字體文件的配置詳解
這篇文章主要給大家介紹了關(guān)于Nginx實(shí)現(xiàn)跨域使用字體文件的配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-06-06
nginx反向代理60s超時(shí)報(bào)錯(cuò)問(wèn)題解決
本文主要介紹了Nginx反向代理時(shí)遇到60秒超時(shí)報(bào)錯(cuò)的問(wèn)題,經(jīng)過(guò)排查發(fā)現(xiàn)是由于代理服務(wù)執(zhí)行時(shí)間過(guò)長(zhǎng)導(dǎo)致的,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
使用Docker實(shí)現(xiàn)Nginx反向代理
本文主要介紹了使用Docker實(shí)現(xiàn)Nginx反向代理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Nginx服務(wù)器添加Systemd自定義服務(wù)過(guò)程解析
這篇文章主要介紹了Nginx服務(wù)器添加Systemd自定義服務(wù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
nginx網(wǎng)站服務(wù)如何配置防盜鏈(推薦)
這篇文章主要介紹了nginx網(wǎng)站服務(wù)如何配置防盜鏈,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
使用nginx配置基于域名的虛擬主機(jī)實(shí)現(xiàn)
這篇文章主要介紹了nginx配置基于域名的虛擬主機(jī)實(shí)現(xiàn)​,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

