tft每日頭條

 > 圖文

 > nginx設置開機自啟動

nginx設置開機自啟動

圖文 更新时间:2025-02-19 09:45:40

nginx設置開機自啟動?利用 systemctl 命令:如果用 yum install 命令安裝的 nginx,yum 命令會自動創建 nginx.service 文件,直接用命令:,今天小編就來聊一聊關于nginx設置開機自啟動?接下來我們就一起去研究一下吧!

nginx設置開機自啟動(Nginx最全操作)1

nginx設置開機自啟動

利用 systemctl 命令:

如果用 yum install 命令安裝的 nginx,yum 命令會自動創建 nginx.service 文件,直接用命令:

systemctl enable nginx # 設置開機啟動 Nginxsystemctl disable nginx # 關閉開機啟動 Nginx

就可以設置開機自啟,否則需要在系統服務目錄裡創建 nginx.service 文件。

創建并打開 nginx.service 文件:

vim /lib/systemd/system/nginx.service

内容如下:

[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target

:wq 保存退出,運行 systemctl daemon-reload 使文件生效。

這樣便可以通過以下命令進行操作 nginx 了:

systemctl start nginx.service # 啟動nginx服務 systemctl enable nginx.service # 設置開機啟動 systemctl disable nginx.service # 停止開機自啟動 systemctl status nginx.service # 查看服務當前狀态 systemctl restart nginx.service # 重新啟動服務 systemctl is-enabled nginx.service #查詢服務是否開機啟動

通過開機啟動命令腳本實現開機自啟

創建開機啟動命令腳本文件:

vim /etc/init.d/nginx

在這個 nginx 文件中插入一下啟動腳本代碼,啟動腳本代碼來源網絡複制,實測有效:

#! /bin/bash# chkconfig: - 85 15 PATH=/usr/local/nginx DESC="nginx daemon"NAME=nginx DAEMON=$PATH/sbin/$NAME CONFIGFILE=$PATH/conf/$NAME.conf PIDFILE=$PATH/logs/$NAME.pidscript NAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop(){ $DAEMON -s stop || echo -n "nginx not running" } do_reload() { $DAEMON -s reload || echo -n "nginx can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0

設置所有人都有對這個啟動腳本 nginx 文件的執行權限:

chmod a x /etc/init.d/nginx

把 nginx 加入系統服務中:

chkconfig --add nginx

把服務設置為開機啟動:

chkconfig nginx on

reboot 重啟系統後,就可以使用上面的 systemctl 方法相同的是:

systemctl start nginx.service # 啟動nginx服務 systemctl enable nginx.service # 設置開機啟動 systemctl disable nginx.service # 停止開機自啟動 systemctl status nginx.service # 查看服務當前狀态 systemctl restart nginx.service # 重新啟動服務 systemctl is-enabled nginx.service #查詢服務是否開機啟動

如果服務啟動的時候出現

Restarting nginx daemon: nginxnginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) nginx not running 的錯誤,通過 nginx -c 參數指定配置文件即可解決

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

如果服務啟動中出現 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的錯誤,可以先通過 service nginx stop 停止服務,再啟動就好。

歡迎大家提出不一樣的觀點,我們一起讨論,

我是辣個男人,一個運維人。

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关圖文资讯推荐

热门圖文资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved