一键在线安装nginx服务器,可选择指定nginx版本安装,默认安装的版本是:nginx-1.20.2 ;若本地 /tpm 目录不存在目标版本的nginx源码压缩包,脚本会自动从nginx官网下载目标版本的nginx安装。

一键在线安装既优化了Linux系统内核,还做了nginx 一般性常用的配置优化。

#!/bin/bash# nginx 一键在线安装脚本
# 2023-04-25
# create by tuduouset -e#====================================================
# 用户可自定义参数部分,不定义则使用默认值# 默认安装路径#install_root=/usr/local# nginx 默认端口# nginx_port=80 # 默认安装版本#nginx_version=nginx-1.20.2#====================================================# 确认是在Centos 7 使用 root 用户执行
check(){# 确认是 Centos7 系统systemver=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`echo $systemverif [[ $systemver != "7" ]];thenecho "请在Centos7系统执行脚本,请检查系统版本,终止作业"exit 1fi# 确认执行用户是 rootif [[ $(id -u --name) != "root" ]];thenecho "请使用 root 用户登录,执行脚本;请检查执行用户,终止作业"exit 1fi# 已运行 nginx ,则退出安装if [[ `ps -ef |grep -E "nginx: master" |wc -l` -ge 2 ]];thenecho "已有运行的 nginx,将退出安装"exit 1fi
}## Linux内核优化
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}# 当前 temp 目录不存在目标版本nginx,则从官网下载 nginx
Downloadnginx(){# 未指定安装目录,则默认安装在: /usr/localif [ -z ${install_root} ];theninstall_root=/usr/localelseinstall_root=${install_root}mkdir -p ${install_root}fiif [ -z ${nginx_version} ];thenecho "默认安装的 Nginx 版本: nginx-1.20.2"nginx_version=nginx-1.20.2else nginx_version=${nginx_version}fiif [ -z ${download_url} ];thenecho "默认从 nginx 官网下载"download_url="http://nginx.org/download"else download_url=${download_url}fiif [ ! -d temp ];thenmkdir -p tempfiif [ ! -e temp/${nginx_version}.tar.gz ];then# 没有 wget 则下载which wget || yum install wget -ywget ${download_url}/${nginx_version}.tar.gz -P tempfi
}Installnginx(){# 使用国内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 启动服务
Nginxsystemd(){##  备份旧的 nginx 启动文件 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# 添加开机启动systemctl daemon-reloadsystemctl enable nginx}# nginx 主配置文件
Nginxconf(){# 未指定端口,则使用默认端口: 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服务# 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 服务器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;  # 增加访问控制#      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;##  # # 下边的这种写法,需要引入第三方的健康检查模块#  # 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;   #指向证书文件#      ssl_certificate_key   pki/test.key;  #指向证书文件#      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} # 切换回脚本执行目录,切换前目录为:  temp/${nginx_version}# nginx  运行状态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# 默认获取服务器第一个网卡的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"
}# 执行脚本
# check
Initsystem
Downloadnginx
Installnginx
Nginxsystemd
Nginxconf
Nginxinfo

nginx 在线一键安装相关推荐

  1. 电脑在线一键安装win10系统教程分享

    当我们解决电脑故障或想要安装新的操作系统时,就可以用重装系统方法来实现.但是重装系统对初学者来说是相对于陌生并且复杂的.然而重装系统的方法有很多,可以一键安装,U盘启动方法安装甚至是硬盘光驱安装等等. ...

  2. 在线计算机安装系统,电脑系统在线一键安装软件

    随着生活水平的提高和科技的发展,如今无论办公还是生活都是越来越离不开电脑,有些用户想要装电脑系统不知道用什么软件装比较安全,在线一键安装电脑系统软件推荐云骑士装机大师,下面以云骑士装win7系统为例, ...

  3. MySQL 在线一键安装

    使用说明: 自定义数据库账号.密码和数据库安装目录.脚本最后输出的是主机第一张网卡的地址,实际可用连接地址要根据自己实际情况,尤其是云服务器. 安装包来源于华为云,可自行指定华为云支持下载的MySQL ...

  4. tomcat 在线一键安装

    说明:tomcat一键在线安装脚本,默认使用国内源下载,下载速度极快,默认安装在/opt 目录,默认安装端口为 8080,安装的默认版是 apache-tomcat-8.5.88.tar.gz . 1 ...

  5. 简单易懂的笔记本电脑一键安装win10系统教程

    虽然win11系统已经出来了,但是其实许多朋友还是喜欢使用win10系统,而且一般电脑的配置都支持win10系统的在线安装.那么笔记本电脑怎么在线一键安装win10系统呢?下面小编就给大家带来简单易懂 ...

  6. Nginx配置在线一键生成“神器”,不用愁了

    点击蓝色"java版web项目"关注我哟 加个"星标",优质文章,第一时间送达 上一篇:这300G的Java资料是我师傅当年给我的,免费分享给大家 下一篇:这2 ...

  7. 在线一键生成网页版 Nginx 配置文件

    1 在线一键生成网页版 Nginx 配置文件 介绍 NginxWebUI是一款方便实用的 nginx 网页配置工具,可以使用 WebUI 配置 Nginx 的各项功能,包括端口转发,反向代理,ssl ...

  8. shell脚本一键安装nginx

        使用shell脚本一键安装Nginx 操作环境Centos7,联网进行安装. 代码如下: #!/bin/bash #this is nginx.sh #date 2018.10.6 [ $(i ...

  9. centos8中一键安装Nginx

    1:检查是否安装 [root@aa ~]# nginx -v -bash: nginx: command not found 2:一键安装yum install -y nginx [root@aa ~ ...

最新文章

  1. Python-OpenCV 处理图像(八):图像二值化处理
  2. 新开了微博,小伙伴们可以关注下哦
  3. 使用临界段实现优化的进程间同步对象-原理和实现
  4. 20万RMB!学数据分析的朋友一定要看!
  5. 笨方法“学习python笔记之字典
  6. 【AI视野·今日Robot 机器人论文速览 第二十八期】Wed, 1 Dec 2021
  7. Zen Garden驾到:首批Metal游戏已登录iTunes应用商店
  8. Python中的快捷键和注释方式
  9. Excel GET.DOCUMENT说明
  10. iOS端Mock GPS定位 —— 测试、开发、玩游戏、发朋友圈等等,你都用得上(转)
  11. js 图表处理之Echar
  12. Ubuntu默认防火墙UFW命令大全
  13. 蓝牙BLE芯片PHY6222之SPI驱动ST7789
  14. 计算机系统组成复习及CRC循环冗余校验码计算
  15. 微积分:2.1导数中的中值定理
  16. 什么是单工,半双工,全双工
  17. checking whether we are cross compiling... configure: error: in `/tmp/pip-build-rktAki/pycrypto解决方法
  18. Java中hasNext()的作用
  19. opencv自动裁切_荐Python Opencv 之 简单的视频裁剪功能的实现
  20. 来,吃了许嵩这颗毒药

热门文章

  1. h264 i p 帧特点
  2. 大专适合学习php么_学习php有没有学历要求
  3. Intellij Error:java: Compilation failed: internal java compiler error
  4. 阿里云发布黑科技:面对海量的文本翻译任务,阿里翻译团队是如何解决的
  5. 今日早报 每日精选12条新闻简报 每天一分钟 知晓天下事 9月24日
  6. BZOJ3168: [Heoi2013]钙铁锌硒维生素
  7. springbooot使用google验证码
  8. OpenShift 4 - 使用 Debezium 捕获变化数据,实现MySQL到PostgreSQL数据库同步(附视频)
  9. sqlserver+asp.net+devextreme从零开始(1)
  10. HTML+CSS详细知识点(下)