diff --git a/scripts/calculate_j.sh b/scripts/calculate_j.sh new file mode 100644 index 00000000..abd51a4c --- /dev/null +++ b/scripts/calculate_j.sh @@ -0,0 +1,59 @@ +#!/bin/bash +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH + +: ' +Copyright (C) 2022 - now HaoZi Technology Co., Ltd. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as published +by the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +' + +# 计算 j 值(通用) +calculate_j() { + export LC_ALL=C + total_mem=$(free -m | awk '/^Mem:/{print $2}') + total_swap=$(free -m | awk '/^Swap:/{print $2}') + total=$((total_mem + total_swap)) + j_value=$((total / 1024)) + cpu_cores=$(nproc) + + if [ $j_value -eq 0 ]; then + j_value=1 + fi + + if [ $j_value -gt "$cpu_cores" ]; then + j_value=$cpu_cores + fi + + echo "$j_value" +} + +# 计算 j 值(2倍内存) +calculate_j2() { + export LC_ALL=C + total_mem=$(free -m | awk '/^Mem:/{print $2}') + total_swap=$(free -m | awk '/^Swap:/{print $2}') + total=$((total_mem + total_swap)) + j_value=$((total / 2024)) + cpu_cores=$(nproc) + + if [ $j_value -eq 0 ]; then + j_value=1 + fi + + if [ $j_value -gt "$cpu_cores" ]; then + j_value=$cpu_cores + fi + + echo "$j_value" +} diff --git a/scripts/mysql/install.sh b/scripts/mysql/install.sh index b7e9d357..4825d89c 100644 --- a/scripts/mysql/install.sh +++ b/scripts/mysql/install.sh @@ -29,8 +29,12 @@ mysqlVersion="" mysqlPassword=$(cat /dev/urandom | head -n 16 | md5sum | head -c 16) cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) + if [[ "${1}" == "80" ]]; then mysqlVersion="8.0.35" + j=$(calculate_j2) elif [[ "${1}" == "57" ]]; then mysqlVersion="5.7.44" else @@ -39,12 +43,6 @@ else exit 1 fi -if [[ "${memTotal}" -lt "4096" ]] && [[ "${1}" == "80" ]]; then - echo -e $HR - echo "错误:这点内存(${memTotal}M)还想装 MySQL 8.0?洗洗睡吧!" - exit 1 -fi - # 安装依赖 if [ "${OS}" == "centos" ]; then dnf makecache -y @@ -125,11 +123,7 @@ if [ "$?" != "0" ]; then exit 1 fi -if [[ "${cpuCore}" -gt "1" ]]; then - make -j2 -else - make -fi +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:MySQL 编译失败,请截图错误信息寻求帮助。" @@ -394,6 +388,9 @@ systemctl enable mysqld systemctl start mysqld ${mysqlPath}/bin/mysqladmin -u root password ${mysqlPassword} +${mysqlPath}/bin/mysql -uroot -p${mysqlPassword} -e "DROP DATABASE test;" +${mysqlPath}/bin/mysql -uroot -p${mysqlPassword} -e "DELETE FROM mysql.user WHERE user='';" +${mysqlPath}/bin/mysql -uroot -p${mysqlPassword} -e "FLUSH PRIVILEGES;" panel writePlugin mysql${1} ${mysqlVersion} panel writeMysqlPassword ${mysqlPassword} diff --git a/scripts/mysql/update.sh b/scripts/mysql/update.sh index 370301b3..3e7bbac3 100644 --- a/scripts/mysql/update.sh +++ b/scripts/mysql/update.sh @@ -26,11 +26,15 @@ downloadUrl="https://jihulab.com/haozi-team/download/-/raw/main/panel/mysql" setupPath="/www" mysqlPath="${setupPath}/server/mysql" mysqlVersion="" -mysqlPassword=$(cat /dev/urandom | head -n 16 | md5sum | head -c 16) +mysqlPassword=$(panel getSetting mysql_root_password) cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) + if [[ "${1}" == "80" ]]; then mysqlVersion="8.0.35" + j=$(calculate_j2) elif [[ "${1}" == "57" ]]; then mysqlVersion="5.7.44" else @@ -39,12 +43,6 @@ else exit 1 fi -if [[ "${memTotal}" -lt "4096" ]] && [[ "${1}" == "80" ]]; then - echo -e $HR - echo "错误:这点内存(${memTotal}M)还想装 MySQL 8.0?洗洗睡吧!" - exit 1 -fi - # 安装依赖 if [ "${OS}" == "centos" ]; then dnf makecache -y @@ -60,11 +58,8 @@ else exit 1 fi -mysqlUserCheck=$(cat /etc/passwd | grep mysql) -if [ "${mysqlUserCheck}" == "" ]; then - groupadd mysql - useradd -s /sbin/nologin -g mysql mysql -fi +# 停止已有服务 +systemctl stop mysqld # 准备目录 cd ${mysqlPath} @@ -100,7 +95,7 @@ rm -f openssl-1.1.1u.tar.gz.checksum.txt mv openssl-1.1.1u openssl cd openssl ./config --prefix=/usr/local/openssl-1.1 --openssldir=/usr/local/openssl-1.1 -make -j$(nproc) +make "-j${j}" make install echo "/usr/local/openssl-1.1/lib" > /etc/ld.so.conf.d/openssl-1.1.conf ldconfig @@ -118,20 +113,13 @@ if [ "$?" != "0" ]; then exit 1 fi -if [[ "${cpuCore}" -gt "1" ]]; then - make -j2 -else - make -fi +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:MySQL 编译失败,请截图错误信息寻求帮助。" exit 1 fi -# 停止已有服务 -systemctl stop mysqld - # 安装 make install if [ "$?" != "0" ]; then @@ -146,8 +134,13 @@ chmod -R 755 ${mysqlPath} chmod 644 ${mysqlPath}/conf/my.cnf # 启动服务 -systemctl daemon-reload -systemctl enable mysqld +systemctl start mysqld + +# 执行更新后的初始化 +${mysqlPath}/bin/mysql -uroot -p${mysqlPassword} -e "DROP DATABASE test;" +${mysqlPath}/bin/mysql -uroot -p${mysqlPassword} -e "DELETE FROM mysql.user WHERE user='';" +${mysqlPath}/bin/mysql -uroot -p${mysqlPassword} -e "FLUSH PRIVILEGES;" +${mysqlPath}/bin/mysql_upgrade -uroot -p${mysqlPassword} --force panel writePlugin mysql${1} ${mysqlVersion} diff --git a/scripts/openresty/install.sh b/scripts/openresty/install.sh index 9c5302d5..c64e3c49 100644 --- a/scripts/openresty/install.sh +++ b/scripts/openresty/install.sh @@ -27,6 +27,9 @@ openrestyPath="${setupPath}/server/openresty" openrestyVersion="1.21.4.3" cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) + # 安装依赖 if [ "${OS}" == "centos" ]; then dnf makecache -y @@ -191,7 +194,7 @@ rm -f libinjection-3.10.0.zip rm -f libinjection-3.10.0.zip.checksum.txt cd ../ -make -j$(nproc) +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:OpenResty waf拓展初始化失败,请截图错误信息寻求帮助。" @@ -238,11 +241,7 @@ export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH export LIB_UTHASH=${openrestyPath}/src/uthash ./configure --user=www --group=www --prefix=${openrestyPath} --with-luajit --add-module=${openrestyPath}/src/ngx_cache_purge --add-module=${openrestyPath}/src/nginx-sticky-module --with-openssl=${openrestyPath}/src/openssl --with-pcre=${openrestyPath}/src/pcre --with-http_v2_module --with-http_slice_module --with-threads --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt="-Wl,-E" --with-cc-opt="-std=gnu99" --with-http_dav_module --add-module=${openrestyPath}/src/nginx-dav-ext-module --add-module=${openrestyPath}/src/ngx_brotli --add-module=${openrestyPath}/ngx_waf -if [[ "${cpuCore}" -gt "1" ]]; then - make -j2 -else - make -fi +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:OpenResty编译失败,请截图错误信息寻求帮助。" diff --git a/scripts/php/install.sh b/scripts/php/install.sh index 10e99458..9297462f 100644 --- a/scripts/php/install.sh +++ b/scripts/php/install.sh @@ -28,6 +28,9 @@ phpVersionCode="" phpPath="${setupPath}/server/php/${phpVersion}" cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) + # 安装依赖 if [ "${OS}" == "centos" ]; then dnf install dnf-plugins-core -y @@ -103,7 +106,7 @@ if [ "${phpVersion}" -le "80" ]; then mv openssl-1.1.1u openssl cd openssl ./config --prefix=/usr/local/openssl-1.1 --openssldir=/usr/local/openssl-1.1 - make -j$(nproc) + make "-j${j}" make install echo "/usr/local/openssl-1.1/lib" > /etc/ld.so.conf.d/openssl-1.1.conf ldconfig @@ -123,11 +126,7 @@ else fi # 编译安装 -if [[ "${cpuCore}" -gt "1" ]]; then - make -j2 -else - make -fi +make "-j${j}" make install if [ ! -f "${phpPath}/bin/php" ]; then echo -e $HR @@ -144,7 +143,7 @@ mkdir -p ${phpPath}/etc cd ${phpPath}/src/ext/zip ${phpPath}/bin/phpize ./configure --with-php-config=${phpPath}/bin/php-config -make -j${cpuCore} +make "-j${j}" make install if [ "$?" != "0" ]; then echo -e $HR diff --git a/scripts/postgresql/install.sh b/scripts/postgresql/install.sh index 7afea1e1..82b4b63b 100644 --- a/scripts/postgresql/install.sh +++ b/scripts/postgresql/install.sh @@ -26,7 +26,9 @@ downloadUrl="https://jihulab.com/haozi-team/download/-/raw/main/panel/postgresql setupPath="/www" postgresqlPath="${setupPath}/server/postgresql" postgresqlVersion="" -cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) + +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) if [[ "${1}" == "15" ]]; then postgresqlVersion="15.5" @@ -88,7 +90,7 @@ if [ "$?" != "0" ]; then rm -rf ${postgresqlPath} exit 1 fi -make -j${cpuCore} +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:PostgreSQL 编译失败,请截图错误信息寻求帮助。" diff --git a/scripts/postgresql/update.sh b/scripts/postgresql/update.sh index 9b71dc30..8bf90d26 100644 --- a/scripts/postgresql/update.sh +++ b/scripts/postgresql/update.sh @@ -26,7 +26,9 @@ downloadUrl="https://jihulab.com/haozi-team/download/-/raw/main/panel/postgresql setupPath="/www" postgresqlPath="${setupPath}/server/postgresql" postgresqlVersion="" -cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) + +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) if [[ "${1}" == "15" ]]; then postgresqlVersion="15.5" @@ -82,7 +84,7 @@ if [ "$?" != "0" ]; then echo "错误:PostgreSQL 编译初始化失败,请截图错误信息寻求帮助。" exit 1 fi -make -j${cpuCore} +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:PostgreSQL 编译失败,请截图错误信息寻求帮助。" diff --git a/scripts/pureftpd/install.sh b/scripts/pureftpd/install.sh index 66e35a29..daa8c2c1 100644 --- a/scripts/pureftpd/install.sh +++ b/scripts/pureftpd/install.sh @@ -25,6 +25,9 @@ setupPath="/www" pureftpdPath="${setupPath}/server/pure-ftpd" pureftpdVersion="1.0.50" +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) + # 准备安装目录 rm -rf ${pureftpdPath} mkdir -p ${pureftpdPath} @@ -53,7 +56,7 @@ if [ "$?" != "0" ]; then exit 1 fi -make +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:Pure-Ftpd-${pureftpdVersion}编译失败,请截图错误信息寻求帮助。" diff --git a/scripts/pureftpd/update.sh b/scripts/pureftpd/update.sh index fc5342bd..d4230008 100644 --- a/scripts/pureftpd/update.sh +++ b/scripts/pureftpd/update.sh @@ -25,6 +25,9 @@ setupPath="/www" pureftpdPath="${setupPath}/server/pure-ftpd" pureftpdVersion="1.0.50" +source ${setupPath}/panel/scripts/calculate_j.sh +j=$(calculate_j) + # 准备安装目录 cp ${pureftpdPath}/etc/pureftpd.passwd /tmp/pureftpd.passwd cp ${pureftpdPath}/etc/pureftpd.pdb /tmp/pureftpd.pdb @@ -57,7 +60,7 @@ if [ "$?" != "0" ]; then exit 1 fi -make +make "-j${j}" if [ "$?" != "0" ]; then echo -e $HR echo "错误:Pure-Ftpd-${pureftpdVersion}编译失败,请截图错误信息寻求帮助。"