nginx 在线一键安装
生活随笔
收集整理的这篇文章主要介绍了
nginx 在线一键安装
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一键在线安装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 在线一键安装的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 设计模式【2.2】-- 工厂模式怎么演变
- 下一篇: pool(二)——动手入门