debian12/ubuntu24/22/20 shell 脚本自动编译web安装网站环境 nginx1.26.0 php7/8 mariadb1011

2024-07-04 20:56:43 浏览数 (3)

代码语言:shell复制
#!/bin/bash
# 支持系统: debian12/ubuntu24/22/20  shell 脚本自动编译web lnmp 安装网站环境  nginx1.26.0 php7/8 mariadb1011  以 systemd 方式设置开机启动
# mysql 重置 root 密码,创建 或删除 mysql 子库和子用户
downDir="/datadisk/eisc/download" ; 
installdir="/datadisk/eisc/server" ; 
www="/datadisk/eisc/www"
wwwconf="/datadisk/eisc/www/wwwconf"
ReleaseList=( nginxRelease phpRelease maraidbRelease cgiccRelease  )

NginxSelectVersion=0; PhpSelectVersion=0; MysqlSelectVersion=0 ; CgiccSelectVersion=0
# 安装路径不能将所有目录都为 777 , 如: mysql 的 /etc/my.cnf 不能为 777 否则mysql 无法启动; 
# 设置web 网站路径 和 nginx conf 子站点路径 

echo "
欢迎使用自动安装 网站web 环境shell 脚本, nginx1.26.0 php-fpm mariadb10.11 
支持的系统: debian12 / ubuntu24/23/22/20

  当前需要更新 apt upgrade , 如果遇到弹窗选择包,请回车跳过。不要按 Esc 退出. 建议选择 ubuntu20.04 

	  安装完成重启服务器后,启动web服务命令:  sudo nginx126 ; sudo php-fpm743 ; sudo /etc/init.d/mariadb1011 restart
  数据库默认登陆: mysql -u root -peisc.cn

  server 源码下载路径:$downDir
  server 安装路径:$installdir
  www 网站路径:$www
  www nginx conf 子站点路径:$wwwconf

set 2023.07.09 小绿叶技术博客  eisc.cn 
"

sleep 2;



os_release()
{

    if [ $USER = "root" ]
    then 
        sudoc=" "
        # 判断字符串的等号要加空格,否则无法判断
    else
        sudoc="sudo"
        echo "当前为普通用户,需要输入root 密码...  $sudoc"
        $sudoc date   ;
    fi

    xitong=` cat /etc/os-release  | grep -w ID | awk -F"=" '{print $2}' | sed "s/"//g" `
    case "$xitong" in
        "centos" ) aptc="yum" ; dpkgc="rpm -qa " ; echo "当前是 centos 系统"   ;;
        "ubuntu" ) aptc="apt" ; dpkgc="dpkg -l " ; echo "当前是 ubuntu 系统"   ;;
        "debian" ) aptc="apt" ; dpkgc="dpkg -l " ; echo "当前是 debian 系统"   ;;
    esac

    if [ ! -e /etc/init.d/mariadb1011 ]
    then
        sudo apt update ; sudo apt upgrade -y 
    fi


    $sudoc mkdir -p $downDir $installdir $www   
    $sudoc chmod 777 $downDir $installdir $www       # chmod 附加权限
}

nginxRelease()
{
    case $selectFlag in
        "0") downUrl="http://work.eisc.cn/ruanjian/ubuntu/server/nginx"      ;;
        "1") downUrl="http://nginx.org/download"                         ;;
        *)   echo "未定义下载地址..." ; exit                               ;;
    esac
    
    if [[ $NginxSelectVersion = 0 ]]
    then
        read -p "nginx 提供版本: 1. nginx-1.26.0   2.nginx-1.20.0   3. nginx1.9.9  请输入序号选择版本, 任意字符退出安装nginx: " NginxSelectVersion
        case $NginxSelectVersion in
            "1")  ReleaseMl=nginx-1.26.0 ; ReleaseInstall=nginx/nginx126 ; ReleaseBin=nginx ; Down="$downUrl/$ReleaseMl.tar.gz" ; echo "nginx版本 $ReleaseMl  下载:$Down" ;; 
            "2")  ReleaseMl=nginx-1.20.0 ; ReleaseInstall=nginx/nginx120 ; ReleaseBin=nginx ; Down="$downUrl/$ReleaseMl.tar.gz" ; echo "nginx版本 $ReleaseMl  下载:$Down" ;; 
            "3")  ReleaseMl=nginx-1.9.9  ; ReleaseInstall=nginx/nginx199 ; ReleaseBin=nginx ; Down="$downUrl/$ReleaseMl.tar.gz" ; echo "nginx版本 $ReleaseMl  下载:$Down" ;; 
            *)    ReleaseMl=0            ; echo "[ok]退出安装 nginx"
        esac
    fi

}

phpRelease()
{
    case $selectFlag in
        "0") downUrl="http://work.eisc.cn/ruanjian/ubuntu/server/php"       ;;
        "1") downUrl="https://www.php.net/distributions"                    ;;
        *)   echo "未定义"                                                   ;;
    esac

    if [[ $PhpSelectVersion = 0 ]]
    then
        read -p "php 提供版本:  1. php7.4.33  2.php7.2.1   3. php8.1.13  任意按键取消安装php  请输入编号: " PhpSelectVersion
        case $PhpSelectVersion in
            "1")  ReleaseMl=php-7.4.33   ; ReleaseInstall=php/php743 ; ReleaseBin=php-fpm ; Down="$downUrl/$ReleaseMl.tar.gz" ; echo "php版本 $ReleaseMl  下载:$Down" ;; 
            "2")  ReleaseMl=php-7.2.1    ; ReleaseInstall=php/php721 ; ReleaseBin=php-fpm ; Down="$downUrl/$ReleaseMl.tar.gz" ; echo "php版本 $ReleaseMl  下载:$Down" ;;
            "3")  ReleaseMl=php-8.1.13   ; ReleaseInstall=php/php811 ; ReleaseBin=php-fpm ; Down="$downUrl/$ReleaseMl.tar.gz" ; echo "php版本 $ReleaseMl  下载:$Down" ;;
            *)    ReleaseMl=0            ; echo "[ok]退出安装 php "
        esac
    fi

}

maraidbRelease()
{
    case $selectFlag in
        "0") downUrl="http://work.eisc.cn/ruanjian/ubuntu/server/mysql"      ;;
        "1") downUrl="https://archive.mariadb.org//mariadb-10.11.0/bintar-linux-systemd-x86_64/"  ;;
        *)   echo "未定义下载地址..." ; exit                               ;;
    esac

    if [[ $MysqlSelectVersion = 0 ]]
    then
        read -p "mysql 提供版本:1. mariadb-10.11.0-linux-systemd-x86_64  请输入1选择版本, 任意字符退出安装 mysql : " MysqlSelectVersion
        case $MysqlSelectVersion in
            "1")  ReleaseMl="mariadb-10.11.0-linux-systemd-x86_64"; ReleaseInstall="mysql/mariadb1011"; ReleaseBin="mariadb";  Down="$downUrl/$ReleaseMl.tar.gz"; echo "$ReleaseMl 下载地址重新定义为:$Down" ;; 
            *)    ReleaseMl=0                                     ; echo "[ok]退出安装  mysql."
        esac
    fi
}

cgiccRelease()
{
    case $selectFlag in
        "0") downUrl="http://work.eisc.cn/ruanjian/ubuntu/server/cgi"      ;;
        "1") downUrl="http://ftp.gnu.org/gnu/cgicc"                    ;;
        *)   echo "未定义下载地址..." ; exit                             ;;
    esac

    if [[ $CgiccSelectVersion = 0 ]]
    then
        read -p "cgicc 提供版本:1. cgicc-3.2.19 请输入1选择版本, 任意字符退出安装 cgicc : " CgiccSelectVersion
        case $CgiccSelectVersion in
            "1")  ReleaseMl="cgicc-3.2.19"; ReleaseInstall="cgi/cgicc321"; ReleaseBin="cgicc";  Down="$downUrl/$ReleaseMl.tar.gz"; echo "$ReleaseMl 下载地址重新定义为:$Down" ;; 
            *)    ReleaseMl=0             ; echo "[ok] 退出安装 cgicc."
        esac
    fi
}

down_select(){
    read -p "选择下载安装包地址: 1 小绿叶技术博客下载     2 官方下载 . 请输入:  "   selectURL
    case $selectURL in
        "1") echo "当前选择小绿叶技术博客下载..." ;     selectFlag=0 ;;
        "2") echo "当前选择官方下载..." ;             selectFlag=1 ;;
        *) echo "输入错误,请重新执行脚本! " ; exit ;;
    esac
}

aptInstall()
{
  gcc="build-essential cmake"
  # build-essential 包包含了 GCC、C  、Make 等必要的开发工具。cmake 是一个跨平台的自动化构建工具,用于管理 C   项目。
  nginxApt="build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl libgd-dev geoip-database libgeoip-dev unzip nfs-kernel-server nfs-common libcrypt-dev"  
  # gcc c   ; pcre ; zlib  ssl  GD ; GeoIP 依赖库 

  phpApt="
      gcc make openssl curl libssl-dev libxml2-dev libzip-dev libcurl4-openssl-dev
      libpng-dev libjpeg-dev libwebp-dev libonig-dev libsqlite3-dev libsodium-dev libargon2-dev 
      libkrb5-dev libbz2-dev libxslt-dev php-mbstring 
      build-essential libtool  libpcre3 libpcre3-dev zlib1g-dev   libgd-dev geoip-database libgeoip-dev
   "
  # 安装包: No package 'krb5-gssapi' found ; Please reinstall the BZip2 distribution ; No package 'libxslt' found 
	  # 报错解决参考:http://t.zoukankan.com/architectforest-p-15714248.html

  mariadbApt="libncurses5"
  cgiApt="build-essential libfcgi-dev autoconf libtool automake libtool spawn-fcgi fcgiwrap"

  $sudoc $aptc install -y $gcc 
  $sudoc $aptc install -y $nginxApt 
  $sudoc $aptc install -y $phpApt 
  $sudoc $aptc install -y $mariadbApt 
  $sudoc $aptc install -y $cgiApt 
 

}

nginx_Compilation_method()
{
    if [[ "$ReleaseMl" = "nginx-1.9.9" ]]
    then 
        # nginx1.9.9 是老版本需要修改文件才能编译
        ./configure --prefix=$rundir   --with-stream --with-http_stub_status_module 
        nginx 开启 tcp 和 文件浏览模块:  --with-stream --with-http_stub_status_module 
        NR=`cat -n ./src/os/unix/ngx_user.c | grep cd.current_salt | awk -F" " '{print $1}'`        ; sleep 0.5
        $sudoc sed -i "$NR d" ./src/os/unix/ngx_user.c                                              ; sleep 0.5
        $sudoc sed -i "s/-Werror/ /g" objs/Makefile                                                 ; sleep 0.5
        # 先修改 c 文件,再修改make

        $sudoc mkdir $setdir/logs ; $sudoc chmod 777 -R $setdir/logs
        $sudoc make  ; echo "make ok"                                                               ; sleep 5         # make  会生成 新文件,新文件也需要修改才能编译
        $sudoc sed -i "s/-Werror/ /g" objs/Makefile                                                 ; sleep 0.5
        $sudoc make install
    else 
        ./configure --prefix=$rundir   --with-stream --with-http_stub_status_module 
        --with-http_ssl_module 
        --with-http_realip_module 
        --with-http_sub_module 
        --with-http_gzip_static_module 
        --with-pcre

        $sudoc mkdir $setdir/logs ; $sudoc chmod 777 -R $setdir/logs
        # nginx 开启 tcp 和 文件浏览模块:  --with-stream --with-http_stub_status_module 
        make ; make install
        # sudo 命令会获得权限而 重复安装到默认的  /usr/local/nginx 目录
    fi
}

cgicc_Compilation_method()
{

    ./configure --prefix=$rundir
    make ; make install
}

php_Compilation_method()
{
    $sudoc groupadd www ; $sudoc useradd -g www www -s /bin/false 
    # sudo groupadd www-data ; sudo useradd -g www-data www-data -s /bin/false  # 重建用户,并且不允许用户登陆系统

    ./configure --prefix=$rundir --with-config-file-path=$rundir/etc 
       --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd 
       --with-iconv-dir --with-freetype --with-jpeg --with-zlib 
       --enable-zip 
       --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl 
       --enable-ftp --enable-gd --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --disable-fileinfo --enable-opcache  --with-webp
      # --with-openssl 

      # 第一行 指定安装路径,和 php.ini  路径为 $installdir/php/php72/etc
      # 第二行 开启php 的 php-fpm web功能。启动用户名和组  和 重要 开启php 的mysql connect() 函数 功能。  
      # 第三行 压缩解码工具 否则 php WordPress 无法无法正常启动 报错:  is not a valid libtool object
      # 后面行,暂时没有研究.  --with-openssl   ubuntu22 编译不过

    make ; make install
    # ./configure 不能使用sudo 权限,否则后面make 和 make install 出现问题
#----------------------- 说明 -------------------#
	# 安装说明:https://www.php.net/manual/zh/install.php
	#--with-fpm-user - 设置 FPM 运行的用户身份(默认 - nobody)。
	#--with-fpm-group - 设置 FPM 运行时的用户组(默认 - nobody)。
#--with-fpm-systemd - 启用 systemd 集成 (默认 - no)。
#--with-fpm-acl - 使用 POSIX 访问控制列表 (默认 - no)。
#--with-fpm-apparmor - 激活 AppArmor 集成 (默认 - no)。
	#--with-fpm-selinux - 激活 SELinux 集成(默认 - no)。


#---------------------- php config ---------------#
    

    $sudoc cp $setdir/sapi/fpm/php-fpm.service.in /etc/systemd/system/php-fpm.service
    $sudoc cp $setdir/php.ini-development $rundir/etc/php.ini

    $sudoc cp $rundir/etc/php-fpm.conf.default $rundir/etc/php-fpm.conf
    $sudoc cp $rundir/etc/php-fpm.d/www.conf.default $rundir/etc/php-fpm.d/www.conf

#----------  更改用户
    NR=`cat -n $rundir/etc/php-fpm.d/www.conf  | grep -w user | grep nobody | awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR s/nobody/www/g" $rundir/etc/php-fpm.d/www.conf
    else echo "[noset] php-user 1"; fi

    NR=`cat -n $rundir/etc/php-fpm.d/www.conf  | grep -w group | grep -v listen | grep nobody | awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR s/nobody/www/g" $rundir/etc/php-fpm.d/www.conf
    else echo "[noset] php-user 2"; fi

    NR=`cat -n /etc/systemd/system/php-fpm.service | grep ProtectSystem | awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR s/full/false/g" /etc/systemd/system/php-fpm.service 
    else echo "[noset] php-user 3"; fi

#----------  解决 No input file specified
    NR=`cat -n $rundir/etc/php.ini | grep fix_pathinfo | grep -v provides| awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR a cgi.fix_pathinfo=1" $rundir/etc/php.ini 
        $sudoc sed -i "$NR d" $rundir/etc/php.ini 
    else echo "[noset] 解决 No input file specified 1"; fi

    NR=`cat -n $rundir/etc/php.ini | grep force_redirect | grep 1| awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR a cgi.force_redirect=0" $rundir/etc/php.ini 
        $sudoc sed -i "$NR d" $rundir/etc/php.ini 
    else echo "[noset] 解决 No input file specified 2"; fi
#-----------  
# listen.owner = www
# listen.group = www
# listen.mode = 0777
# 这三个是php 的 sock 进程通信的重要参数

    NR=`cat -n $rundir/etc/php-fpm.d/www.conf  | grep listen.owner | grep www | awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        sed -i "$NR a listen.owner = www" $rundir/etc/php-fpm.d/www.conf 
        sed -i "$NR d" $rundir/etc/php-fpm.d/www.conf 
    else echo "[noset] listen.owner = www "; fi

    NR=`cat -n $rundir/etc/php-fpm.d/www.conf  | grep listen.group | grep www | awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        sed -i "$NR a listen.group = www" $rundir/etc/php-fpm.d/www.conf 
        sed -i "$NR d" $rundir/etc/php-fpm.d/www.conf 
    else echo "[noset] listen.group = www "; fi
    
    NR=`cat -n $rundir/etc/php-fpm.d/www.conf  | grep listen.mode | grep www | awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        sed -i "$NR a listen.mode = 0777" $rundir/etc/php-fpm.d/www.conf 
        sed -i "$NR d" $rundir/etc/php-fpm.d/www.conf 
        # 修改用户和权限,否则 php 无法通讯
    else echo "[noset] listen.mode = 0777 "; fi

    NR=`cat -n $rundir/etc/php-fpm.d/www.conf | grep  "127.0.0.1" | grep -v ";"| awk -F" " '{print $1}'`
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR a listen =  /var/run/php-fpm.sock" $rundir/etc/php-fpm.d/www.conf            # php进程通讯模式
        #$sudoc sed -i "$NR a listen =  127.0.0.1:9000" $rundir/etc/php-fpm.d/www.conf                  # php的ip通讯模式
        $sudoc sed -i "$NR d" $rundir/etc/php-fpm.d/www.conf
    else echo "[noset] listen =  /var/run/php-fpm.sock "; fi

#sudo sed -i "778 s/;//g" /etc/php/7.4/fpm/php.ini
#sudo sed -i "778 s/1/0/g" /etc/php/7.4/fpm/php.ini
#sudo sed -i "798 s/;//g" /etc/php/7.4/fpm/php.ini
# 解决 No input file specified

    NR=`cat -n $rundir/etc/php.ini  | grep display_errors | grep -v ";"| awk -F" " '{print $1}' ` ; echo $NR ; 
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR s/On/Off/g" $rundir/etc/php.ini 
        # 关闭 php 警告  
    fi

    NR=`cat -n $rundir/etc/php.ini  | grep error_reporting| grep -v ";"| awk -F" " '{print $1}' ` ; echo $NR ; 
    if [ ${#NR} -gt 1 ];then
        $sudoc sed -i "$NR s/E_ALL/0/g" $rundir/etc/php.ini 
        # 关闭php 警告 
    fi

    #NR=`cat -n php.ini | grep log_errors| grep -v ";"| awk -F" " '{print $1}' ` ; echo $NR ; $sudoc sed -i "$NR s/E_ALL/0/g" php.ini
    # 警告记录到日志 
    $sudoc $rundir/bin/$ReleaseBin
    $sudoc chmod 777 /var/run/php-fpm.sock
    # 需要给 php-fpm.sock 权限 777  否则nginx 无法访问


}

mariadb_Compilation_method()
{
    $sudoc groupadd mysql ; $sudoc useradd -g mysql mysql -s /bin/false 
    # 创建mysql用户组和用户,不允许登陆系统

    $sudoc mv $downDir/$ReleaseInstall/* $rundir
    $sudoc mkdir -p $installdir/sqldata  
    $sudoc chown -R mysql:mysql $installdir/sqldata ; $sudoc chmod 777 -R $installdir/sqldata 
    $sudoc $rundir/scripts/mysql_install_db --user=mysql --basedir=$rundir --datadir=$installdir/sqldata
    # 初始化mariadb

    $sudoc cp $rundir/support-files/wsrep.cnf /etc/my.cnf
    $sudoc chmod 777 /etc/my.cnf
echo "
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
#添加以下配置
basedir=$rundir
datadir=$installdir/sqldata

# 设置字符集为UTF-8
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

" > /etc/my.cnf

    $sudoc chmod 644 /etc/my.cnf
    # my.cnf 权限为 777 不安全,所以mysql无法启动。
    $sudoc cp $rundir/support-files/mysql.server /etc/init.d/mysql
    $sudoc cp $rundir/bin/mysqld /etc/init.d/mysqld 
    $sudoc ln -s $rundir/support-files/mysql.server /etc/init.d/mariadb1011
    $sudoc chmod  x /etc/init.d/mysqld

    #---------- 重置 mysql root 密码也是该流程 ---------# 
    $sudoc  killall mariadbd mysqld_safe

    runmysql=`sudo netstat -nltp | grep 3306 | grep -w tcp | grep mariadb`
    if [ ${#runmysql} -lt 1 ]
    then
      sudo /etc/init.d/mariadb1011 start
    fi

    # 重新启动mysql 服务, mysql 启动时候才能进行 mysqladmin 命令 重置密码
    sleep 1;
    $sudoc $rundir/bin/mysqladmin  -u root password eisc.cn
    echo "数据库root 密码为: eisc.cn  使用命令更改密码: "
    echo "sudo $rundir/bin/mysqladmin  -u root password eisc.cn"

    $sudoc mkdir -p $installdir/back/sql/ ; $sudoc chmod 777 $installdir/back/sql/

}

download()
{

    cd $downDir ; echo "当前进入目录进行下载:  cd $downDir "

    echo "[runing] 正在检查是否下载安装包: $ReleaseMl.tar.gz"

    # [ ! -d $setdir ] && echo "[running] 目标安装路径不存在" || break ; 

    if [ ! -f $ReleaseMl.tar.gz ]
    then 
        echo "[runing] 当前没有下载,正在下载包:wget $Down "
        wget $Down -O $downDir/$ReleaseMl.tar.gz
    else
        echo  "[ok] 已经下载: $downDir/$ReleaseMl.tar.gz"
    fi

    cutNumber=`ls $setdir/* |sort |uniq -c | awk -F" " '{print $1}'` ; 
    if [[ "$cutNumber" = "" ]]                   # 判断变量的数值大小,需要双小括号  引号
    then 
        echo "[running] 正在解压 $downDir/$ReleaseMl.tar.gz 到目录: $setdir" ; 
        tar -xzvf $downDir/$ReleaseMl.tar.gz -C $downDir/
        mv $downDir/$ReleaseMl/*  $downDir/$ReleaseInstall
        rm -rf $downDir/$ReleaseMl 
    else 
        echo "[ok] 已经解压 : $setdir 正在安装 " 
    fi


}

RunInstall(){

    for i in ${ReleaseList[*]}
    do
        $i
        MakeFlag=1;
        soFlag=1

        setdir=$downDir/$ReleaseInstall ;
        rundir=$installdir/$ReleaseInstall ;
        $sudoc mkdir -p $setdir $rundir ; $sudoc chmod 777 -R $setdir $rundir

        echo "当前正在安装  $i"  ; sleep 1;

        case "$ReleaseBin" in 
            "nginx")   [ ! -f $rundir/sbin/$ReleaseBin   ]  && soFlag=1 || soFlag=0  ;;   
            "php-fpm") [ ! -f $rundir/sbin/$ReleaseBin   ]  && soFlag=1 || soFlag=0  ;; 
            "mariadb") [ ! -f $rundir/bin/$ReleaseBin    ]  && soFlag=1 || soFlag=0  ;;
            "cgicc")   [ ! -f $rundir/bin/cgicc-config   ]  && soFlag=1 || soFlag=0  ;;
        esac

        if [[ $ReleaseMl = 0 ]] 
        then
            echo "用户取消安装: $i"
        else

            if [[ ! -f  $rundir/bin/$ReleaseBin ]] && [[ $soFlag = "1" ]]
            then
                download        # 安装前检查是否安装 

                cd $setdir 
                echo "[waring] 包没有安装 $rundir/bin/$ReleaseBin 当前进入目录:  $setdir  "  ; 

                case "$ReleaseBin" in 
                    "nginx"   )     nginx_Compilation_method   ; Fpath ; $sudoc cp $rundir/sbin/$ReleaseBin /bin/  ;;
                    "php-fpm" )     php_Compilation_method     ; Fpath ; $sudoc cp $rundir/sbin/$ReleaseBin /bin/  ;;
                    "mariadb" )     mariadb_Compilation_method ; Fpath ; $sudoc cp $rundir/bin/$ReleaseBin /bin/   ;;
                    "cgicc" )       cgicc_Compilation_method   ; Fpath ; $sudoc cp $rundir/bin/$ReleaseBin /bin/   ;;
                    *)              echo "未定指定编译,走通用编译方法..." ; $qx ./configure --prefix=$setdir ; $qx make -j 4 ; $qx  make instlal; Fpath ;;
                esac 

                if [ $MakeFlag = "1" ]
                then
                    $qx ./configure  ; $qx make -j 2 ; $qx make install  
                fi

                if [[  -f  $rundir/sbin/$ReleaseBin ]] 
                then 
                    echo "[ok] Success installd path: $rundir/sbin/$ReleaseBin"
                else 
                    echo "[fail] install Fail : $rundir/sbin/$ReleaseBin "
                fi  

            else
                echo "[ok] 已经安装: $rundir "     

            fi

            if [[ "$ReleaseBin" = "nginx" ]]
            then
                nginx_web_config
            fi
        fi
    done
}

Fpath()
{
    PathNginx=NGIXN_HOME
    PathPhp=PHP_HOME
    PathMariadb=MARIADB_HOME

    $sudoc chmod 777 /etc/profile 
    StrCmake=`cat /etc/profile | grep $ReleaseInstall`
    StrCmakeLength=${#StrCmake}
    if [ $StrCmakeLength -lt 1 ]
    then 
        case "$ReleaseBin" in 
            "nginx") echo "#### $ReleaseInstall ####"     >> /etc/profile ;
                           echo "export $PathNginx=$rundir/sbin"   >> /etc/profile ;
                           echo "export PATH=${$PathNginx}:$PATH" >> /etc/profile ;;

            "php-fpm") echo "#### $ReleaseInstall ####"     >> /etc/profile ;
                           echo "export $PathPhp=$rundir/sbin"   >> /etc/profile ;
                           echo "export PATH=${$PathPhp}:$PATH" >> /etc/profile ;;

            "mariadb") echo "#### $ReleaseInstall ####"     >> /etc/profile ;
                           echo "export $PathMariadb=$rundir/bin"   >> /etc/profile ;
                           echo "export PATH=${$PathMariadb}:$PATH" >> /etc/profile ;;

            "cgicc") echo "#### $ReleaseInstall ####"     >> /etc/profile ;
                           echo "export $PathMariadb=$rundir/bin"   >> /etc/profile ;
                           echo "export PATH=${$PathMariadb}:$PATH" >> /etc/profile ;;
        esac

# 指定目录可以 写一行,指定运行文件需要两行:目录 执行路径

    else
        echo "[ok] 环境变量已经配置,继续向下执行..."

    fi    

# export LD_LIBRARY_PATH=/path/to/libmpfr.so.6:$LD_LIBRARY_PATH

}

nginx_web_config(){

      $sudoc mkdir -p $wwwconf ; sudo chmod 777 -R $wwwconf

      setconf=`cat $installdir/$ReleaseInstall/conf/nginx.conf | grep "$wwwconf" `
      if [ ${#setconf} -gt 10 ]
      then 
          echo "[ok]nginx 已经配置, 子站点目录: $wwwconf/www.conf"
      else
          NR=`cat -n $installdir/$ReleaseInstall/conf/nginx.conf | grep http | grep "{" | awk -F" " '{print $1}'`
          sudo sed -i "$NR a include       $wwwconf/*.conf; " $installdir/$ReleaseInstall/conf/nginx.conf
      fi

      $sudoc $rundir/sbin/nginx 
      $sudoc $rundir/sbin/nginx -s reload
      # 修改php 后重新启动nginx
}

create_www_conf(){

    echo "案例:   eisc.cn 0.0.0.0 62000"
    read -p " 请输入三个参数:" peizhi

    buff=( $peizhi )

    yuming=${buff[0]}
    # 前端域名,增加到nginx 消息头部
    ip=${buff[1]}
    # 后端服务器ip 
    portmin=${buff[2]}
    portmin=$[portmin-1]
    # 设置最小端口,后面网站按照顺序自动加1

    
    projectNmameList=(   www     work    sou     enchantment guzheng     zhoubao gcc     xibin  inside  rykj    phpmyadmin  telephone       )   
    port=(               62010   62011   62012   62013       62014       62015   62016   62017  62018   62210   62211       62212           )
    # 63000~65535  端口被 Vstpd 被动模式端口占用

    cd $wwwconf; sudo chmod 777 -R $wwwconf 

    if [ ! -e eiscwwwconf.tar.gz ]
    then
      $sudoc tar -czvf eiscwwwconf.tar.gz *
      $sudoc rm -rf `ls | grep -v eiscwwwconf.tar.gz`
    fi

    $sudoc rm -rf `ls | grep -v eiscwwwconf.tar.gz`

    projectDirList=(`ls $www | grep -vE "ssl|wwwconf|wwwroot|*.sh|code"`)    
    echo "数组所有成员为: ${port[*]} "
    echo "在路径下:$www  所有网站文件夹名字为:${projectDirList[*]} " ; sleep 1

    a=0;
    for projectDir in ${projectDirList[*]}
    do 
        number=0
        foundFlag=0
        ((a  )); b=$[portmin a] ; portlist[$c]=$b ; ((c  ))
        # 打印一次变量,接收一个元素存入数组: portlist
        if [ ! -e $www/$projectDir/cgi-bin ]
        then
            sudo mkdir $www/$projectDir/cgi-bin
        fi

        # 遍历数组
        for projectName in "${projectNmameList[@]}"
        do
            if [[ "$projectName" == "$projectDir" ]]
            then
                foundFlag=1
                b=${port[number]} ;   echo "#------- 元素 $projectDir 是数组 projectNmameList 中的第 $number 个元素,被手动指定固定端口  $b -------#" 
                break
            fi
            ((number  ))
        done

        # 如果没有找到元素
        if [[ $foundFlag -eq 0 ]]; then
            echo "元素 '$projectDir' 不在数组中手动配置中,将自动从输入的 端口自增加数"
        fi

        echo "  $projectDir 创建web网站 的端口:   $b 前端域名    $projectDir.$yuming    后端ip port   $ip:$b     c   cgi 解析目录: $projectDir/cgi-bin" 
        echo "  $projectDir 创建web网站 的端口:   $b 前端域名    $projectDir.$yuming    后端ip port   $ip:$b     c   cgi 解析目录: $projectDir/cgi-bin" >> log_www_conf.txt

        #---------  生成前端转发 子站点文件  -----------#
        $sudoc mkdir -p $wwwconf/nginxzhuanfa ; $sudoc chmod 777 $wwwconf/nginxzhuanfa
        YMsetFile=$wwwconf/nginxzhuanfa/$projectDir-$b.conf
        echo "
        server{
        listen 80;
        server_name $projectDir.$yuming;
        location / {
            proxy_pass http://$ip:$b;
            }
        }
        " > $YMsetFile

        if [ "$i" = "www" ]
        then
            NR=`cat -n $YMsetFile | grep server_name | awk -F" " '{print $1}'`
            sed -i "$NR a server_name $projectDir.$yuming $yuming;" $YMsetFile
            sed -i "$NR d" $YMsetFile
        fi      

        if [ "$i" = "gcc" ]
        then
            NR=`cat -n $YMsetFile | grep server_name | awk -F" " '{print $1}'`
            sed -i "$NR a server_name www.okgcc.cn okgcc.cn;" $YMsetFile
            sed -i "$NR d" $YMsetFile
        fi

        #---------- 后端转发子站点 ----------#

        echo "
        server {
            listen       $b;
            root   $www/$projectDir;
            index index.php index.html index.htm index;
            
            add_header 'yuming is ' '$projectDir.$yuming';
            add_header 'ipport is ' '$ip:$b';

            location ~ .php {

                    fastcgi_pass   unix:/var/run/php-fpm.sock; 
                    #fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                    include        fastcgi_params;
            }

            location /cgi-bin {
                fastcgi_pass  unix:/var/run/fcgiwrap.socket;
                include fastcgi.conf;
            }
        } " > $wwwconf/$b.$projectDir.conf

        if [[ "$projectDir" = "work" ]]
        then
            NR=`cat -n $wwwconf/$b.$projectDir.conf | grep location | grep ".php" | awk -F" " '{print $1}'`
            sed -i "$NR i  charset utf-8;}"                  $wwwconf/$b.$projectDir.conf
            sed -i "$NR i  index index.html index.htm;"      $wwwconf/$b.$projectDir.conf
            sed -i "$NR i  autoindex on;"                    $wwwconf/$b.$projectDir.conf
            sed -i "$NR i  location /ruanjian/ {"            $wwwconf/$b.$projectDir.conf 
        fi  
        # 开启 文件浏览功能

    done

    echo "[ok]创建网站日志存放在:   $wwwconf/log_www_conf.txt"
    echo "[ok]前端转发 nginx 子文件目录: $wwwconf/nginxzhuanfa"    
}       


run_cgibin(){

    cgiport=10000
    psnet=`sudo netstat -ntlp | grep -w $cgiport `
    echo "启动 cgi-bin c   环境,端口为:$cgiport"

    if [ ${#psnet} -lt 1 ]
    then 
        sudo spawn-fcgi -f /usr/sbin/fcgiwrap -a 127.0.0.1 -p $cgiport -F 32 -P /tmp/fastcgi-c.pid -u $USER -g $USER
        # 启动
    fi
    
}

nginx_config_if()
{
  echo "
    #----------- 配置子站点nginx ------------#  
    请输入 前端转发服务器域名,后端服务器公网ip 和 第一个网站端口。有多个网站,端口自动计数加1 。
    如果没有域名 和前端服务器,随意输入三个参数,但端口一定为 65536 以内。脚本可以重复执行,重新配置
    "

    read -p "是否进行配置: y 需要配置, 任意按键并回车跳过, 请输入: " ifa
    if [ "$ifa" = "y" ]
    then 
        echo "正在进行配置 子站点..."
        create_www_conf 
        # 配置子站点

    else
	        echo "[ok]已经跳过nginx配置子站点!"
    fi


    echo "[runing] 配置 cgi-bin  c   web  网站开发环境,是否安装?" 
    read -p " y/n? : " ifcgi
    if [ "$ifcgi" = "y" ]
    then 
        cgi_install_config
    fi

    
}

mysql_data(){
#------ 修改mysql data 数据目录 -----#
  $sudoc chmod 777 /eisc/
  $sudoc mkdir /eisc/data/
  $sudoc chown -R mysql:mysql /eisc/data/
  $sudoc chmod 777 /usr/local/mariadb-10.9.2/data/
  $sudoc cp -r /usr/local/mariadb-10.9.2/data/*  /eisc/data/
  $sudoc /usr/local/mariadb-10.9.2/support-files/mysql.server stop
  $sudoc /usr/local/mariadb-10.9.2/support-files/mysql.server start
}

mysql_database(){
#!/bin/bash
# shell 自动创建数据库 用户和密码
$sudoc apt install libncurses*
	# 解决mysql 命令报错,缺少库:Couldn't find any package by glob 'libncurses.so.5'

$sudoc apt install mysql-client  -y
# 安装连接数据库工具
$sudoc mkdir /var/run/mysqld/
$sudoc ln -s /tmp/mysql.sock /var/run/mysqld/mysqld.sock
# 解决 mysql 报错,无法进入mysql 。 mariadb 的启动sock 不一样

    runmysql=`sudo netstat -nltp | grep 3306 | grep -w tcp | grep mariadb`
    if [ ${#runmysql} -lt 1 ]
    then
      $sudoc /etc/init.d/mariadb1011 start
    fi
    
$sudoc $setdir/bin/mysqladmin  -u root password eisc.cn
# 启动数据库,重置数据库 root 用户密码为: eisc.cn

  echo "#---------------- 数据库管理工具 ------------#

参考输入案例: create eisc.cn www www 000000 localhost

  1 创建 or 删除,输入: create 或 drop 
  2 数据库 root 用户密码
  3 子数据库名
  4 子库用户名 
  5 子库密码 
  6 开放数据库方式:本地/远程 输入: localhost 或 %    
	  用空格隔开,必须按照顺序输入6个参数!"
  read -p "请输入:" in_buff
  buff=( abcd $in_buff);

  echo "你输入的信息为: ${buff[*]}";

  case ${buff[1]} in 
  "create")
#    mysql -uroot -p${buff[2]} -e "create database ${buff[3]} character set utf8 collate utf8_bin;"
    mysql -uroot -p${buff[2]} -e "create database ${buff[3]} character set utf8;"
    mysql -uroot -p${buff[2]} -e "grant all on ${buff[4]}.* to '${buff[4]}'@'${buff[6]}' identified by '${buff[5]}'"
    mysql -uroot -p${buff[2]} -e "show databases;SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS ListUsername FROM mysql.user where User='${buff[4]}';"
  ;;
  "drop")
    mysql -uroot -p${buff[2]} -e "drop database ${buff[3]}"
    mysql -uroot -p${buff[2]} -e "drop user '${buff[4]}'@'${buff[6]}'"
    mysql -uroot -p${buff[2]} -e "show databases;SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS ListUsername FROM mysql.user where User='${buff[4]}';"
  ;;
  esac
}

mysql_contor(){


    runmysql=`$sudoc netstat -nltp | grep 3306 | grep -w tcp | grep mariadb`
    if [ ${#runmysql} -lt 1 ]
    then
      sudo /etc/init.d/mariadb1011 start
    fi
  # 启动数据库
  echo "#----  数据库 root 密码始终被重置为 eisc.cn  ----# 
  如果需要手动重置为自己想要的密码,命令行手动执行命令:
  sudo $setdir/bin/mysqladmin  -u root password eisc.cn
  将 eisc.cn 改为自己的密码
  "
  read -p "是否进行数据库管理工作,创建数据库和用户,输入: y/n ? " ins
  if [ "$ins" = "y" ]
  then
    echo "开始管理数据库 ..."
    mysql_database
  else
    echo "[ok]退出数据库管理!"
  fi
}


kaiji_web_service(){
# ubuntu  以 systemd 服务方式,自动启动服务软件 

   systemdDir="/usr/lib/systemd/system"
   webname="eisc_web_start.service"
   cd $systemdDir ; $sudoc touch $webname ; $sudoc chmod 777 $webname 
#----- 写入 start.service 服务 -----#
   echo "
[Unit] 
Description=start web
 
[Service]
Type=forking
ExecStart=/etc/init.d/shell/start.sh
ExecReload=/bin/kill -SIGHUP $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install] 
WantedBy=multi-user.target
   " > $webname

   $sudoc apt install dos2unix -y
   $sudoc dos2unix $webname
   # 转为 unix 格式,否则可能出现字符乱码

   $sudoc chmod 644 $webname
   $sudoc systemctl daemon-reload
   $sudoc systemctl disable $webname
   $sudoc systemctl enable $webname

cd 

}

kaiji_start_web_sh(){
 $sudoc mkdir -p /etc/init.d/shell/ ; $sudoc chmod 777 /etc/init.d/shell/ 


    if [ ! -e /etc/init.d/shell/openvpn_connect.sh ]
    then 
        wget eisc.cn/file/ubuntu/shell/server/openvpn_connect.sh -O /etc/init.d/shell/openvpn_connect.sh
	fi


read -p "是否继续进行配置 systemd 开机启动? y/n: " kaijisz
  if [ "$kaijisz" = "y" ]
  then
   kaiji_web_service
   # 启动这个函数
   $sudoc mkdir -p $installdir/shell ; $sudoc chmod 777 -R $installdir/shell

   # 将脚本授权给 root 用户

   echo "#!/bin/bash
    # bash 解释器 一定要在第一行 开头写,否则下面脚本不执行,开机启动不成功。 由于是 service服务,因此此脚本是 root 用户执行

    # mount -t nfs -o rw,hard,intr 192.168.122.1:/datadisk/eisc /datadisk/eisc/              # 以 nfs 方式挂载磁盘 到 本地的 /datadisk/eisc/  目录
    # mount.cifs //10.111.111.80/datadisk /datadisk -o username=datadisk,password=eisc.cn    # 以 samba 方式挂载磁盘,需要在宿主机上chmod 附加权限; 而在虚拟机上chmod 无效

    /datadisk/eisc/server/nginx/nginx126/sbin/nginx &
    /datadisk/eisc/server/php/php743/sbin/php-fpm &

    #--- mysql ---#
    chown -R mysql:mysql /datadisk/eisc/server/sqldata/*
    chmod 777 -R /datadisk/eisc/server/sqldata/*
    chmod  x /etc/init.d/mariadb1011
    /etc/init.d/mariadb1011 start &
    #-----------------#

    /bin/spawn-fcgi -f /usr/sbin/fcgiwrap -a 127.0.0.1 -p 10000 -F 32 -P /tmp/fastcgi-c.pid &

    sudo -u eisc /datadisk/eisc/server/gitea/gitea &
    #使用 sudo 指定 eisc  用户 运行该 git 仓库软件,root用户无法运行gitea

    touch /home/eisc/log_start_web.txt ; chmod 777 /home/eisc/log_start_web.txt

    # /etc/init.d/shell/openvpn_connect.sh &

    " > /etc/init.d/shell/start.sh
      

   echo "#----------------- 查看写入的开机启动脚本 -------------------#"
   cat /etc/init.d/shell/start.sh

   $sudoc chmod  x /etc/init.d/shell/*  $installdir/nginx/nginx199/sbin/nginx $installdir/php/php743/sbin/php-fpm $installdir/gitea/gitea
   $sudoc chown -R root:root /etc/init.d/shell/*  ; $sudoc chmod 777 -R /etc/init.d/shell/*

  else
    echo "[ok]目前不配置systemd "
  fi
}



main(){

    read -p "首次安装当前需要 chmod 777 权限给目录 chmod 777 -R $downDir $installdir $www   y/n? : " chmodselect
    if [[ $chmodselect = y ]] || [[ $chmodselect = Y ]]
    then
        os_release; down_select ; aptInstall ; RunInstall 
    else 
        echo "跳过前面安装,进入功能管理界面"
    fi

    
    nginx_config_if
    $sudoc $rundir/sbin/nginx ; $sudoc $rundir/sbin/nginx -s reload
    
    # 包含创建 www 子站点 和 c   cgi
    
    mysql_contor ; kaiji_start_web_sh

    run_cgibin

    
}
main


#  ubuntu20/21/22 一键部署 web 环境: wget eisc.cn/file/ubuntu/shell/server/web.sh ; chmod  x web.sh ; ./web.sh 
#  注意需要使用 # sudo 命令会获得权限而 重复安装到默认的  /usr/local/nginx 目录








0 人点赞