Shell脚本编译安装Nginx(实际生产)
1. 脚本内容
[root@jd-bj-Server1034 ~]# cat install_nginx.sh ---> 脚本分了几个模块 根据自己实际需求进行更改,我这里使用的是Centos7系统
#!/usr/bin/bash
# 安装编译需要的依赖软件
install_software () {
echo -e "\033[32m-----------依赖软件安装-----------\033[0m"
sleep 3
yum -y install GeoIP GeoIP-devel GeoIP-data gd-deve libxml2 libxml2-dev libxslt-dev \
gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel \
libraries libxslt-devel gd gd-devel wget unzip perl -y
}
# 从运维外挂站点下载制作好的nginx压缩包(nginx_install.tar.gz)里面包含了nginx编译需要的nginx二进制包,第三方模块
# 运维外挂站非常简单用nginx配置即可
createfile_tarfile () {
echo -e "\033[32m-----------下载 | 解压 | 授权-----------\033[0m"
sleep 3
# 创建目录并将nginx_install.tar.gz下载到目录中,并对包进行解压
mkdir -p /root/software/Nginx/
wget -cP /root/software/Nginx/ http://123.123.123.123:9999/nginx-install/nginx-1.24.0/nginx_install.tar.gz
tar zxvf /root/software/Nginx/nginx_install.tar.gz -C /root/software/Nginx/
# 将解压出来的压缩包逐个解压(nginx二进制包,第三方模块包)
tar zxvf /root/software/Nginx/nginx-1.24.0.tar.gz -C /root/software/Nginx/
tar zxvf /root/software/Nginx/nginx-module-vts.tar.gz -C /root/software/Nginx/
tar zxvf /root/software/Nginx/ngx_brotli.tar.gz -C /root/software/Nginx/
tar zxvf /root/software/Nginx/openssl-1.1.1l.tar.gz -C /root/software/Nginx/
tar zxvf /root/software/Nginx/zlib-1.2.11.tar.gz -C /root/software/Nginx/
tar zxvf /root/software/Nginx/v0.62.tar.gz -C /root/software/Nginx/
unzip /root/software/Nginx/pcre-8.45.zip -d /root/software/Nginx/
#可以简化为循环,但所有压缩包格式需要都为tar.gz(这只是个实例 我建议还是以绝对路径方式进行解压)
#software=/root/software/Nginx
#cd $software
#for i in $(ls -1 ${software})
#do
# tar -zxvf ${software}/${i} -C ${software}/
#done
# 修改权限
chown -R root.root /root/software/Nginx
chmod -R 755 /root/software/Nginx
}
# 编译
make_software () {
echo -e "\033[32m-----------编译Nginx-----------\033[0m"
sleep 3
cd /root/software/Nginx/nginx-1.24.0/ && ./configure --prefix=/usr/local/nginx \
--modules-path=/usr/lib64/nginx/modules \
--error-log-path=/usr/local/nginx/log/error.log \
--http-log-path=/usr/local/nginx/log/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=root \
--group=root \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-http_random_index_module \
--with-debug \
--with-pcre=/root/software/Nginx/pcre-8.45 \
--with-openssl=/root/software/Nginx/openssl-1.1.1l \
--with-zlib=/root/software/Nginx/zlib-1.2.11 \
--add-dynamic-module=/root/software/Nginx/echo-nginx-module-0.62 \
--add-module=/root/software/Nginx/nginx-module-vts
}
# 安装
install_nginx () {
echo -e "\033[32m-----------安装Nginx-----------\033[0m"
sleep 3
cd /root/software/Nginx/nginx-1.24.0/ && make
cd /root/software/Nginx/nginx-1.24.0/ && make install
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
# 这里在启动nginx的时候会提示一些目录不存在需要提前创建好
mkdir -p /var/lib/nginx/tmp/client_body
mkdir -p /usr/local/nginx/tmp/proxy
mkdir -p /usr/local/nginx/conf/conf.d
}
install_software
createfile_tarfile
make_software
install_nginx
2.目录结构
[root@jd-bj-Server1034 Nginx]# pwd ----> 脚本所在目录 如果你需要自定义需要对脚本进行调整,这里是为了保证脚本不受到目录查找错误的可能影响写死了
/root/software/Nginx
[root@jd-bj-Server1034 Nginx]# tree -L 1
# 模块大家可以直接到github上找寻
├── nginx_install.tar.gz ---> nginx安装包(里面压缩的就是这个文件夹中的文件)
├── nginx-1.24.0.tar.gz
├── nginx-module-vts.tar.gz ---> nginx第三方插件
├── ngx_brotli.tar.gz ---> nginx第三方插件
├── openssl-1.1.1l.tar.gz ---> nginx第三方插件
├── pcre-8.45.zip ---> nginx第三方插件
├── read.txt
├── v0.62.tar.gz ---> echo-nginx-module第三方模块
└── zlib-1.2.11.tar.gz ---> nginx第三方插件
0 directories, 10 files
再次提醒一下脚本需要根据自己实际情况进行调整,不要直接用于生产,我的脚本也是在测试环境调试后才会到生产环境。