這篇文章主要介紹了centos7系統下nginx安裝并配置開機自啟動操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
這篇文章主要介紹了centos7系統下nginx安裝并配置開機自啟動操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
準備工作
我的centos7系統是最小化安裝的, 缺很多庫, 首先安裝必須的運行庫yum?install?wget?gcc?gcc-c++?pcre-devel?zlib-devel
##創建工作目錄并進入工作目錄mkdir?-p?/z/nginx?&&?cd?/z/nginx
##獲取nginx最新的安裝包wget?http://nginx.org/download/nginx-1.11.10.tar.gz
##解壓縮tar?zxvf?nginx-1.11.10.tar.gz
##進入目錄cd?nginx-1.11.10
##檢測系統配置, 生成make相關文件./configure
./configure執行成功會輸出以下信息
nginx的安裝位置,以及文件路徑Configuration?summary
+?using?system?PCRE?library
+?OpenSSL?library?is?not?used
+?using?system?zlib?library
nginx?path?prefix:?"/usr/local/nginx"
nginx?binary?file:?"/usr/local/nginx/sbin/nginx"
nginx?modules?path:?"/usr/local/nginx/modules"
nginx?configuration?prefix:?"/usr/local/nginx/conf"
nginx?configuration?file:?"/usr/local/nginx/conf/nginx.conf"
nginx?pid?file:?"/usr/local/nginx/logs/nginx.pid"
nginx?error?log?file:?"/usr/local/nginx/logs/error.log"
nginx?http?access?log?file:?"/usr/local/nginx/logs/access.log"
nginx?http?client?request?body?temporary?files:?"client_body_temp"
nginx?http?proxy?temporary?files:?"proxy_temp"
nginx?http?fastcgi?temporary?files:?"fastcgi_temp"
nginx?http?uwsgi?temporary?files:?"uwsgi_temp"
nginx?http?scgi?temporary?files:?"scgi_temp"
編譯并安裝make?&&?make?install
創建nginx啟動命令腳本vi?/etc/init.d/nginx
插入以下內容, 注意修改PATH和NAME字段, 匹配自己的安裝路徑 (這段是從網上copy的)#!?/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.pid
SCRIPTNAME=/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
設置執行權限chmod?a+x?/etc/init.d/nginx
注冊成服務chkconfig?--add?nginx
設置開機啟動chkconfig?nginx?on
重啟, 查看nginx服務是否自動啟動shutdown?-h?0?-r
netstat?-apn|grep?nginx
對nginx服務執行停止/啟動/重新讀取配置文件操作#啟動nginx服務
systemctl?start?nginx.service
#停止nginx服務
systemctl?stop?nginx.service
#重啟nginx服務
systemctl?restart?nginx.service
#重新讀取nginx配置(這個最常用,?不用停止nginx服務就能使修改的配置生效)
systemctl?reload?nginx.service