温馨提示×

centos上php版本怎么升级

小樊
48
2025-07-22 16:49:35
栏目: 编程语言

在CentOS上升级PHP版本,可以按照以下步骤进行:

方法一:使用Remi仓库

  1. 安装Remi仓库

    sudo yum install epel-release sudo yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm 
  2. 启用Remi仓库

    sudo yum-config-manager --enable remi-php74 # 替换为你想要的PHP版本,例如php74, php80等 
  3. 更新PHP

    sudo yum update php\* 
  4. 重启Web服务器: 如果你使用的是Apache:

    sudo systemctl restart httpd 

    如果你使用的是Nginx和PHP-FPM:

    sudo systemctl restart nginx sudo systemctl restart php-fpm 

方法二:手动编译安装

  1. 安装依赖包

    sudo yum install -y gcc make autoconf libc-dev zlib-devel libxml2-dev bzip2-dev curl-devel libjpeg-devel libpng-devel freetype-devel gmp-devel libmcrypt-devel mariadb-devel aspell-devel recode-devel autoconf 
  2. 下载PHP源码

    wget https://www.php.net/distributions/php-7.4.33.tar.gz # 替换为你想要的PHP版本 tar -zxvf php-7.4.33.tar.gz cd php-7.4.33 
  3. 配置编译选项

    ./configure --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-mbstring \ --with-curl \ --with-gd \ --with-jpeg-dir=/usr \ --with-png-dir=/usr \ --with-freetype-dir=/usr \ --with-zlib \ --with-mysqli \ --with-pdo-mysql \ --with-gettext \ --with-openssl \ --with-xsl \ --enable-zip 
  4. 编译并安装

    make && sudo make install 
  5. 配置PHP: 将/usr/local/php/etc/php.ini-production复制到/usr/local/php/etc/php.ini

    cp /usr/local/php/etc/php.ini-production /usr/local/php/etc/php.ini 
  6. 更新环境变量: 编辑~/.bashrc/etc/profile,添加以下行:

    export PATH=/usr/local/php/bin:$PATH 

    然后使其生效:

    source ~/.bashrc # 或 source /etc/profile 
  7. 重启Web服务器: 同方法一中的步骤。

注意事项

  • 备份数据:在进行任何升级操作之前,务必备份你的网站数据和配置文件。
  • 测试环境:建议先在测试环境中进行升级操作,确保没有问题后再应用到生产环境。
  • 依赖关系:手动编译安装时,确保所有依赖包都已正确安装。

通过以上步骤,你应该能够在CentOS上成功升级PHP版本。

0