Linux一键编译安装Nginx脚本

Posted 白-胖-子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux一键编译安装Nginx脚本相关的知识,希望对你有一定的参考价值。

[root@c7-183 ~]# curl 10.0.0.88/shell/installnginx.sh
#!/bin/bash
SRC_DIR=/usr/local/src
NGINX_URL=http://nginx.org/download/
NGINX_FILE=nginx-1.20.1
TAR=.tar.gz
NGINX_INSTALL_DIR=/apps/nginx
NGINX_DIR=`echo ${NGINX_FILE}${TAR}| sed -nr 's/^(.*[0-9]).*/\\1/p'`
CPUS=`lscpu |awk '/^CPU\\(s\\)/{print $2}'`
## 操作系统判断
os_type () {
awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release
}
## 操作系统版本判断
os_version () {
awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release
}
## 安装前检查
check () {
wget ${NGINX_URL}${NGINX_FILE}${TAR}
}
## 安装依赖包
install_pre(){
if [ `os_type` == "CentOS" -a `os_version` == '8' ] ;then
yum -y -q install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel
openssl openssl-devel perl-ExtUtils-Embed
elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then
yum -y -q install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed
else
apt update &> /dev/null
apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1gdev
&> /dev/null
fi
}
## 安装
install () {
useradd -s /sbin/nologin -r nginx &> /dev/null
tar xf ${NGINX_FILE}${TAR} -C $SRC_DIR && cd $SRC_DIR/${NGINX_DIR}
./configure --prefix=${NGINX_INSTALL_DIR} \\
--user=nginx \\
--group=nginx \\
--with-http_ssl_module \\
--with-http_v2_module \\
--with-http_realip_module \\
--with-http_stub_status_module \\
--with-http_gzip_static_module \\
--with-pcre \\
--with-stream \\
--with-stream_ssl_module \\
--with-stream_realip_module
make&& make install
echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh
. /etc/profile.d/nginx.sh
cat > /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
PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid
ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t
ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx
ExecReload=/bin/kill -s HUP \\$MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
sed -i "s/nginx/`hostname -I`/" /apps/nginx/html/index.html
systemctl daemon-reload
systemctl enable --now nginx &> /dev/null
exit; 
}
check&&install_pre&&install

编译安装nginx

以上是关于Linux一键编译安装Nginx脚本的主要内容,如果未能解决你的问题,请参考以下文章

linux一键安装nginx脚本

linux一键安装php脚本

CentOS一键编译安装nginx,带http2/brotli/zlib/pcre/ssl,非root监听80端口

sh linux ubuntu一键安装svn,直接使用源安装会有问题,编译安装太麻烦,使用老版本可以一键安装,脚本来源于网络,替换了失效的下载链接。

Linux系统shell脚本之nginx编译安装

LNMP源码编译安装及一键部署LNMP架构