Linux下製作Nginx綠色免安裝包

前言

linux下安裝nginx比較繁瑣,遇到內網部署環境更是麻煩,所以研究了下nginx綠色免安裝版的部署包製作,開箱即用,特此記錄分享,一下操作在centos8環境下安裝,如果需要其他內核系統的安裝(Debian/Ubuntu等),請在對應環境虛擬機下安裝製作

安裝包製作

安裝依賴

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修改配置

這步主要爲了後續註冊爲系統服務做準備,如果您只需要獲取安裝直接命令或腳本啓動可跳過

如果不修改,註冊爲系統服務後會提示logs文件路徑無法找到

將compile文件中的./configure --prefix=.部分修改爲./configure --prefix=/usr/soft/nginx

執行編譯

bash compile <nginx-version>

例如需要1.25.5版本,則執行語句爲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

腳本註冊服務

腳本製作

將nginx-1.25.5.tar.gz中的內容解壓,獲取到sbin目錄同級的所有內容重新打包成nginx-green.tar壓縮文件

可使用7-Zip壓縮

nginx-green.tar同級目錄創建nginx.sh腳本,腳本內容如下

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"

腳本使用

服務器創建/usr/soft/nginx目錄

把nginx-green.tar和nginx.sh拷貝到/usr/soft/nginx中

進入目錄

cd /usr/soft/nginx

腳本授權

chmod +x nginx.sh

執行註冊

source nginx.sh

服務管理

啓動:systemctl start nginx
查看:systemctl status nginx
停止:systemctl stop nginx
重啓:systemctl reload nginx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章