相关文章:
经常遇到编译安装php或扩展的安装,虽然打了镜像,但也会有升级php的需求,或安装扩展,所以就记录整理了 php安装脚本,以及常用扩展的安装,以此来简化自己的工作。
shell脚本编译安装 php及多个扩展
脚本中大多数扩展下载地址,点击进行搜索
脚本在 rocky linux 9.3系统机器上装的,也已经用于生产环境,之前写过手动一步步安装,这次整理成 shell脚本,并增加了很多扩展安装
#!/bin/bash #auth:zhpengfei #date:2024-03-28 # 全局变量 PROJECT_HOME="/usr/local/data" VERSION="php-8.3.3" PHP_HOME="$PROJECT_HOME/$VERSION" PHP_CONFIG="$PHP_HOME/etc" SOFT_HOME="$PROJECT_HOME/soft" PHP_PKG="$SOFT_HOME/$VERSION" PHP_URL="http://cn2.php.net/distributions/$VERSION.tar.gz" USER="www" GROUP="www" # 准备环境 prepare_environment() { useradd $USER -d /usr/local/data/www -s /sbin/nologin mkdir -p "$SOFT_HOME" } prepare_environment # 定义函数来检查命令执行结果 check_command_result() { if [ $? -ne 0 ]; then echo "Error: $1 failed" exit 1 fi } # 下载并安装 PHP install_php() { cd "$SOFT_HOME" # 下载 PHP wget -nc "$PHP_URL" tar zxf "${VERSION}.tar.gz" # 安装依赖 install_dependencies # 编译安装 PHP compile_install_php # 修改 PHP 配置文件 configure_php } # 安装 PHP 依赖 install_dependencies() { echo "Installing dependencies..." yum install -y wget gcc gcc-c++ autoconf automake libtool make libxml2 libxml2-devel openssl openssl-devel sqlite-devel libpng libpng-devel libpsl libpsl-devel libicu-devel libcurl-devel gmp-devel --enablerepo=devel oniguruma-devel net-snmp-devel epel-release openldap openldap-devel cp -frp /usr/lib64/libldap* /usr/lib/ } # 编译安装 PHP compile_install_php() { echo "Compiling and installing PHP..." cd "${SOFT_HOME}/${VERSION}" ./configure --prefix=$PHP_HOME \ --with-config-file-path=$PHP_CONFIG \ --enable-fpm \ --with-fpm-user=$USER \ --with-fpm-group=$GROUP \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --enable-mysqlnd-compression-support \ --with-zlib \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-intl \ --enable-ftp \ --enable-gd-jis-conv \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --enable-soap \ --with-gettext \ --enable-fileinfo \ --enable-opcache \ --with-pear \ --with-ldap=shared \ --without-gdbm \ --with-curl \ --enable-ctype # 检查 configure 是否成功 check_command_result "Configure" make -j 4 # 检查 make 是否成功 check_command_result "Make" make install # 检查 make 是否成功 check_command_result "Make install" ln -s $PHP_HOME $PROJECT_HOME/php } # 优化 PHP 配置 configure_php() { echo "Configuring PHP..." # 修改 php-fpm.conf cp "$PHP_CONFIG/php-fpm.conf.default" "$PHP_CONFIG/php-fpm.conf" sed -i 's#;error_log = log/php-fpm.log#error_log = log/php-fpm.log#g' "$PHP_CONFIG/php-fpm.conf" sed -i 's/;rlimit_files = 1024/rlimit_files = 65535/g' "$PHP_CONFIG/php-fpm.conf" sed -i 's#;log_level = notice#log_level = error#g' "$PHP_CONFIG/php-fpm.conf" sed -i 's#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#g' "$PHP_CONFIG/php-fpm.conf" sed -i 's#;emergency_restart_threshold = 0#emergency_restart_threshold = 60#g' "$PHP_CONFIG/php-fpm.conf" sed -i 's#;emergency_restart_interval = 0#emergency_restart_interval = 60#g' "$PHP_CONFIG/php-fpm.conf" sed -i 's#;process_control_timeout = 0#process_control_timeout = 5#g' "$PHP_CONFIG/php-fpm.conf" # 修改 www.conf cp "$PHP_CONFIG/php-fpm.d/www.conf.default" "$PHP_CONFIG/php-fpm.d/www.conf" sed -i 's#pm = dynamic#pm = static#g' "$PHP_CONFIG/php-fpm.d/www.conf" sed -i 's#pm.max_children = 5#pm.max_children = 150#g' "$PHP_CONFIG/php-fpm.d/www.conf" sed -i 's#;pm.status_path = /status#pm.status_path = /PhpStatus#g' "$PHP_CONFIG/php-fpm.d/www.conf" sed -i 's#;slowlog = log/$pool.log.slow#slowlog = var/log/$pool.log.slow#g' "$PHP_CONFIG/php-fpm.d/www.conf" sed -i 's#;request_slowlog_timeout = 0#request_slowlog_timeout = 10#g' "$PHP_CONFIG/php-fpm.d/www.conf" # 修改 php.ini cp "$PHP_PKG/php.ini-production" "$PHP_CONFIG/php.ini" sed -i 's/memory_limit = 128M/memory_limit = 512M/g' "$PHP_CONFIG/php.ini" sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#g' "$PHP_CONFIG/php.ini" sed -i 's#expose_php = On#expose_php = Off#g' "$PHP_CONFIG/php.ini" sed -i 's#disable_functions = #disable_functions = phpinfo#g' "$PHP_CONFIG/php.ini" } #安装php扩展可调用 install_php_extension(){ extension_version=$1 extension_name=`echo $extension_version |awk -F'-' '{print $1}'` cd $SOFT_HOME wget -nc -q http://pecl.php.net/get/$extension_version.tgz tar xvf $extension_version.tgz cd $extension_version $PHP_HOME/bin/phpize # 检查 phpize 是否成功 check_command_result "phpize" # 如果存在 configure 脚本,则执行配置 if [ -f "configure" ]; then ./configure --with-php-config=$PHP_HOME/bin/php-config fi # 编译和安装扩展 make && make install # 添加扩展到 php.ini 文件中 if [ "$extension_name" == "libsodium" ];then grep "extension=sodium.so" "$PHP_CONFIG/php.ini" ||echo "extension=sodium.so" >> $PHP_CONFIG/php.ini else grep "$extension_name.so" "$PHP_CONFIG/php.ini" || echo "extension=$extension_name.so" >> "$PHP_CONFIG/php.ini" fi # 输出安装信息 echo "Installation of $extension_name completed." # 打印安装后的文件信息 $PHP_HOME/bin/php --ri $extension_name } #安装redis扩展 install_php_redis(){ redis_version=redis-6.0.2 #调用install_php_extension install_php_extension $redis_version } #安装imagick扩展 install_php_imagick(){ imagick_version="imagick-3.7.0" yum install -y epel-release ImageMagick-devel #调用install_php_extension install_php_extension $imagick_version } install_php_zip(){ #安装之前解决依赖版本低问题 zip_version=zip-1.22.3 yum remove libzip -y cd $SOFT_HOME wget -nc https://nih.at/libzip/libzip-1.2.0.tar.gz tar -zxvf libzip-1.2.0.tar.gz cd libzip-1.2.0 ./configure make && make install cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h install_php_extension $zip_version } #安装swoole扩展 install_php_swoole(){ swoole_version=swoole-5.1.1 install_php_extension $swoole_version } install_php_mongodb(){ yum install -y autoconf mongod_version=mongodb-1.18.1 install_php_extension $mongod_version } install_php_igbinary(){ igbinary_version=igbinary-3.2.15 install_php_extension $igbinary_version } install_php_memcache(){ yum install libmemcached libmemcached-devel zlib zlib-devel Memcache_V=memcache-8.2 install_php_extension $Memcache_V } install_php_memcached(){ #yum install libmemcached libmemcached-devel zlib zlib-devel rpm -i https://artifacts.m6w6.name/libmemcached/1.1.4/RPM/Linux-x86_64-GNU-12.3.0-Release/libmemcached-awesome-1.1.4.rpm ./configure --with-php-config=/usr/local/data/php-8.3.3/bin/php-config --disable-memcached-sasl memcached_V=memcached-3.2.0 install_php_extension $memcached_V } install_php_gd(){ cd /usr/local/data/soft/php-8.3.3/ext/gd /usr/local/data/php/bin/phpize ./configure --with-php-config=/usr/local/data/php/bin/php-config make -j 4 && make install sed -i '$a extension=gd.so' /usr/local/data/php/etc/php.ini } install_php_sodium(){ yum install -y libsodium libsodium-devel sodium_version=libsodium-2.0.23 install_php_extension $sodium_version } #禁用eval函数 install_php_diseval(){ cd $SOFT_HOME git clone https://gitee.com/gongfuxiaomiantuan/PHP_diseval_extension.git; cd PHP_diseval_extension/source ./configure --with-php-config=$PHP_HOME/bin/php-config make && make install echo "extension=diseval.so" >> $PHP_CONFIG/php.ini } install_php_zephir_parser(){ cd $SOFT_HOME yum install -y re2c wget -nc https://github.com/zephir-lang/php-zephir-parser/archive/refs/tags/v1.3.8.tar.gz -O php-zephir-parser-1.3.8.tgz tar zxvf php-zephir-parser-1.3.8.tgz cd php-zephir-parser-1.3.8 $PHP_HOME/bin/phpize ./configure --with-php-config=$PHP_HOME/bin/php-config make && make install echo "extension=zephir_parser.so" >> $PHP_CONFIG/php.ini } case $1 in php) install_php;; redis) install_php_redis;; imagick) install_php_imagick;; zip) install_php_zip;; swoole) install_php_swoole;; mongodb) install_php_mongodb;; igbinary) install_php_igbinary;; memcache) install_php_memcache;; memcached) install_php_memcached;; gd) install_php_gd;; sodium) install_php_sodium;; diseval) install_php_diseval;; zephir_parser) install_php_zephir_parser;; help|*) echo "usage $0 php|redis|imagick|zip|swoole|mongodb|igbinary|memcache|memcached|gd|sodium|diseval|zephir_parser" esac
php安装脚本使用方法
[root@192 ~]# sh php8-install.sh usage php8-install.sh php|redis|imagick|zip|swoole|mongodb|igbinary|memcache|memcached|diseval|zephir_parser
安装php
sh php8_install.sh php
安装php扩展
sh php8-install.sh redis
php进程管理脚本
#! /bin/sh ### BEGIN INIT INFO # Provides: php-fpm # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts php-fpm # Description: starts the PHP FastCGI Process Manager daemon ### END INIT INFO prefix=/usr/local/data/php exec_prefix=${prefix} php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_fpm_PID=${prefix}/var/run/php-fpm.pid php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID" wait_for_pid () { try=0 while test $try -lt 35 ; do case "$1" in 'created') if [ -f "$2" ] ; then try='' break fi ;; 'removed') if [ ! -f "$2" ] ; then try='' break fi ;; esac echo -n . try=`expr $try + 1` sleep 1 done } case "$1" in start) echo -n "Starting php-fpm " $php_fpm_BIN --daemonize $php_opts if [ "$?" != 0 ] ; then echo " failed" exit 1 fi wait_for_pid created $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Gracefully shutting down php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -QUIT `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; status) if [ ! -r $php_fpm_PID ] ; then echo "php-fpm is stopped" exit 0 fi PID=`cat $php_fpm_PID` if ps -p $PID | grep -q $PID; then echo "php-fpm (pid $PID) is running..." else echo "php-fpm dead but pid file exists" fi ;; force-quit) echo -n "Terminating php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -TERM `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $0 stop $0 start ;; reload) echo -n "Reload service php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -USR2 `cat $php_fpm_PID` echo " done" ;; configtest) $php_fpm_BIN -t ;; *) echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}" exit 1 ;; esac
php进程脚本使用方法
/etc/init.d/php-fpm configtest #测试配置文件是否正确 /etc/init.d/php-fpm start #启动php /etc/init.d/php-fpm restart #重启php /etc/init.d/php-fpm reload #重新加载 PHP 配置 /etc/init.d/php-fpm status #查看php状态
总结
shell脚本安装 php8.3.3 和扩展已经使用过很多次了。还是挺方便的,建议收藏哈