溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Centos7中Nginx開機(jī)自啟動(dòng)的問(wèn)題怎么解決

發(fā)布時(shí)間:2022-05-06 10:43:43 來(lái)源:億速云 閱讀:264 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“Centos7中Nginx開機(jī)自啟動(dòng)的問(wèn)題怎么解決”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

關(guān)于在centos7中設(shè)置nginx開機(jī)自啟動(dòng),我們可以通過(guò)編寫開機(jī)自啟動(dòng)shell腳本來(lái)解決。

測(cè)試環(huán)境

操作系統(tǒng):centos7 64位 1611

nginx版本: 1.11.10

本機(jī)nginx安裝時(shí)的配置參數(shù)

./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

編寫腳本

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

以下是腳本內(nèi)容

#!/bin/bash
# nginx startup script for the nginx http server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: nginx is a high-performance web and proxy server.
#       it has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
retval=0
prog="nginx"
# source function library.
. /etc/rc.d/init.d/functions
# source networking configuration.
. /etc/sysconfig/network
# check that networking is up.
[ "${networking}" = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
  echo "nginx already running...."
  exit 1
fi
  echo -n $"starting $prog: "
  daemon $nginxd -c ${nginx_config}
  retval=$?
  echo
  [ $retval = 0 ] && touch /var/lock/subsys/nginx
  return $retval
}
# stop nginx daemons functions.
stop() {
    echo -n $"stopping $prog: "
    killproc $nginxd
    retval=$?
    echo
    [ $retval = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
# reload nginx service functions.
reload() {
  echo -n $"reloading $prog: "
  #kill -hup `cat ${nginx_pid}`
  killproc $nginxd -hup
  retval=$?
  echo
}
# see how we were called.
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
    reload
    ;;
restart)
    stop
    start
    ;;
status)
    status $prog
    retval=$?
    ;;
*)
    echo $"usage: $prog {start|stop|restart|reload|status|help}"
    exit 1
esac
exit $retval
:wq 保存并退出

*對(duì)于shell腳本中的部分文件路徑請(qǐng)修改成你主機(jī)上nginx的相應(yīng)路徑,例如:  nginxd=/usr/local/nginx/sbin/nginx  nginx_config=/usr/local/nginx/conf/nginx.conf  nginx_pid=/usr/local/nginx/logs/nginx.pid  以上都是本測(cè)試機(jī)nginx的相應(yīng)路徑  還有nginx的pid默認(rèn)路徑是nginx安裝目錄的logs/nginx.pid里。

設(shè)置文件的訪問(wèn)權(quán)限

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

(a+x ==> all user can execute  所有用戶可執(zhí)行)

這樣在控制臺(tái)就很容易的操作nginx了:查看nginx當(dāng)前狀態(tài)、啟動(dòng)nginx、停止nginx、重啟nginx…

usage : nginx {start|stop|restart|reload|status|help}

如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加載新的配置文件并運(yùn)行,可以將此命令加入到rc.local文件中,這樣開機(jī)的時(shí)候nginx就默認(rèn)啟動(dòng)了

加入到rc.local文件中

[root@localhost]# vi /etc/rc.local

加入一行  /etc/init.d/nginx start    保存并退出,下次重啟會(huì)生效。

注意

如果開機(jī)后發(fā)現(xiàn)自啟動(dòng)腳本沒(méi)有執(zhí)行,你要去確認(rèn)一下rc.local這個(gè)文件的訪問(wèn)權(quán)限是否是可執(zhí)行的,因?yàn)閞c.local默認(rèn)是不可執(zhí)行的。

修改rc.local訪問(wèn)權(quán)限,增加可執(zhí)行權(quán)限

[root@localhost]# chmod +x /etc/rc.d/rc.local

現(xiàn)在重啟后,自啟動(dòng)腳本就能正常執(zhí)行了。

可以通過(guò)以下命令來(lái)查看nginx進(jìn)行的運(yùn)行情況

[root@localhost]# ps aux | grep nginx

“Centos7中Nginx開機(jī)自啟動(dòng)的問(wèn)題怎么解決”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI