nginx 在线一键安装
生活随笔
收集整理的這篇文章主要介紹了
nginx 在线一键安装
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
???????? 一鍵在線安裝nginx服務(wù)器,可選擇指定nginx版本安裝,默認(rèn)安裝的版本是:nginx-1.20.2 ;若本地 /tpm 目錄不存在目標(biāo)版本的nginx源碼壓縮包,腳本會(huì)自動(dòng)從nginx官網(wǎng)下載目標(biāo)版本的nginx安裝。
??????? 一鍵在線安裝既優(yōu)化了Linux系統(tǒng)內(nèi)核,還做了nginx 一般性常用的配置優(yōu)化。
#!/bin/bash# nginx 一鍵在線安裝腳本 # 2023-04-25 # create by tuduouset -e#==================================================== # 用戶可自定義參數(shù)部分,不定義則使用默認(rèn)值# 默認(rèn)安裝路徑#install_root=/usr/local# nginx 默認(rèn)端口# nginx_port=80 # 默認(rèn)安裝版本#nginx_version=nginx-1.20.2#====================================================# 確認(rèn)是在Centos 7 使用 root 用戶執(zhí)行 check(){# 確認(rèn)是 Centos7 系統(tǒng)systemver=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`echo $systemverif [[ $systemver != "7" ]];thenecho "請(qǐng)?jiān)贑entos7系統(tǒng)執(zhí)行腳本,請(qǐng)檢查系統(tǒng)版本,終止作業(yè)"exit 1fi# 確認(rèn)執(zhí)行用戶是 rootif [[ $(id -u --name) != "root" ]];thenecho "請(qǐng)使用 root 用戶登錄,執(zhí)行腳本;請(qǐng)檢查執(zhí)行用戶,終止作業(yè)"exit 1fi# 已運(yùn)行 nginx ,則退出安裝if [[ `ps -ef |grep -E "nginx: master" |wc -l` -ge 2 ]];thenecho "已有運(yùn)行的 nginx,將退出安裝"exit 1fi }## Linux內(nèi)核優(yōu)化 Initsystem(){ cat >> /etc/security/limits.conf << EOF## ulimit settings ##soft nofile 65536hard nofile 65536soft nproc 65536hard nproc 65536## ulimit settings ## EOFcat >> /etc/sysctl.conf << EOF## edit /etc/sysctl.conf ##kernel.shmall = 2043878 kernel.shmmax = 8175512 kernel.shmmni = 4096 kernel.sem = 250 32000 100 128fs.file-max = 65536net.ipv4.ip_local_port_range = 1024 65000net.core.rmem_default = 262144net.core.rmem_max = 262144net.core.somaxconn = 2048net.core.wmem_default = 262144net.core.wmem_max = 262144net.ipv4.tcp_rmem = 262144net.ipv4.tcp_wmem = 262144## edit /etc/sysctl.conf ## EOFsysctl -p}# 當(dāng)前 temp 目錄不存在目標(biāo)版本nginx,則從官網(wǎng)下載 nginx Downloadnginx(){# 未指定安裝目錄,則默認(rèn)安裝在: /usr/localif [ -z ${install_root} ];theninstall_root=/usr/localelseinstall_root=${install_root}mkdir -p ${install_root}fiif [ -z ${nginx_version} ];thenecho "默認(rèn)安裝的 Nginx 版本: nginx-1.20.2"nginx_version=nginx-1.20.2else nginx_version=${nginx_version}fiif [ -z ${download_url} ];thenecho "默認(rèn)從 nginx 官網(wǎng)下載"download_url="http://nginx.org/download"else download_url=${download_url}fiif [ ! -d temp ];thenmkdir -p tempfiif [ ! -e temp/${nginx_version}.tar.gz ];then# 沒(méi)有 wget 則下載which wget || yum install wget -ywget ${download_url}/${nginx_version}.tar.gz -P tempfi }Installnginx(){# 使用國(guó)內(nèi)yum源和安裝依賴if [ -e /etc/yum.repos.d/CentOS-Base.repo ];thenmv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo-$(date +%Y%m%d%H%M%S)fiwget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repoyum clean all && yum makecacheyum -y install gcc-c++ pcre-devel pcre openssl-devel unzip zip patch make tee net-tools# 安裝 nginx# 若已安裝 nginx 則備份if [ -d ${install_root}/${nginx_version} ];thenmv ${install_root}/${nginx_version} ${install_root}/${nginx_version}-$(date +%Y%m%d%H%M%S)fitar -xvf temp/${nginx_version}.tar.gz -C temp# 編譯安裝nginxcd temp/${nginx_version}./configure --prefix=${install_root}/${nginx_version} \--modules-path=${install_root}/${nginx_version}/modules \--with-stream --with-stream=dynamic \--with-http_sub_module --with-http_stub_status_module \--with-http_ssl_module --with-poll_module --with-http_gunzip_module \--with-http_gzip_static_module --with-http_realip_module \--with-http_dav_module --with-threads --with-pcremake && make install# make -j 4 && make install# 若存在舊的nginx,則備份if [[ -d /usr/local/nginx || -L /usr/local/nginx ]];thenmv /usr/local/nginx /usr/local/nginx-$(date +%Y%m%d%H%M%S)filn -s ${install_root}/${nginx_version} /usr/local/nginx}# nginx 啟動(dòng)服務(wù) Nginxsystemd(){## 備份舊的 nginx 啟動(dòng)文件 if [ -f /usr/lib/systemd/system/nginx.service ];thenmv /usr/lib/systemd/system/nginx.service /usr/lib/systemd/system/nginx.service-$(date +%Y%m%d%H%M%S)ficat > /usr/lib/systemd/system/nginx.service << EOF[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStartPre=${install_root}/${nginx_version}/sbin/nginx -tExecStart=${install_root}/${nginx_version}/sbin/nginx -c ${install_root}/${nginx_version}/conf/nginx.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true[Install]WantedBy=multi-user.target EOF# 添加開(kāi)機(jī)啟動(dòng)systemctl daemon-reloadsystemctl enable nginx}# nginx 主配置文件 Nginxconf(){# 未指定端口,則使用默認(rèn)端口: 80if [ -z ${nginx_port} ];thennginx_port=80elsenginx_port=${nginx_port}fi# nginx 配置文件cat > ${install_root}/${nginx_version}/conf/nginx.conf << EOFuser root;worker_processes auto;#load_module modules/ngx_stream_module.so;events {use epoll;multi_accept on;worker_connections 61024;}# ##此處代理tcp服務(wù)# stream {# include stream.d/*.conf;# log_format proxy '\$remote_addr [\$time_local] '# '\$protocol \$status \$bytes_sent \$bytes_received '# '\$session_time "\$upstream_addr" '# '"\$upstream_bytes_sent" "\$upstream_bytes_received" "\$upstream_connect_time"';## access_log logs/tcp-access.log proxy ;# open_log_file_cache off;# }http {include mime.types;default_type application/octet-stream;log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" ''\$status \$body_bytes_sent "\$http_referer" ''"\$http_user_agent" "\$http_x_forwarded_for" "\$upstream_response_time"';access_log logs/access.log main;#隱藏nginx版本信息server_tokens off;charset utf-8;sendfile on;#tcp_nopush on;tcp_nodelay on;gzip on;gzip_buffers 4 8k;gzip_comp_level 2;gzip_min_length 1000;gzip_types text/plain text/json text/css application/x-httpd-php application/json application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript image/png image/jpg image/jpeg image/gif image/bmp;keepalive_timeout 600;proxy_set_header Cookie \$http_cookie;proxy_set_header X-Real-IP \$remote_addr;proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto \$scheme;proxy_set_header X-NginX-Proxy true;#proxy_set_header tenantAlias test; #我的需要用到租戶編碼client_max_body_size 2048m;client_body_buffer_size 256k;proxy_connect_timeout 1800;proxy_send_timeout 1800;proxy_read_timeout 1800;proxy_buffering on;proxy_buffer_size 256k;proxy_buffers 4 256k;proxy_busy_buffers_size 256k;proxy_temp_file_write_size 256k;proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;proxy_max_temp_file_size 2048m;proxy_http_version 1.1;proxy_set_header Connection "";send_timeout 1800;#proxy_cookie_path / "/; httponly ";# Web 服務(wù)器server {# listen 80;listen ${nginx_port};server_name example.com;root html;index index.html;location / {try_files \$uri \$uri/ /index.html;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}# # 反向代理# server {# listen 8080; # server_name localhost; # # example-1# location / {# allow 192.168.51.0/24; # 增加訪問(wèn)控制# deny all;# proxy_pass http://example-1;# proxy_connect_timeout 500s;# proxy_read_timeout 500s;# proxy_send_timeout 500s;# proxy_set_header Host \$host:\$server_port;# proxy_set_header X-Real-IP \$remote_addr;# proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;# }# }## upstream example-1 {# server 192.168.51.101:8848 max_fails=1 fail_timeout=20s;## # # 下邊的這種寫(xiě)法,需要引入第三方的健康檢查模塊# # server 192.168.51.101:8848;# # check interval=3000 rise=2 fall=5 timeout=1000 type=tcp port=8848;# }# # # https # server {# listen 443 ssl;# ssl_certificate pki/test.crt; #指向證書(shū)文件# ssl_certificate_key pki/test.key; #指向證書(shū)文件# server_name example.com; #此處可配置域名# location / {# proxy_pass http://127.0.0.1:80;# proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;# proxy_set_header Host \$host:\$server_port;# proxy_set_header X-Real-IP \$remote_addr;# proxy_set_header https 1;# proxy_connect_timeout 1500s;# proxy_read_timeout 1500s;# proxy_send_timeout 1500s;# # access_log off;# }# }# 指定其他配置文件目錄 # include conf.d/*.conf;} EOFsystemctl restart nginx}Nginxinfo(){cd ${OLDPWD} # 切換回腳本執(zhí)行目錄,切換前目錄為: temp/${nginx_version}# nginx 運(yùn)行狀態(tài)if [[ `ps -ef |grep -E "nginx: master" |wc -l` -ge 2 ]];thennginx_status=runningecho "nginx is running ..."elsenginx_status=stopedecho "nginx is stoped ..."echo "please check your nginx"fi# 默認(rèn)獲取服務(wù)器第一個(gè)網(wǎng)卡的IPHostIP=`ifconfig |grep inet|grep -oP "\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}"| grep -vE "127.0.0.1|^255"|head -n 1`echo "Nginx info: " |tee temp/install.logecho "nginx nginx_version: ${nginx_version}" |tee -a temp/install.logecho "nginx HostIP: $HostIP" |tee -a temp/install.logecho "nginx nginx_port: ${nginx_port}" |tee -a temp/install.logecho "nginx install_root: ${install_root}" |tee -a temp/install.logecho "nginx nginx_home: ${install_root}/${nginx_version}" |tee -a temp/install.logecho "nginx status: ${nginx_status}" | tee -a temp/install.logecho -e "\e[33m"cat temp/install.logecho -e "\e[0m" }# 執(zhí)行腳本 # check Initsystem Downloadnginx Installnginx Nginxsystemd Nginxconf Nginxinfo總結(jié)
以上是生活随笔為你收集整理的nginx 在线一键安装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 设计模式【2.2】-- 工厂模式怎么演变
- 下一篇: pool(二)——动手入门