#!/bin/bash
# nginx 一键在线安装脚本
# 2024-06-25
# create by xfp
set -e
#====================================================
# 用户可自定义参数部分,不定义则使用默认值
# 默认安装路径
#install_root=/usr/local
# nginx 默认端口
# nginx_port=80
# 默认安装版本
#nginx_version=nginx-1.27.0
#====================================================
# 确认是在Centos 7 使用 root 用户执行
check(){
# 确认是 Centos7 系统
systemver=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
echo $systemver
if [[ $systemver != "7" ]];then
echo "请在Centos7系统执行脚本,请检查系统版本,终止作业"
exit 1
fi
# 确认执行用户是 root
if [[ $(id -u --name) != "root" ]];then
echo "请使用 root 用户登录,执行脚本;请检查执行用户,终止作业"
exit 1
fi
# 已运行 nginx ,则退出安装
if [[ `ps -ef |grep -E "nginx: master" |wc -l` -ge 2 ]];then
echo "已有运行的 nginx,将退出安装"
exit 1
fi
}
## Linux内核优化
Initsystem(){
cat >> /etc/security/limits.conf << EOF
## ulimit settings ##
soft nofile 65536
hard nofile 65536
soft nproc 65536
hard nproc 65536
## ulimit settings ##
EOF
cat >> /etc/sysctl.conf << EOF
## edit /etc/sysctl.conf ##
kernel.shmall = 2043878
kernel.shmmax = 8175512
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
fs.file-max = 65536
net.ipv4.ip_local_port_range = 1024 65000
net.core.rmem_default = 262144
net.core.rmem_max = 262144
net.core.somaxconn = 2048
net.core.wmem_default = 262144
net.core.wmem_max = 262144
net.ipv4.tcp_rmem = 262144
net.ipv4.tcp_wmem = 262144
## edit /etc/sysctl.conf ##
EOF
sysctl -p
}
# 当前 temp 目录不存在目标版本nginx,则从官网下载 nginx
Downloadnginx(){
# 未指定安装目录,则默认安装在: /usr/local
if [ -z ${install_root} ];then
install_root=/usr/local
else
install_root=${install_root}
mkdir -p ${install_root}
fi
if [ -z ${nginx_version} ];then
echo "默认安装的 Nginx 版本: nginx-1.27.0"
nginx_version=nginx-1.27.0
else
nginx_version=${nginx_version}
fi
if [ -z ${download_url} ];then
echo "默认从 nginx 官网下载"
download_url="http://nginx.org/download"
else
download_url=${download_url}
fi
if [ ! -d temp ];then
mkdir -p temp
fi
if [ ! -e temp/${nginx_version}.tar.gz ];then
# 没有 wget 则下载
which wget || yum install wget -y
wget ${download_url}/${nginx_version}.tar.gz -P temp
fi
}
Installnginx(){
# 使用国内yum源和安装依赖
if [ -e /etc/yum.repos.d/CentOS-Base.repo ];then
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo-$(date +%Y%m%d%H%M%S)
fi
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
yum clean all && yum makecache
yum -y install gcc-c++ pcre-devel pcre openssl-devel unzip zip patch make tee net-tools
# 安装 nginx
# 若已安装 nginx 则备份
if [ -d ${install_root}/${nginx_version} ];then
mv ${install_root}/${nginx_version} ${install_root}/${nginx_version}-$(date +%Y%m%d%H%M%S)
fi
tar -xvf temp/${nginx_version}.tar.gz -C temp
# 编译安装nginx
cd 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-pcre
make && make install
# make -j 4 && make install
# 若存在旧的nginx,则备份
if [[ -d /usr/local/nginx || -L /usr/local/nginx ]];then
mv /usr/local/nginx /usr/local/nginx-$(date +%Y%m%d%H%M%S)
fi
ln -s ${install_root}/${nginx_version} /usr/local/nginx
}
# nginx 启动服务
Nginxsystemd(){
## 备份旧的 nginx 启动文件
if [ -f /usr/lib/systemd/system/nginx.service ];then
mv /usr/lib/systemd/system/nginx.service /usr/lib/systemd/system/nginx.service-$(date +%Y%m%d%H%M%S)
fi
cat > /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStartPre=${install_root}/${nginx_version}/sbin/nginx -t
ExecStart=${install_root}/${nginx_version}/sbin/nginx -c ${install_root}/${nginx_version}/conf/nginx.conf
ExecReload=/bin/kill -s HUP \$MAINPID
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 添加开机启动
systemctl daemon-reload
systemctl enable nginx
}
# nginx 主配置文件
Nginxconf(){
# 未指定端口,则使用默认端口: 80
if [ -z ${nginx_port} ];then
nginx_port=80
else
nginx_port=${nginx_port}
fi
# nginx 配置文件
cat > ${install_root}/${nginx_version}/conf/nginx.conf << EOF
user 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;
underscores_in_headers on;
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 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
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 180;
proxy_cookie_path / "/; httponly ";
limit_conn_zone \$binary_remote_addr zone=one:10m;
# Web 服务器
server {
# listen 80;
listen ${nginx_port};
server_name example.com;
root html;
index index.html;
location / {
try_files \$uri \$uri/ /index.html;
limit_conn one 5;
limit_rate 20k;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 504 400 404 403 413 /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;
}
EOF
systemctl restart nginx
}
Nginxinfo(){
cd ${OLDPWD} # 切换回脚本执行目录,切换前目录为: temp/${nginx_version}
# nginx 运行状态
if [[ `ps -ef |grep -E "nginx: master" |wc -l` -ge 2 ]];then
nginx_status=running
echo "nginx is running ..."
else
nginx_status=stoped
echo "nginx is stoped ..."
echo "please check your nginx"
fi
# 默认获取服务器第一个网卡的IP
HostIP=`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.log
echo "nginx nginx_version: ${nginx_version}" |tee -a temp/install.log
echo "nginx HostIP: $HostIP" |tee -a temp/install.log
echo "nginx nginx_port: ${nginx_port}" |tee -a temp/install.log
echo "nginx install_root: ${install_root}" |tee -a temp/install.log
echo "nginx nginx_home: ${install_root}/${nginx_version}" |tee -a temp/install.log
echo "nginx status: ${nginx_status}" | tee -a temp/install.log
echo -e "\e[33m"
cat temp/install.log
echo -e "\e[0m"
}
# 执行脚本
# check
Initsystem
Downloadnginx
Installnginx
Nginxsystemd
Nginxconf
Nginxinfo
Nginx - 一键实现Nginx的快速安装和优化配置
于 2024-08-02 08:45:18 首次发布