#!/bin/bash
# openresty 一键在线安装脚本
# 2024-06-25
# create by xfp
set -e
# 设置SRC_DIR为当前目录
SRC_DIR="$PWD"
echo "当前目录是: $SRC_DIR"
#====================================================
# 用户可自定义参数部分,不定义则使用默认值
# 默认安装路径
#install_root=/usr/local
# openresty 默认端口
# openresty_port=80
# 默认安装版本
#openresty_version=openresty-1.25.3.2
#====================================================
# 确认是在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
}
#https://openresty.org/download/openresty-1.25.3.2.tar.gz
# 当前 temp 目录不存在目标版本nginx,则从官网下载 nginx
Downloadopenresty(){
# 未指定安装目录,则默认安装在: /usr/local
if [ -z ${install_root} ];then
install_root=/mnt
else
install_root=${install_root}
mkdir -p ${install_root}
fi
if [ -z ${openresty_version} ];then
echo "默认安装的 openresty 版本: openresty-1.25.3.2"
openresty_version=openresty-1.25.3.2
else
openresty_version=${openresty_version}
fi
if [ -z ${download_url} ];then
echo "默认从 openresty 官网下载"
download_url="https://openresty.org/download/"
else
download_url=${download_url}
fi
if [ ! -d temp ];then
mkdir -p temp
fi
if [ ! -e temp/${openresty_version}.tar.gz ];then
# 没有 wget 则下载
which wget || yum install wget -y
wget ${download_url}/${openresty_version}.tar.gz -P temp
fi
}
Installopenresty(){
# 安装 openresty
# 若已安装 openresty 则备份
if [ -d ${install_root}/${openresty_version} ];then
mv ${install_root}/${openresty_version} ${install_root}/${openresty_version}-$(date +%Y%m%d%H%M%S)
fi
tar -xvf temp/${openresty_version}.tar.gz -C temp
tar -xvf ${SRC_DIR}/openssl-1.1.1w.tar.gz
tar -xvf ${SRC_DIR}/pcre-8.45.tar.gz
tar -xvf ${SRC_DIR}/zlib-1.3.1.tar.gz
tar -xvf ${SRC_DIR}/2.3.tar.gz
# 编译安装openresty
cd temp/${openresty_version}
./configure --prefix=${install_root}/${openresty_version} \
--modules-path=${install_root}/${openresty_version}/modules \
--with-openssl=${SRC_DIR}/openssl-1.1.1w \
--with-pcre=${SRC_DIR}/pcre-8.45 \
--with-zlib=${SRC_DIR}/zlib-1.3.1 \
--add-module=${SRC_DIR}/ngx_cache_purge-2.3 \
--with-http_ssl_module \
--with-luajit \
--with-http_stub_status_module \
--with-stream \
--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
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}/${openresty_version}/nginx /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}/${openresty_version}/nginx/sbin/nginx -t
ExecStart=${install_root}/${openresty_version}/nginx/sbin/nginx -c ${install_root}/${openresty_version}/nginx/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
}
# openresty 主配置文件
Openrestyconf(){
# 未指定端口,则使用默认端口: 80
if [ -z ${openresty_port} ];then
openresty_port=8088
else
openresty_port=${openresty_port}
fi
# openresty 配置文件
cat > ${install_root}/${openresty_version}/nginx/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;
#隐藏openresty版本信息
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-openresty-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 ${openresty_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
没有合适的资源?快使用搜索试试~ 我知道了~
一键实现Nginx的快速安装和优化配置
需积分: 5 0 下载量 89 浏览量
2024-08-05
14:29:50
上传
评论
收藏 12.87MB GZ 举报
温馨提示
一键实现Nginx的快速安装和优化配置
资源推荐
资源详情
资源评论
收起资源包目录
Install_Openresty_V1.25.3.2.tar.gz (6个子文件)
Install_Openresty_V1.25.3.2
pcre-8.45.tar.gz 2.01MB
install_nginx.sh 10KB
install_openresty.sh 11KB
openssl-1.1.1w.tar.gz 9.44MB
2.3.tar.gz 11KB
zlib-1.3.1.tar.gz 1.44MB
共 6 条
- 1
资源评论
only°夏至besos
- 粉丝: 4628
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功