Nginx原始碼編譯安裝

2020-09-19 14:00:37

獲取Nginx軟體包 ==>(文章末尾有指令碼一鍵安裝,含獲取軟體包)

官網網站:http://www.nginx.org/社群版或http://www.nginx.com/企業版

原始碼編譯安裝Nginx軟體

回顧原始碼編譯三步走:

① ./configure設定

② make編譯

③ make install安裝

第一步:上傳Nginx軟體包到Linux伺服器端

第二步:聯網,安裝Nginx軟體所需的依賴庫

# yum install pcre-devel zlib-devel openssl-devel -y

第三步:對Nginx軟體包進行解壓縮操作

# tar -xf nginx-1.18.0.tar.gz

第四步:建立一個www賬號

# useradd -r -s /sbin/nologin www

第五步:使用./configure對Nginx軟體進行設定(對軟體安裝包進行設定)

# cd nginx-1.18.0
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module

編譯引數說明

引數 作用
–prefix 編譯安裝到的軟體目錄
–user worker程序執行使用者
–group worker程序執行使用者組
–with-http_ssl_module 支援https 需要pcel-devel依賴

第六步:編譯與安裝Nginx軟體

# make && make instal

Nginx目錄介紹

目錄 作用
conf 組態檔(nginx.conf)
html 網站預設目錄(類似apache的htdocs目錄)
logs 紀錄檔(access.log、error.log)
sbin 可執行檔案 [軟體的啟動 停止 重新啟動等]

原生啟動方式:

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

原生關閉方式:

# sbin/nginx -s stop

原生重新啟動方式:需要停止Nginx服務,相當於先關閉後開啟

# sbin/nginx -s stop
# sbin/nginx -c /usr/local/nginx/conf/nginx.conf

原生的熱過載(不停止Nginx服務,過載nginx.conf組態檔)

# sbin/nginx -s reload

☆ Nginx服務設定

CentOS7.6 設定:

使用前提,必須先把Nginx停止掉!!!!!!!!

# sbin/nginx -s stop

編寫nginx.service指令碼,有了這個指令碼,我們就可以使用systemctl對其進行控制了

# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=Nginx Web Server
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

啟動Nginx:

# systemctl start nginx

停止Nginx:

# systemctl stop nginx

過載Nginx:

# systemctl reload nginx

開啟啟動與開機不啟動:

# systemctl enable nginx
# systemctl disable nginx

shell指令碼安裝Nginx

#!/bin/bash

wget http://nginx.org/download/nginx-1.18.0.tar.gz &>/dev/null
sleep 1
tar -xf nginx-1.18.0.tar.gz &>/dev/null
sleep 1
yum -y install gcc
yum -y install gcc-c++
yum -y install make
yum -y install pcre-devel
yum -y install zlib-devel
yum -y install openssl-devel
sleep 1
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx
sleep 1
make && make install
sleep 1
cd /usr/local/nginx
sbin/nginx
ps -ef | grep nginx

#shell指令碼就是命令的堆砌