nginx伺服器安裝啟動及配置檔案詳解
1.安裝Nginx
1.1 選擇穩定nginx版本
centos的yum不提供nginx安裝,通過配置官方yum源的方式獲取到的也只是原始碼包。所以我們找到了Nginx官網看下官方提供的安裝方式:Nginx原始碼包下載的官網地址(http://nginx.org/en/download.html)
從官網上提供三個型別的版本,分別是Mainline version、Stable version、Legacy versions
- Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
- Stable version:最新穩定版,生產環境上建議使用的版本
- Legacy versions:遺留的老版本的穩定版
在這裡我們選擇Stable 版本的 nginx-1.12.1.tar.gz
安裝環境是CentOS 6.5 安裝過程所執行的命令需要root許可權,所以我們選擇使用root使用者安裝。
1.2 安裝依賴包和必要的庫
1.2.1 安裝依賴包
# yum -y install gcc gcc-c++ make libtool
這些依賴包是編譯所必需的,如果yum上沒有的話可以去下載原始碼來安裝
1.2.2 安裝必要的庫
這些庫都是nginx中一些功能模組所依賴的,如without-http_gzip_module需要zlib庫來構建和執行此模組,http_rewrite_module需要pcre庫來構建和執行此模組,http_ssl_module需要OpenSSL庫來構建和執行此模組
1.安裝pcre庫(版本8.40)
# cd /usr/local/
# wget http://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
# tar -zxvf pcre-8.40.tar.gz
# cd pcre-8.40
# ./configure --enable-utf8 --enable-unicode-properties
# make && make install
2.安裝zlib庫(版本1.2.11)
# cd /usr/local/
# wget http://www.zlib.net/zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
# cd zlib-1.2.11
# ./configure
# make && make install
3.安裝OpenSSL(版本1.1.0e)
# cd /usr/local/
# wget http://www.openssl.org/source/openssl-1.1.0e.tar.gz
# tar -zxvf openssl-1.1.0e.tar.gz
# cd openssl-1.1.0e
# ./config
# make && make install
1.3 編譯安裝Nginx
1.3.1 nginx編譯選項詳解
# cd /usr/local/
# wget http://nginx.org/download/nginx-1.12.1.tar.gz
# tar -zxvf nginx-1.12.1.tar.gz
# cd nginx-1.12.1.tar
# ./configure --help
可以在編譯時./configure –help列出大部分常用模組和編譯選項,列出的編譯選項中以–without開頭的都預設安裝,以PATH結尾的需要手動指定依賴庫原始碼目錄。
# ./configure –help
–help print this message
–prefix=PATH set installation prefix
–sbin-path=PATH set nginx binary pathname
–with-select_module enable select module
–without-select_module disable select module
–with-poll_module enable poll module
……
下面是編譯選項的說明
--prefix=PATH
: 指定nginx的安裝目錄。預設 /usr/local/nginx--sbin-path=PATH
:設定nginx可執行檔案的名稱。預設情況下,檔案指向 安裝目錄/sbin/nginx--conf-path=PATH
:設定nginx.conf配置檔案的名稱。nginx允許使用不同的配置檔案啟動,通過在命令列引數中指定它 。預設情況下,檔案指向 安裝目錄/conf/nginx.conf--pid-path=PATH
:設定儲存主程序ID檔案nginx.pid的名稱。預設情況下,檔案指向 安裝目錄/logs/nginx.pid--error-log-path=PATH
:設定錯誤,警告和診斷檔案的名稱。預設情況下,檔案指向 安裝目錄/logs/error.log--http-log-path=PATH
:設定HTTP伺服器的請求日誌檔案的名稱。預設情況下,檔案指向 安裝目錄/logs/access.log--lock-path=PATH
:安裝檔案鎖定,防止安裝檔案被別人利用,或自己誤操作。--user=nginx
:指定程式執行時的非特權使用者。安裝完成後,可以隨時在nginx.conf配置檔案更改user。預設使用者名稱為nobody--group=nginx
:指定程式執行時的非特權使用者所在組名稱。預設情況下,組名稱設定為非root使用者的名稱。--with-http_realip_module
啟用ngx_http_realip_module
支援(這個模組允許從請求標頭更改客戶端的IP地址值,預設為關)--with-http_ssl_module
:啟用ngx_http_ssl_module
支援(使支援https請求,需已安裝openssl)--with-http_stub_status_module
:啟用ngx_http_stub_status_module
支援(獲取nginx自上次啟動以來的工作狀態)--with-http_gzip_static_module
:啟用ngx_http_gzip_module
支援(該模組同–without-http_gzip_module功能一樣)--http-client-body-temp-path=PATH
:設定http客戶端請求臨時檔案路徑--http-proxy-temp-path=PATH
:設定http代理臨時檔案路徑--http-fastcgi-temp-path=PATH
:設定http fastcgi臨時檔案路徑--http-uwsgi-temp-path=PATH
:設定http scgi臨時檔案路徑--with-pcre
:設定pcre庫的原始碼路徑,如果已通過yum方式安裝,使用–with-pcre自動找到庫檔案。使用--with-pcre=PATH
時,需要從PCRE網站下載pcre庫的原始碼(版本8.4)並解壓,剩下的就交給Nginx的./configure和make來完成。perl正則表示式使用在location指令和ngx_http_rewrite_module
模組中。--with-zlib=PATH
:指定 zlib(版本1.2.11)的原始碼解壓目錄。在預設就啟用的網路傳輸壓縮模組ngx_http_gzip_module時需要使用zlib--with-http_ssl_module
:使用https協議模組。預設情況下,該模組沒有被構建。前提是openssl已安裝--add-module=PATH
: 新增第三方外部模組,如nginx-sticky-module-ng
或快取模組。每次新增新的模組都要重新編譯(Tengine可以在新加入module時無需重新編譯)
完整編譯指令
./configure \
--prefix=/usr/local/nginx-1.12.1 \
--sbin-path=/usr/local/nginx-1.12.1/sbin/nginx \
--conf-path=/usr/local/nginx-1.12.1/conf/nginx.conf \
--pid-path=/usr/local/nginx-1.12.1/logs/nginx.pid \
--lock-path=/usr/local/nginx-1.12.1/logs/nginx.lock \
--error-log-path=/usr/local/nginx-1.12.1/logs/error.log \
--http-log-path=/usr/local/nginx-1.12.1/logs/access.log \
--user=nginx \
--group=nginx \
--with-poll_module \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-zlib=/usr/local/zlib-1.2.11 \
--with-pcre=/usr/local/pcre-8.40 \
--with-openssl=/usr/local/openssl-1.1.0e \
--with-pcre-jit \
1.3.2 nginx安裝
# make && make install
1.4 啟動Nginx
1.4.1 檢查配置檔案是否正確
注意,啟動前和修改了配置檔案nginx.conf後最好先檢查一下修改過的配置檔案是否正確,以免重啟後Nginx出現錯誤影響伺服器穩定執行。判斷Nginx配置是否正確命令如下:
# /usr/nginx-1.12.1/sbin/nginx -t
1.4.2 啟動
# useradd nginx #建立nginx使用者
# /usr/nginx-1.12.1/sbin/nginx # 預設配置檔案 conf/nginx.conf, -c指定其他配置檔案
啟動成功會在logs目錄中出現nginx.pid檔案,即nginx程序號
若未成功可以檢視logs目錄下的error.log檔案檢視報錯日誌
1.4.3 平滑重啟nginx
如果更改了配置就要重啟Nginx,要先關閉Nginx再開啟?不是的,可以向Nginx 傳送訊號,平滑重啟。
/usr/nginx-1.12.1/sbin/nginx -s reload
或
kill -HUP 程序號或程序號檔案路徑
1.4.4 停止Nginx
停止操作是通過向nginx程序傳送訊號來進行的
步驟1:查詢nginx主程序號
ps -ef | grep nginx
步驟2:傳送訊號
1.正常關閉
kill -QUIT 主程序號
2.快速停止Nginx:
kill -TERM 主程序號
3.強制停止Nginx:
pkill -9 nginx
1.4.5 Nginx服務自啟
把nginx新增到系統服務中,使其可以使用service nginx start/stop/restart等。
建立一個名稱為nginx的指令碼,新增如下內容,更改裡面的nginx路徑和配置檔案路徑,給執行許可權。
如果想新增指令碼用service啟動,必須要腳本里麵包含這2行
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
以下是寫好的指令碼
#!/bin/sh
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxyand 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/local/nginx-1.12.1/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx-nginx-1.12.1/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
killall -9 nginx
}
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 && exit0
$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
cp nginx /etc/init.d/
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig -level 35 nginx
測試啟動
service nginx start
service nginx stop
service nginx reload
參考:
下一篇。。。nginx.conf配置檔案詳解