前言
linux下安裝nginx比較繁瑣,遇到內(nèi)網(wǎng)部署環(huán)境更是麻煩,所以研究了下nginx綠色免安裝版的部署包制作,開箱即用,特此記錄分享,一下操作在centos8環(huán)境下安裝,如果需要其他內(nèi)核系統(tǒng)的安裝(Debian/Ubuntu等),請在對應(yīng)環(huán)境虛擬機(jī)下安裝制作
安裝包制作
安裝依賴
yum install gcc-c++ pcre perl git unzip pcre-devel zlib zlib-devel openssl openssl-devel -y
安裝nginx-portable
wget https://github.com/nuccch/nginx-portable/archive/master.zip;unzip master.zip
cd nginx-portable-master/
configure修改配置
這步主要為了后續(xù)注冊為系統(tǒng)服務(wù)做準(zhǔn)備,如果您只需要獲取安裝直接命令或腳本啟動可跳過
如果不修改,注冊為系統(tǒng)服務(wù)后會提示logs文件路徑無法找到
將compile文件中的./configure --prefix=.部分修改為./configure --prefix=/usr/soft/nginx
執(zhí)行編譯
bash compile <nginx-version>
例如需要1.25.5版本,則執(zhí)行語句為bash compile 1.25.5
獲取安裝包
編譯完成后在build目錄會生成nginx-1.25.5.tar.gz綠色免安裝版,直接解壓即可使用,示例命令如下
啟動:cd /nginx/sbin && ./nginx
停止:./nginx -s stop
重啟:./nginx -s reload
檢查配置正確性:./nginx -t
查看Nginx版本信息:./nginx -v
腳本注冊服務(wù)
腳本制作
將nginx-1.25.5.tar.gz中的內(nèi)容解壓,獲取到sbin目錄同級的所有內(nèi)容重新打包成nginx-green.tar壓縮文件
可使用7-Zip壓縮
nginx-green.tar同級目錄創(chuàng)建nginx.sh腳本,腳本內(nèi)容如下
echo "start install"
cd /usr/soft/nginx
echo "in unzip" && tar -xvf nginx-green.tar && echo "unzip success"
cat > /etc/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target
[Service]
Type=forking
ExecStart=/usr/soft/nginx/sbin/nginx -c /usr/soft/nginx/conf/nginx.conf
ExecStop=/usr/soft/nginx/sbin/nginx -s stop
ExecReload=/usr/soft/nginx/sbin/nginx -s reload
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
echo "create system service success"
systemctl daemon-reload
echo "reload system service setting success"
systemctl enable nginx
echo "setting nginx startup success"
systemctl start nginx
echo "start nginx success"
腳本使用
服務(wù)器創(chuàng)建/usr/soft/nginx目錄
把nginx-green.tar和nginx.sh拷貝到/usr/soft/nginx中
進(jìn)入目錄
cd /usr/soft/nginx
腳本授權(quán)
chmod +x nginx.sh
執(zhí)行注冊
source nginx.sh
服務(wù)管理
啟動:systemctl start nginx
查看:systemctl status nginx
停止:systemctl stop nginx
重啟:systemctl reload nginx