shell脚本实例-中间件
更多脚本下载:https://opszzfwordpress.club/script/
nginx
nginx源码自动安装脚本:将以下的脚本复制到nginx源码文件的目录中
注意:编译安装的openssl、zlib、pcre需下载二进制压缩包拷贝进nginx源码包的主目录
#!/bin/bash echo "nginx安装现在开始" sleep 2 chown -R root:root `pwd` #echo "请设置安装路径:格式:/\$dir/\$dir" while read -p "请设置安装路径 (格式:/\$dir/\$dir):" DIR;do if [ "$DIR" = "" ];then echo "值为空,请重新输入" else echo "安装路径设置完成" break fi done Dire(){ if [ ! -d "$DIR" ];then echo "开始建立目录" mkdir -p $DIR else echo "设置的安装目录已存在" sleep 2 if [ "`ls $DIR`" != "" ];then echo "设置的目录下存在文件,请检查" exit 10 fi fi } user(){ id nginx &>/dev/null if [ $? != 0 ];then echo "现在开始建立nginx用户" sleep 2 useradd nginx else echo "nginx用户已存在,继续安装" sleep 2 fi } Appli(){ ping -c3 baidu.com &>/dev/null echo "现在检测网络连通性" if [ $? -eq 0 ];then echo "网络连通正常" else echo "网络连通异常,请检查" exit 10 fi echo "现在检查yum源情况" sleep 2 if [ -f -a "/etc/yum.repos.d/*.repo" ];then echo "yum源存在" else echo "yum源不存在,请检查" exit 10 fi echo "下面开始下载依赖插件" sleep 2 yum -y install gcc-c++ } confi(){ echo "下面开始进行编译安装" tar xf openssl-1.1.1b.tar.gz tar xf pcre-8.45.tar.gz tar xf zlib-1.2.11.tar.gz sleep 2 make clean ./configure \ --prefix=$DIR \ --sbin-path=$DIR/sbin/nginx \ --modules-path=$DIR/modules \ --conf-path=$DIR/conf/nginx.conf \ --error-log-path=$DIR/log/error.log \ --http-log-path=$DIR/log/access.log \ --pid-path=$DIR/nginx.pid \ --lock-path=$DIR/nginx.lock \ --http-client-body-temp-path=$DIR/client_temp \ --http-proxy-temp-path=$DIR/proxy_temp \ --http-fastcgi-temp-path=$DIR/fastcgi_temp \ --http-uwsgi-temp-path=$DIR/uwsgi_temp \ --http-scgi-temp-path=$DIR/scgi_temp \ --with-compat \ --user=nginx \ --group=nginx \ --with-openssl=openssl-1.1.1b \ --with-pcre=pcre-8.45/ \ --with-zlib=zlib-1.2.11/ \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-http_v2_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ make && make install if [ $? != 0 ];then echo "!!编译失败,请检查,现在退出安装!!" exit 99 else echo "编译成功,现在继续安装" fi } File(){ cat >$DIR/conf/nginx.conf <<EOF user nginx; worker_processes auto; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server_tokens off; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; include vhosts/*.conf; gzip on; access_log $DIR/log/access.log; error_log $DIR/log/error.log; # server { # listen 80; # server_name localhost; # # #charset koi8-r; # # #access_log logs/host.access.log main; # # location / { # root html; # index index.html index.htm; # } # # #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; # } # } } EOF mkdir $DIR/conf/vhosts chown -R nginx:nginx $DIR rm -rf {openssl-1.1.1b,pcre-8.45,zlib-1.2.11} } Tot(){ Dire user Appli confi File } Tot $DIR/sbin/nginx -v &>version.log VER=`awk '{print $3}' version.log | awk -F"/" '{print $2}'` echo "nginx-${VER}已安装完成,请切换到nginx用户进行启动" rm -rf version.log sleep 2
nginx平滑升级
#!/bin/bash echo "nginx升级现在开始" sleep 2 chown -R root:root `pwd` while read -p "请输入原安装路径 (格式:/\$dir/\$dir):" DIR;do if [ "$DIR" = "" ];then echo "值为空,请重新输入" else : break fi done Dire(){ if [ ! -d "$DIR" ];then echo "原安装路径不存在,请检查" exit 10 else : fi if [ ! -f "$DIR/sbin/nginx" ];then echo "原安装目录下nginx二进制文件不存在,请检查" exit 10 fi if [ ! -f "$DIR/nginx.pid" ];then echo "nginx.pid文件不存在,请检查nginx运行情况" exit 10 fi } Dire Appli(){ echo "现在检查yum源情况" sleep 2 if [ -f -a "/etc/yum.repos.d/*.repo" ];then echo "yum源存在" else echo "yum源不存在,请检查" exit 10 fi echo "下面开始下载依赖插件" sleep 2 yum -y install gcc-c++ } Appli confi(){ echo "下面开始进行编译" tar xf openssl-1.1.1b.tar.gz tar xf pcre-8.45.tar.gz tar xf zlib-1.2.11.tar.gz sleep 2 if [ -f "Makefile" ];then make clean fi ./configure \ --prefix=$DIR \ --sbin-path=$DIR/sbin/nginx \ --modules-path=$DIR/modules \ --conf-path=$DIR/conf/nginx.conf \ --error-log-path=$DIR/log/error.log \ --http-log-path=$DIR/log/access.log \ --pid-path=$DIR/nginx.pid \ --lock-path=$DIR/nginx.lock \ --http-client-body-temp-path=$DIR/client_temp \ --http-proxy-temp-path=$DIR/proxy_temp \ --http-fastcgi-temp-path=$DIR/fastcgi_temp \ --http-uwsgi-temp-path=$DIR/uwsgi_temp \ --http-scgi-temp-path=$DIR/scgi_temp \ --with-compat \ --user=nginx \ --group=nginx \ --with-openssl=openssl-1.1.1b \ --with-pcre=pcre-8.45/ \ --with-zlib=zlib-1.2.11/ \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-http_v2_module \ --with-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ make if [ $? != 0 ];then echo "!!编译失败,请检查!!" rm -rf {openssl-1.1.1b,pcre-8.45,zlib-1.2.11} exit 99 else echo "编译成功" rm -rf {openssl-1.1.1b,pcre-8.45,zlib-1.2.11} fi #校验新nginx版本编译的二进制文件 objs/nginx -t -c $DIR/conf/nginx.conf if [ $? -eq 0 ];then echo "编译二进制文件校验成功" else echo "编译二进制文件校验失败,请检查" exit 10 fi } confi ng_update(){ #备份原二进制文件 mv $DIR/sbin/nginx $DIR/sbin/nginx.bak_`date +%F` #复制编译好的二进制文件到原nginx目录 cp objs/nginx $DIR/sbin/ #发送信号:USR2,WINCH kill -USR2 `cat $DIR/nginx.pid` sleep 2 if [ ! -f "$DIR/nginx.pid.oldbin" ];then echo "新的nginx进程号未生成,请检查" exit 10 fi kill -WINCH `cat $DIR/nginx.pid.oldbin` sleep 2 if [ "`ps -ef | grep 'nginx: master process' | grep -v 'grep' | wc -l`" -lt 2 ];then echo "新进程未启动,请检查" exit 10 fi #发送退出信号:QUIT kill -QUIT `cat $DIR/nginx.pid.oldbin` echo "nginx升级成功" $DIR/sbin/nginx -v } ng_update
php
php源码自动安装脚本:使用php-7.4.27版本,将脚本复制到php源码文件目录中,执行脚本
#!/bin/bash #设置安装路径 php_dir(){ echo "请输入安装路径,格式:/\$dir/\$dir" while read dir;do if [ "$dir" = "" ];then echo "请输入安装路径" else if [ ! -d $dir ];then mkdir -p $dir break else dir_num=`ls $dir | wc -l` if [ "$dir_num" != 0 ];then echo "设置的安装目录下有文件,请检查" exit 10 else break fi fi fi done echo "php安装路径已配置完成" sleep 2 } php_dir #安装依赖插件 yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers sqlite-devel oniguruma oniguruma-devel mysqli echo "依赖插件安装完成" sleep 2 #编译安装 confi(){ echo "现在开始编译安装" sleep 2 ./configure --prefix=$dir --with-config-file-path=$dir --enable-mbstring --with-openssl --enable-ftp --enable-gd --with-jpeg --with-png --with-mysqli --with-pear --enable-sockets --with-freetype-dir=$dir --with-zlib --with-libxml-dir=$dir --with-xmlrpc --enable-zip --enable-fpm --enable-xml --enable-sockets --with-gd --with-zlib --with-iconv --enable-zip --with-freetype --enable-soap --enable-pcntl --enable-cli --enable-bcmath --with-curl make && make install if [ $? -eq 0 ];then echo "php编译安装成功" sleep 2 else echo "php编译安装失败,请检查" sleep 2 exit fi } confi #配置php文件 config_file(){ cp php.ini-production $dir/php.ini yes | cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm cp $dir/etc/php-fpm.conf.default $dir/etc/php-fpm.conf cp $dir/etc/php-fpm.d/www.conf.default $dir/etc/php-fpm.d/www.conf sed -i "s@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g" $dir/etc/php-fpm.conf sed -i "s/^.*post_max_size.*$/post_max_size = 16M/g" $dir/php.ini sed -i "s/^.*max_execution_time.*$/max_execution_time = 300/g" $dir/php.ini sed -i "s/^.*max_input_time.*$/max_input_time = 300/g" $dir/php.ini sed -i "s&^;date.timezone.*$&date.timezone = Asia/Shanghai&g" $dir/php.ini } config_file php_redis(){ cd phpredis yum -y install autoconf if [ ! $? -eq 0 ];then echo "autoconf无法下载,请检查" exit 10 fi $dir/bin/phpize ./configure \ --with-php-config=$dir/bin/php-config make && make install if [ ! $? -eq 0 ];then echo "phpredis安装错误,请检查" exit 10 else : fi echo extension="redis.so" >> $dir/php.ini echo "phpredis安装成功" } echo "是否配置php-redis[y/n]" while read red;do if [ "$red" = "" ];then echo "输入错误,请输入y或n" elif [ "$red" = "y" ];then php_redis break elif [ "$red" = "n" ];then break else echo "输入错误,请输入y或n" fi done echo "php已经安装完成,版本:`$dir/bin/php -v | awk 'NR==1{print $1,$2}'`" echo "请输入/etc/init.d/php-fpm或service php-fpm start启动php"
php升级脚本,将脚本复制进新版本的php源码包目录中,按提示执行即可
#!/bin/bash ##输入原php安装路径 php_dir(){ read -p 'please print php install directory:,form:/$dir/$dir: ' phpdir if [ "$phpdir" = "" ];then echo "error: directory null" exit 10 fi phpfile=(bin sbin lib) for i in ${phpfile[*]};do if [ ! -d "$phpdir/$i" ];then echo "error: $i not exist in $phpdir" exit 10 fi done } php_dir ##安装所需的插件 plu_ins(){ plu=(gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers sqlite-devel oniguruma oniguruma-devel mysqli) for i in ${plu[*]};do yum list installed | grep -w "$i" &>/dev/null if [ $? -eq 0 ];then echo "$i is exist" else yum -y install $i if [ $? -eq 0 ];then echo "$i install success" else echo "$i install fail" fi echo "" sleep 2 fi done } plu_ins phpvalue=`$phpdir/bin/php -i | grep 'configure' | sed 's/Configure Command =>//g' | sed "s@'@@g"` ##备份相关文件 filebak(){ /etc/init.d/php-fpm stop ##关闭原版本php进程 sleep 5 ps -ef | grep 'php-fpm' | grep -v 'grep' &>/dev/null if [ $? -eq 0 ];then echo "error: php stop failed" exit 10 fi mv $phpdir/bin $phpdir/bin_bak_`date +%F` mv $phpdir/sbin $phpdir/sbin_bak_`date +%F` mv $phpdir/lib $phpdir/lib_bak_`date +%F` } filebak ##编译安装新版本php phpinstall(){ $phpvalue if [ $? -eq 0 ];then echo "success: new php configure" else echo "error: new php configure failed" exit 10 fi make if [ $? -eq 0 ];then echo "success: new php make" else echo "error: new php make failed" exit 10 fi sleep 2 make install if [ $? -eq 0 ];then echo "success: new php make install" else echo"error: new php make install failed" exit 10 fi sleep 2 } phpinstall user=`ls -l $phpdir -d | awk '{print $3}'` group=`ls -l $phpdir -d | awk '{print $4}'` ##重启php if [ $user != "root" ];then chown -R $user:$group $phpdir su - $user -c "/etc/init.d/php-fpm start" else /etc/init.d/php-fpm start fi sleep 5 ps -ef | grep 'php-fpm' | grep -v 'grep' &>/dev/null if [ $? -ne 0 ];then echo "error: php start failed after update" exit 10 fi echo "success: php version update" echo "php version: `$phpdir/bin/php -v | head -1 | awk '{print $2}'`"