Nginx安裝及配置
開源Nginx官網地址(https://nginx.org)
Nginx源碼包下載地址(https://nginx.org/en/download.html)
-
Mainline version 主線版本
-
Stable version 穩定版本
-
Legacy versions 陳舊版本
源碼安裝
在安裝操作系統的安裝軟件配置部分,建議選擇“Server with GUI”,并選擇“Development Tools”和“Compatibility Libraries”兩項附加軟件。確保gcc、libgcc、gcc-c++等編譯器已經正確安裝。
在安裝Nginx之前,需要安裝一些Nginx的依賴程序,Nginx的主要依賴程序有zlib、pcre、openssl三個,其中,zlib用于支持gzip模塊,pcre用于支持rewrite模塊,openssl用于支持ssl功能,為了簡單快捷,推薦通過yum安裝zlib、pcre、openssl軟件包。
安裝方式如下:
yum -y install zlib pcre pcre-devel openssl openssl-devel# 如果安裝的操作系統為最小化,那么可以使用yum安裝一下軟件包組
# 查看軟件包組列表
yum grouplist
# 下載“Development Tools”和“Compatibility Libraries”兩項附加軟件
yum -y groupinstall "Development Tools"
yum -y groupinstall "Compatibility Libraries"
-
檢測編譯環境并配置安裝規則
解壓文件
tar -xf nginx-1.26.2.tar.gz
進入目錄
cd nginx-1.26.2
開始檢測
./configure
–prefix=/usr/local/nginx
–sbin-path=/usr/local/nginx/sbin/nginx
–conf-path=/usr/local/nginx/conf/nginx.conf
–error-log-path=/usr/local/nginx/logs/error.log
–http-log-path=/usr/local/nginx/logs/access.log
–pid-path=/usr/local/nginx/logs/nginx.pid
–with-http_stub_status_module
–with-http_ssl_module
–with-http_gzip_static_module
–with-pcre -
# 基本配置,夠用# 以下路徑均為默認路徑,可自行更改# 指定程序安裝路徑--prefix=/usr/local/nginx# 指定二進制文件路徑--sbin-path=/usr/local/nginx/sbin/nginx# 指定配置文件路徑--conf-path=/usr/local/nginx/conf/nginx.conf# 指定報錯日志文件路徑--error-log-path=/usr/local/nginx/logs/error.log# 指定訪問日志文件路徑--http-log-path=/usr/local/nginx/logs/access.log# 指定進程號文件路徑--pid-path=/usr/local/nginx/logs/nginx.pid# 安裝用來監控Nginx狀態的模塊--with-http_stub_status_module# 啟用Nginx的gzip壓縮--with-http_gzip_static_module# 設置Niginx啟用正則表達式--with-pcre# 啟用Nginx的SSL模塊,此模塊依賴“--with-openssl”這個選項,通常一起使用--with-http_ssl_module# 指定OpenSSL源碼包的路徑,如果編譯的時候沒有指定“--with-openssl”選項,那么會默認使用系統自帶的openssl庫--with-openssl
-
編譯安裝
make
make install
編譯安裝完成后,可以使用nginx命令來查看編譯安裝的配置規則
# -v :顯示版本并退出
# -V :顯示版本和配置選項然后退出
/usr/local/nginx/sbin/nginx -Vnginx version: nginx/1.26.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre
-
編寫啟動腳本
cat < /etc/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true[Install]
WantedBy=multi-user.target
EOF -
重新加載systemd管理器配置
systemctl daemon-reload
-
管理Nginx服務
當我們配置好啟動文件之后,就可以通過systemctl來管理nginx服務了。
# 重載nginx服務
systemctl reload nginx
# 啟動nginx服務
systemctl start nginx
# 關閉nginx服務
systemctl stop nginx
# 設置nginx服務開機自啟動
systemctl enable nginx
# 查看nginx服務運行狀態
systemctl status nginx
# 查看nginx服務是否設置自啟動
systemctl is-enabled nginx
-
使用Nginx
建立軟鏈接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
查看幫助
nginx -h
查看版本信息
nginx -v
查看版本信息及配置選項
nginx -V
檢測配置文件是否有誤
nginx -t
……