search
HomeBackend DevelopmentPHP TutorialPHP源码编译安装管理常用脚本

#!/bin/sh# 编译安装管理PHPApp=phpAppName=PHPAppBase=/AppAppDir=$AppBase/$AppAppProg=$AppDir/sbin/php-fpmAppIni=$AppDir/etc/php.iniAppConf=$AppDir/etc/php-fpm.confExtensionDir=$($AppDir/bin/php-config --extension-dir)AppSrcBase=/App/srcAppSrcFile=$App-*.tar.*AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///')AppUser=$(grep "^[[:space:]]*user" $AppConf 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g")AppGroup=$(grep "^[[:space:]]*group" $AppConf 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g")AppPidDir=$(dirname $(grep "^[[:space:]]*pid" $AppConf 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g") 2> /dev/null)AppErrorLogDir=$(dirname $(grep "^[[:space:]]*error_log" $AppConf 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g") 2> /dev/null)AppSlowLogDir=$(dirname $(grep "^[[:space:]]*slowlog" $AppConf 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g") 2> /dev/null)UploadTmpDir=$(grep "^[[:space:]]*upload_tmp_dir" $AppIni 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g")grep "^session.save_handler" $AppIni 2> /dev/null | grep -q "files"[ $? -eq 0 ] && SessionDir=$(grep "^[[:space:]]*session.save_path" $AppIni 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g")CacheDir=$(grep "^[[:space:]]*eaccelerator.cache_dir" $AppIni 2> /dev/null | awk -F= '{print $2}' | sed -e 's/[[:space:]]//g' -e 's/"//g' -e "s/'//g")AppUser=${AppUser:-nobody}AppGroup=${AppGroup:-nobody}AppPidDir=${AppPidDir:=$AppDir/var/run}AppErrorLogDir=${AppErrorLogDir:-$AppDir/var/log}AppSlowLogDir=${AppSlowLogDir:-$AppDir/var/log}RemoveFlag=0InstallFlag=0# 获取PIDfpid(){    AppMasterPid=$(ps ax | grep "php-fpm: master process" | grep -v "grep" | awk '{print $1}' 2> /dev/null)    AppWorkerPid=$(ps ax | grep "php-fpm: pool" | grep -v "grep" | awk '{print $1}' 2> /dev/null)}# 查询状态fstatus(){    fpid    if [ ! -f "$AppProg" ]; then            echo "$AppName 未安装"    else        echo "$AppName 已安装"        if [ -z "$AppMasterPid" ]; then            echo "$AppName 未启动"        else            echo "$AppName 正在运行"        fi    fi}# 删除fremove(){    fpid    RemoveFlag=1    if [ -z "$AppMasterPid" ]; then        if [ -d "$AppDir" ]; then            rm -rf $AppDir && echo "删除 $AppName"        else            echo "$AppName 未安装"        fi    else        echo "$AppName 正在运行" && exit    fi}# 备份fbackup(){    Day=$(date +%Y-%m-%d)    BackupFile=$App.$Day.tgz    if [ -f "$AppProg" ]; then        cd $AppBase        tar zcvf $BackupFile --exclude=var/log/* --exclude=var/run/* $App --backup=numbered        [ $? -eq 0 ] && echo "$AppName 备份成功" || echo "$AppName 备份失败"    else        echo "$AppName 未安装"    fi}# 安装finstall(){    fpid    InstallFlag=1    if [ -z "$AppMasterPid" ]; then        test -f "$AppProg" && echo "$AppName 已安装"        [ $? -ne 0 ] && fupdate && fcpconf    else        echo "$AppName 正在运行"    fi}# 拷贝配置fcpconf(){    cp -vf --backup=numbered $ScriptDir/php.ini $AppIni    cp -vf --backup=numbered $ScriptDir/php-fpm.conf $AppConf}# 更新fupdate(){    Operate="更新"    [ $InstallFlag -eq 1 ] && Operate="安装"    [ $RemoveFlag -ne 1 ] && fbackup    cd $AppSrcBase    test -d "$AppSrcDir" && rm -rf $AppSrcDir    tar Jxf $AppSrcFile || tar jxf $AppSrcFile || tar zxf $AppSrcFile    cd $AppSrcDir    ./configure \    "--prefix=$AppDir" \    "--disable-all" \    "--enable-fpm" \    "--enable-opcache" \    "--enable-pdo" \    "--enable-session" \    "--with-pcre-dir" \    "--with-pdo-mysql=mysqlnd"    [ $? -eq 0 ] && make && make install    if [ $? -eq 0 ];then        echo "$AppName $Operate成功"    else        echo "$AppName $Operate失败"        exit 1    fi}# 初始化finit(){    echo "初始化 $AppName"    id -gn $AppGroup &> /dev/null    if [ $? -ne 0 ]; then        groupadd $AppGroup && echo "新建 $AppName 运行组:$AppGroup"    else        echo "$AppName 运行组:$AppGroup 已存在"    fi    id -un $AppUser &> /dev/null    if [ $? -ne 0 ]; then        useradd -s /bin/false -g $AppGroup -M $AppUser        if [ $? -eq 0 ]; then            echo "新建 $AppName 运行用户:$AppUser"            echo "S0nGPhb693$" | passwd --stdin $AppUser &> /dev/null        fi    else        echo "$AppName 运行用户:$AppUser 已存在"    fi    echo $AppPidDir | grep -q "^/"    if [ $? -eq 1 ]; then        AppPidDir=$AppDir/var/$AppPidDir    fi    if [ ! -e "$AppPidDir" ]; then        mkdir -p $AppPidDir && echo "新建 $AppName PID文件存放目录:$AppPidDir"    else        echo "$AppName PID文件存放目录:$AppPidDir 已存在"    fi    echo $AppErrorLogDir | grep -q "^/"    if [ $? -eq 1 ]; then        AppErrorLogDir=$AppDir/var/$AppErrorLogDir    fi    if [ ! -e "$AppErrorLogDir" ]; then        mkdir -p $AppErrorLogDir && echo "新建 $AppName 错误日志目录:$AppErrorLogDir"    else        echo "$AppErrorLogDir 错误日志目录:$AppErrorLogDir 已存在"    fi    echo $AppSlowLogDir | grep -q "^/"    if [ $? -eq 1 ]; then        AppSlowLogDir=$AppDir/$AppSlowLogDir    fi    if [ ! -e "$AppSlowLogDir" ]; then        mkdir -p $AppSlowLogDir && echo "新建 $AppName 慢日志目录:$AppSlowLogDir"    else        echo "$AppSlowLogDir 慢日志目录:$AppSlowLogDir 已存在"    fi    printf "\n"    if [ -n "$UploadTmpDir" ]; then        echo $UploadTmpDir | grep -q "^/"        if [ $? -eq 0 ]; then            if [ ! -e "$UploadTmpDir" ]; then                mkdir -p $UploadTmpDir && echo "新建 $AppName 文件上传临时存储目录:$UploadTmpDir"            else                echo "$AppName 文件上传临时存储目录:$UploadTmpDir 已存在"            fi            chown -R $AppUser:$AppGroup $UploadTmpDir && echo "修改 $AppName 文件上传临时存储目录拥有者为 $AppUser,属组为 $AppGroup"            printf "\n"        fi    fi    if [ -n "$SessionDir" ]; then        echo $SessionDir | grep -q "^/"        if [ $? -eq 0 ]; then            if [ ! -e "$SessionDir" ]; then                mkdir -p $SessionDir && echo "新建 $AppName 会话存储目录:$SessionDir"            else                echo "$AppName 会话存储目录:$SessionDir 已存在"            fi            chown -R $AppUser:$AppGroup $SessionDir && echo "修改 $AppName 会话存储目录拥有者为 $AppUser,属组为 $AppGroup"            printf "\n"        fi    fi    if [ -n "$CacheDir" ]; then        echo $CacheDir | grep -q "^/"        if [ $? -eq 0 ]; then            if [ ! -e "$CacheDir" ]; then                mkdir -p $CacheDir && echo "新建 eAccelerator 缓存目录:$CacheDir"            else                echo "eAccelerator 缓存目录:$CacheDir 已存在"            fi            chown -R $AppUser:$AppGroup $CacheDir && echo "修改 eAccelerator 缓存目录拥有者为 $AppUser,属组为 $AppGroup"        fi    fi    sed -i "s|extension_dir.*$|extension_dir = \"$ExtensionDir\"|" $AppIni}# 检查配置ftest(){    $AppProg -t && echo "$AppName 配置正确" || echo "$AppName 配置错误"}# 启动fstart(){    fpid    if [ -n "$AppMasterPid" ]; then        echo "$AppName 正在运行"    else        $AppProg -c $AppIni && echo "启动 $AppName" || echo "$AppName 启动失败"    fi}# 停止fstop(){    fpid    if [ -n "$AppMasterPid" ]; then        kill -INT $AppMasterPid && echo "停止 $AppName" || echo "$AppName 停止失败"    else        echo "$AppName 未启动"    fi}# 重载配置freload(){    fpid    if [ -n "$AppMasterPid" ]; then        kill -USR2 $AppMasterPid && echo "重载 $AppName 配置" || echo "$AppName 重载配置失败"    else        echo "$AppName 未启动"    fi}# 重启frestart(){    fpid    [ -n "$AppMasterPid" ] && fstop && sleep 1    fstart}# 终止进程fkill(){    fpid    if [ -n "$AppMasterPid" ]; then        echo "$AppMasterPid" | xargs kill -9        if [ $? -eq 0 ]; then            echo "终止 $AppName 主进程"        else            echo "终止 $AppName 主进程失败"        fi    else        echo "$AppName 主进程未运行"    fi    if [ -n "$AppWorkerPid" ]; then        echo "$AppWorkerPid" | xargs kill -9        if [ $? -eq 0 ]; then            echo "终止 $AppName 工作进程"        else            echo "终止 $AppName 工作进程失败"        fi    else        echo "$AppName 工作进程未运行"    fi}ScriptDir=$(cd $(dirname $0); pwd)ScriptFile=$(basename $0)case "$1" in    "install"   ) finstall;;    "update"    ) fupdate;;    "reinstall" ) fremove && finstall;;    "remove"    ) fremove;;    "backup"    ) fbackup;;    "init"      ) finit;;    "start"     ) fstart;;    "stop"      ) fstop;;    "restart"   ) frestart;;    "status"    ) fstatus;;    "cpconf"    ) fcpconf;;    "test"      ) ftest;;    "reload"    ) freload;;    "kill"      ) fkill;;    *           )    echo "$ScriptFile install              安装 $AppName"    echo "$ScriptFile update               更新 $AppName"    echo "$ScriptFile reinstall            重装 $AppName"    echo "$ScriptFile remove               删除 $AppName"    echo "$ScriptFile backup               备份 $AppName"    echo "$ScriptFile init                 初始化 $AppName"    echo "$ScriptFile start                启动 $AppName"    echo "$ScriptFile stop                 停止 $AppName"    echo "$ScriptFile restart              重启 $AppName"    echo "$ScriptFile status               查询 $AppName 状态"    echo "$ScriptFile cpconf               拷贝 $AppName 配置"    echo "$ScriptFile test                 检查 $AppName 配置"    echo "$ScriptFile reload               重载 $AppName 配置"    echo "$ScriptFile kill                 终止 $AppName 进程"    ;;esac


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools