今天鹹魚給大家分享個源碼編譯安裝 Nginx 的 shell 腳本 這個 shell 腳本可重覆執行 完整源碼放在最後 定義一個變數來存放 nginx 版本號 version=1.15.4 nginx 下載地址:http://nginx.org/download/ 下列函數功能則是判斷當前步驟是否執 ...
今天鹹魚給大家分享個源碼編譯安裝 Nginx 的 shell 腳本
這個 shell 腳本可重覆執行
完整源碼放在最後
-
定義一個變數來存放 nginx 版本號
version=1.15.4
nginx 下載地址:http://nginx.org/download/
-
下列函數功能則是判斷當前步驟是否執行成功,並將結果輸出出來
-
創建 nginx 用戶和用戶組
建議用大於 1000 的 GID 和 UID 號,表示普通用戶
這段代碼里我做了一個條件判斷:
如果在 /etc/passwd
和 /etc/group
文件中過濾出 nginx,表示已經創建了 nginx 用戶和 nginx 用戶組,就不再創建了
-
安裝一些擴展包
-
下載 Nginx 並解壓縮
-
編譯安裝 Nginx
這裡也做了一個條件判斷:
如果 /usr/local/nginx
目錄存在,則說明 nginx 已經成功安裝好了
-
建立軟連接
這一步看個人習慣可做可不做
-
註冊服務
將 nginx 註冊成服務之後就可以使用 systemctl 控制它了
-
內核參數優化
-
啟動 nginx 並設置開機自啟動
-
負責配置寫入的函數
在上面的內核參數優化函數裡面,我並沒有使用 echo 將配置直接重定向到 /etc/sysctl.conf
文件裡面
而是用了 add_config_tofile
函數,第一個參數是配置項,第二個參數是文件名
-
main 函數
-
完整代碼
執行結果如下:
#! /bin/bash version=1.15.4 #判斷函數是否執行成功 function show_result(){ if [ "$1" -eq 0 ] then echo -e "\e[32m$2 is Success . [ OK ] \e[0m" else echo -e "\e[31m$2 is Fail . [ FAIL ] \e[0m" fi } #創建 nginx 用戶和用戶組 function user_create(){ local item="Create User and Group" if [ `cat /etc/{passwd,group} | grep nginx | wc -l ` -ge 2 ]; then echo -e "\e[31mUser and Group exist! \e[0m" else groupadd -g 1004 nginx && \ useradd -u 1004 -g 1004 -M -s /sbin/nologin nginx show_result $? "${item}" fi } #下載一些拓展包 function nginx_pkg(){ local item="Packages Install" yum -y install gcc openssl-devel pcre-devel zlib-devel > /dev/null 2>&1 show_result $? "${item}" } #下載nginx function nginx_download(){ local item="Nginx Download" cd /usr/local/src && \ wget http://nginx.org/download/nginx-${version}.tar.gz > /dev/null 2>&1 test -e /usr/local/src/nginx-${version} || tar zxf nginx-${version}.tar.gz rm -rf /usr/local/src/nginx-${version}.tar.gz show_result $? "${item}" } #編譯安裝 function nginx_compile(){ local item="Nginx Compile" cd /usr/local/src/nginx-${version} if [ `ls -l /usr/local/ | grep 'nginx' | wc -l` -ge 1 ]; then echo -e "\e[31mNginx exist! \e[0m" else ./configure --prefix=/usr/local/nginx > /dev/null 2>&1 && make > /dev/null 2>&1 && make install > /dev/null 2>&1 fi show_result $? "${item}" } #軟連接建立 function nginx_softlink(){ local item="Nginx Softlink" test -d /etc/nginx/ || ln -s /usr/local/nginx/conf/ /etc/nginx test -e /usr/sbin/nginx || ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ show_result $? "${item}" } #註冊服務 function nginx_service(){ local item="Nginx Service" test -e /usr/lib/systemd/system/nginx.service || \ echo ' [Unit] Description=The nginx HTTP and reverse proxy server After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid # Nginx will fail to start if /run/nginx.pid already exists but has the wrong # SELinux context. This might happen when running `nginx -t` from the cmdline. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621ExecStartPre=/usr/bin/rm-f /usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true ' > /usr/lib/systemd/system/nginx.service systemctl daemon-reload show_result $? "${item}" } #內核優化 function nginx_kernel(){ local item="Optimize Kernel Arguments" cp /etc/sysctl.conf /etc/sysctl.conf.${current_time} > /dev/null 2>&1 arch_ratio=$([[ ! -z $(uname -a | grep x86_64) ]] && expr 64 / 32 || expr 32 / 32) memory_size=$(free -b| awk 'NR==2{print $2}') nf_conntrack_size=$(expr ${memory_size} / 16384 / ${arch_ratio}) #開啟反向路徑過濾 add_config_tofile "net.ipv4.conf.default.rp_filter = 1" /etc/sysctl.conf add_config_tofile "net.ipv4.conf.all.rp_filter = 1" /etc/sysctl.conf #處理無源路由包 add_config_tofile "net.ipv4.conf.all.accept_source_route = 0" /etc/sysctl.conf add_config_tofile "net.ipv4.conf.default.accept_source_route = 0" /etc/sysctl.conf #core文件名中添加pid作為擴展名 add_config_tofile "kernel.core_uses_pid = 1" /etc/sysctl.conf #開啟syn洪水攻擊保護 add_config_tofile "net.ipv4.tcp_syncookies = 1" /etc/sysctl.conf #修改消息隊列長度 add_config_tofile "kernel.msgmnb = 65536" /etc/sysctl.conf add_config_tofile "kernel.msgmax = 65536" /etc/sysctl.conf #修改最大記憶體共用段大小bytes add_config_tofile "kernel.shmmax = 68719476736" /etc/sysctl.conf add_config_tofile "kernel.shmall = 4294967296" /etc/sysctl.conf #timewait數量預設18000 add_config_tofile "net.ipv4.tcp_max_tw_buckets = 600" /etc/sysctl.conf add_config_tofile "net.ipv4.tcp_sack = 1" /etc/sysctl.conf add_config_tofile "net.ipv4.tcp_window_scaling = 1" /etc/sysctl.conf add_config_tofile "net.ipv4.tcp_rmem = 4096 87380 16777216" /etc/sysctl.conf add_config_tofile "net.ipv4.tcp_wmem = 4096 65536 16777216" /etc/sysctl.conf add_config_tofile "net.core.rmem_default = 8388608" /etc/sysctl.conf add_config_tofile "net.core.wmem_max = 16777216" /etc/sysctl.conf #未收到客戶端確認信息連接請求的最大值 add_config_tofile "net.ipv4.tcp_max_syn_backlog = 262144" /etc/sysctl.conf #放棄建立連接之前發送的synack包 add_config_tofile "net.ipv4.tcp_syn_retries = 2" /etc/sysctl.conf #開啟重用,允許time—wait socket 重新用語新的tcp連接 add_config_tofile "net.ipv4.tcp_tw_reuse = 1" /etc/sysctl.conf add_config_tofile "net.ipv4.tcp_fin_timeout = 1" /etc/sysctl.conf #防止簡單的ddos攻擊 add_config_tofile "net.ipv4.tcp_max_orphans = 3276800" /etc/sysctl.conf #啟用timewait快速收回 add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf #keeptime啟用時tcp發送keepalive消息的頻度,預設2h add_config_tofile "net.ipv4.tcp_keepalive_time = 600" /etc/sysctl.conf #允許系統打開的埠範圍 add_config_tofile "net.ipv4.ip_local_port_range = 1024 65535" /etc/sysctl.conf #資源回收 add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf #路由轉發 add_config_tofile "net.ipv4.ip_forward = 1" /etc/sysctl.conf #修改防火牆連接跟蹤表大小,預設65535 add_config_tofile "net.netfilter.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf add_config_tofile "net.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf #解禁ping add_config_tofile "net.ipv4.icmp_echo_ignore_all = 0" /etc/sysctl.conf modprobe bridge sysctl -p > /dev/null 2>&1 show_result $? "${item}" } #啟動 nginx function nginx_start(){ local item="Nginx start" systemctl enable nginx --now > /dev/null 2>&1 show_result $? "${item}" } #負責寫入配置的函數 function add_config_tofile(){ local keywords=`echo $1| awk -F "[= ]+" '{print $1}'` local SearchResult=`grep "^${keywords}" "$2"` if [ -z "${SearchResult}" ] then echo $1 >> $2 else sed -i "s/^${keywords}.*/$1/" $2 fi } #主函數 function main(){ user_create nginx_pkg nginx_download nginx_compile nginx_softlink nginx_service nginx_kernel nginx_start } main