首页 教程

PPress 一键安装脚本 install.sh 解释

PPress 一键安装脚本 install.sh 的代码解释,这是在Centos7下一键安装的全自动化脚本。

一、整体功能:

这个脚本是一个安装脚本,用于安装 PPress 软件。它会检查系统环境,根据网络位置设置不同的软件源,安装 Python 及其依赖(如果需要),克隆 PPress 代码库,安装 PPress 的 Python 依赖,配置防火墙,启动 PPress 服务,并创建一个名为 ppress 的命令行工具来管理 PPress 服务。

二、代码结构和功能解释: 用户权限检查:

if [ $(id -u) -ne 0 ]; then
    echo "This script must be run as root" 1>&2
    exit 1
fi

检查当前用户是否为 root 用户,如果不是则输出错误信息并退出脚本。

PPress 安装开始提示:

echo "Starting PPress installation..."

打印开始安装的信息。

Python 版本检查:

echo "Checking Python version..."
if command -v python3 >/dev/null 2>&1; then
    PYTHON_VERSION=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
    if [ "$(echo "$PYTHON_VERSION >= 3.8" | bc)" -eq 1 ]; then
        echo "Python $PYTHON_VERSION is already installed and meets requirements."
        INSTALL_PYTHON=0
    else
        echo "Python $PYTHON_VERSION is too old, need >= 3.8"
        INSTALL_PYTHON=1
    fi
else
    echo "Python 3 not found"
    INSTALL_PYTHON=1
fi

检查系统是否已安装 Python3 并检查其版本是否大于等于 3.8。如果满足,则设置 INSTALL_PYTHON 为 0,表示无需安装 Python,否则为 1。

网络位置检查:

echo "Checking your location..."
if curl -s --connect-timeout 3 --max-time 5 www.google.com &>/dev/null; then
    echo "Detected international network..."
    is_china_ip=0
else
    echo "Detected China network..."
    is_china_ip=1
fi

通过 curl 访问 www.google.com 并根据能否成功连接来判断是否处于中国网络,将结果存储在 is_china_ip 变量中。

设置仓库地址:

if [ "$is_china_ip" -eq 1 ]; then
    echo "Using Gitee repository..."
    export REPOSITORY_URL='https://gitee.com/fojie/PPress.git'
else
    echo "Using GitHub repository..."
    export REPOSITORY_URL='https://github.com/yandaozi/PPress.git'
fi

根据网络位置设置 PPress 代码的仓库地址,使用 Gitee 或 GitHub 仓库。

Python 安装部分(如果需要):

if [ "$INSTALL_PYTHON" -eq 1 ]; then
    # 设置 Python 源
    if [ "$is_china_ip" -eq 1 ]; then
        echo "Using China mirrors..."
        export PYTHON_SOURCE='https://mirrors.huaweicloud.com/python/3.12.3/Python-3.12.3.tgz'
        export PIP_SOURCE='https://pypi.tuna.tsinghua.edu.cn/simple/'
    else
        echo "Using international mirrors..."
        export PYTHON_SOURCE='https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz'
        export PIP_SOURCE='https://pypi.org/simple/'
    fi

    # 安装必要的工具和依赖
    echo "Installing required packages..."
    yum groupinstall -y "Development Tools"
    yum install -y gcc gcc-c++ make \
        openssl-devel bzip2-devel libffi-devel \
        zlib-devel xz-devel sqlite-devel \
        readline-devel tk-devel ncurses-devel \
        gdbm-devel db4-devel expat-devel \
        curl git wget

    # 安装 Python 3.12
    echo "Installing Python 3.12..."
    cd /opt
    echo "Downloading Python source code..."
    wget $PYTHON_SOURCE 2>&1 | \
        while read line; do
            echo -ne "\rDownloading... $line"
        done
    echo -e "\nDownload completed."

    echo "Extracting Python source code..."
    if [ -f "$(basename $PYTHON_SOURCE)" ]; then
        tar -zxf $(basename $PYTHON_SOURCE)
    else
        echo "Error: Python source file not found"
        exit 1
    end

    if [! -d "Python-3.12.3" ]; then
        echo "Error: Python source directory not found"
        exit 1
    end

    cd $(basename $PYTHON_SOURCE | sed 's/.tgz//')
    echo "Configuring Python build..."
    # 清理之前的构建
    make clean || true

    # 配置 Python 构建
   ./configure \
        --prefix=/usr/local \
        --enable-shared \
        --with-system-expat \
        --with-system-ffi \
        --enable-loadable-sqlite-extensions \
        --with-ensurepip=yes \
        LDFLAGS="-Wl,-rpath /usr/local/lib"

    echo "Building Python (this may take a while)..."
    # 编译和安装
    CPU_CORES=$(nproc)
    echo "Using $CPU_CORES CPU cores for compilation..."
    make -j$CPU_CORES
    make install

    # 创建软链接
    ln -sf /usr/local/bin/python3.12 /usr/local/bin/python3
    ln -sf /usr/local/bin/pip3.12 /usr/local/bin/pip3
fi

如果需要安装 Python,根据网络位置设置 Python 和 pip 的源,安装开发工具和 Python 所需的依赖包。下载 Python 3.12.3 源码,解压,配置构建,编译并安装。创建 python3 和 pip3 的软链接。 配置 pip 源:

mkdir -p ~/.pip
if [ "$is_china_ip" -eq 1 ]; then
    echo "[global]
    index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
    trusted-host = pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf
fi

根据网络位置设置 pip 的默认源,对于中国网络使用清华源。

克隆 PPress 代码:

echo "Cloning PPress repository..."
cd /opt
git clone --depth=1 $REPOSITORY_URL
cd PPress

克隆 PPress 代码库到 /opt 目录并进入该目录。

安装 PPress 依赖:

echo "Installing PPress dependencies..."
pip3.12 install -r requirements.txt -i $PIP_SOURCE --no-cache-dir -U

安装 PPress 的 Python 依赖,使用之前设置的 pip 源,不使用缓存。

端口设置和防火墙配置:

echo "Installation completed!"
read -p "Please enter the port on which you want to run PPress (default: 5000): " PORT
PORT=${PORT:-5000}

# 配置防火墙
echo "Configuring firewall..."
if command -v firewall-cmd >/dev/null 2>&1; then
    # 如果是 firewalld
    firewall-cmd --permanent --add-port=${PORT}/tcp
    firewall-cmd --reload
    echo "Firewall port ${PORT} opened (firewalld)"
elif command -v iptables >/dev/null 2>&1; then
    # 如果是 iptables
    iptables -I INPUT -p tcp --dport ${PORT} -j ACCEPT
    service iptables save
    echo "Firewall port ${PORT} opened (iptables)"
else
    echo "Warning: No firewall management tool found"
fi

让用户输入 PPress 运行的端口,默认为 5000。根据系统使用的防火墙工具(firewalld 或 iptables)打开相应端口。

运行 PPress:

echo "Starting PPress on port $PORT..."
# 运行 PPress
export FLASK_APP=run.py
flask run --host=0.0.0.0 --port=$PORT

启动 PPress 服务,使用 Flask 运行,监听在 0.0.0.0 地址和用户指定的端口。

创建 ppress 命令:

echo "Creating ppress command..."
cat > /usr/local/bin/ppress << 'EOF'
#!/bin/bash

# PPress 配置文件
CONFIG_FILE="/etc/ppress.conf"
PID_FILE="/var/run/ppress.pid"
LOG_FILE="/var/log/ppress.log"

# 如果配置文件不存在则创建
if [! -f "$CONFIG_FILE" ]; then
    echo "PORT=5000" > "$CONFIG_FILE"
fi

# 加载配置
source "$CONFIG_FILE"

# 获取运行状态
get_status() {
    if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
        local pid=$(cat "$PID_FILE")
        local port=$(grep "PORT=" "$CONFIG_FILE" | cut -d= -f2)
        echo "Status: Running"
        echo "PID: $pid"
        echo "Port: $port"
        echo "URL: http://$(hostname -I | awk '{print $1}'):$port"
    else
        echo "Status: Stopped"
    end
}

# 启动服务
start() {
    if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
        echo "PPress is already running."
        get_status
        return
    end
    
    cd /opt/PPress
    export FLASK_APP=run.py
    nohup flask run --host=0.0.0.0 --port=$PORT > "$LOG_FILE" 2>&1 & echo $! > "$PID_FILE"
    echo "PPress started."
    get_status
}

# 停止服务
stop() {
    if [ -f "$PID_FILE" ]; then
        kill $(cat "$PID_FILE")
        rm "$PID_FILE"
        echo "PPress stopped."
    else
        echo "PPress is not running."
    end
}

# 重启服务
restart() {
    stop
    sleep 2
    start
}

# 修改端口
change_port() {
    read -p "Enter new port number: " new_port
    if [[ "$new_port" =~ ^[0-9]+$ ]]; then
        # 保存旧端口号
        old_port=$PORT
        
        # 配置防火墙
        if command -v firewall-cmd >/dev/null 2>&1; then
            firewall-cmd --permanent --remove-port=$old_port/tcp
            firewall-cmd --permanent --add-port=$new_port/tcp
            firewall-cmd --reload
        elif command -v iptables >/dev/null 2>&1; then
            iptables -D INPUT -p tcp --dport $old_port -j ACCEPT
            iptables -I INPUT -p tcp --dport $new_port -j ACCEPT
            service iptables save
        end
        
        # 更新配置文件
        sed -i "s/PORT=.*/PORT=$new_port/" "$CONFIG_FILE"
        # 重新加载配置
        source "$CONFIG_FILE"
        echo "Port changed to $new_port"
        
        # 如果服务正在运行,则重启
        if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
            echo "Restarting PPress with new port..."
            stop
            sleep 2
            start
        else
            echo "PPress is not running. Start it with 'ppress start' to use the new port."
        end
    else
        echo "Invalid port number"
    end
}

# 显示帮助信息
show_help() {
    echo "Usage: ppress <command>"
    echo "Commands:"
    echo "  status    - Show current status"
    echo "  start     - Start PPress"
    echo "  stop      - Stop PPress"
    echo "  restart   - Restart PPress"
    echo "  port      - Change port"
    echo "  help      - Show this help"
}

# 主命令处理
case "$1" in
    status)
        get_status
        ;;
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    port)
        change_port
        ;;
    help|"")
        show_help
        ;;
    *)
        echo "Unknown command: $1"
        show_help
        exit 1
        ;;
esac
EOF

# 设置执行权限
chmod +x /usr/local/bin/ppress

# 创建初始配置文件
echo "PORT=$PORT" > /etc/ppress.conf

echo "PPress command installed. Type 'ppress help' for usage."

# 启动服务
ppress start

创建一个名为 ppress 的脚本,存储在 /usr/local/bin 目录中,用于管理 PPress 服务,包括查看状态、启动、停止、重启和修改端口等功能。 为 ppress 脚本设置执行权限。

创建初始配置文件 /etc/ppress.conf,存储端口信息。 最后启动 PPress 服务。

三、可能遇到的问题及解决办法:

权限问题: 确保以 root 用户运行脚本,否则可能无法执行某些操作,如安装软件包、修改系统配置等。 部分命令可能需要使用 sudo 运行。

网络问题: 如果在下载 Python 源码或克隆 PPress 代码库时出现问题,可能是网络问题或源不可用。可以检查网络连接,确保可以访问相应的源。 对于国内用户,使用国内镜像源可能会更稳定。

软件源问题: 可能会遇到软件源无法访问或软件源中软件包版本更新导致的不兼容问题。可以根据错误信息尝试更换软件源或调整软件包版本。

编译问题: 在编译 Python 时,可能会因为缺少依赖或系统资源不足导致编译失败。可以检查 yum 安装依赖的步骤是否正确执行,或增加系统资源(如 CPU、内存)。

四、优化建议:

错误处理:

在更多的关键步骤添加更详细的错误处理,例如在 yum 安装时检查返回值,对 pip 安装时的异常进行处理。

对于每个命令的执行结果进行检查,如 wget、git clone 等,当命令失败时给出更详细的错误信息。

用户输入验证:

在用户输入端口时,除了检查是否为数字,还可以检查是否在合理的端口范围(如 1 到 65535)。

日志记录:

可以在关键步骤添加更多的日志信息,记录到 /var/log/ppress-install.log 或其他日志文件,方便故障排除。

环境清理:

在安装过程中,如果某些步骤失败,可以添加相应的清理步骤,例如在 Python 安装失败时删除已下载的文件和创建的目录,避免残留无用文件。

总之,这个 install.sh 脚本完成了 PPress 软件的安装和服务管理功能,但可以通过以上优化建议使其更加健壮和易于维护。

上一篇: Amore主题 PPress的简洁博客主题
下一篇: 没有了
文章标签
相关文章
评论 (暂无评论)