雖然使用命令行可以對 Nginx 進行各種操作,比如啟動等,但是還是不太方便,下面介紹在 Linux 下安裝 Nginx 後,如何設定其開機自啟動。
1. CentOS6.x&RedHat6.x 配置 Nginx 開機自啟動
1.1 建立 Nginx 的管理腳本 首先,在系統的/etc/init.d/ 目錄下建立 nginx 檔案:
vi /etc/init.d/nginx
點選 i 鍵,在腳本中新增如下內容:
#!/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 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 } 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
顯示程式碼
以上腳本內容來自於 Nginx 官方,腳本地址:http://wiki.nginx.org/RedHatNginxInitScript
注意,對於自訂編譯安裝的 Nginx(說明文檔中提供的即是此種方式),需要根據安裝路徑修改腳本中這兩項配置:
nginx="/usr/nginx/sbin/nginx" ====>nginx執行程式的路徑 NGINX_CONF_FILE="/usr/nginx/conf/nginx.conf" ====>配置檔案的路徑
儲存腳本檔案後設定檔案的執行權限:
chmod a+x /etc/init.d/nginx
然後,就可以透過該腳本對 Nginx 服務進行管理了:
/etc/init.d/nginx start /etc/init.d/nginx stop
1.2 使用 chkconfig 設定開機自啟動 上面的步驟完成了用腳本管理 Nginx 服務的功能,接下來我們就可以使用 chkconfig 來設定 Nginx 開機啟動了。
先將nginx服務加入 chkconfig 管理列表:
chkconfig --add /etc/init.d/nginx
配置完以後,就可以使用以下命令設定開機自啟動等操作了:
chkconfig nginx on #設定開機自啟動 chkconfig nginx off #停止開機自啟動 service nginx start #啟動 Nginx 服務 service nginx stop #停止 Nginx 服務 service nginx restart #重啟 Nginx 服務 service nginx status #查看 Nginx 狀態
2. CentOS7.x&RedHat7.x 配置 Nginx 開機自啟動
2.1 建立 Nginx 的管理腳本 首先,在系統的/lib/systemd/system/ 目錄下建立 nginx.service檔案:
vi /lib/systemd/system/nginx.service
點選 i 鍵,在腳本中新增如下內容:
[Unit] Description=nginx service After=network.target [Service] Type=forking ExecStart=/usr/ nginx/sbin/nginx ExecReload=/usr/ nginx/sbin/nginx -s reload ExecStop=/usr/ nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
注意,對於自訂編譯安裝的 Nginx(說明文檔中提供的即是此種方式),需要根據實際路徑修改腳本中的 Nginx 啟動路徑“/usr/nginx/sbin/nginx”。
儲存腳本檔案後設定檔案的執行權限:
chmod a+x /lib/systemd/system/nginx.service
2.2使用 systemctl 設定開機自啟動 配置完腳本以後,就可以使用以下命令設定開機自啟動等操作了:
systemctl enable nginx.service #設定開機自啟動 systemctl disable nginx.service #停止開機自啟動 systemctl start nginx.service #啟動 Nginx 服務 systemctl stop nginx.service #停止服務 systemctl status nginx.service #查看服務當前狀態 systemctl list-units --type=service #查看所有已啟動的服務
3. 注意事項
如果 Centos8.5 環境 Nginx 和 Tomcat 都放在 /home 目錄下,註冊服務不能啟動的話,可以放到 /usr 目錄下 。