LAMP服务器升级指南(Linux+Apache+MySQL/MariaDB+PHP)
LAMP服务器升级需遵循“备份先行、分步升级、兼容性优先、测试验证”的原则,确保服务稳定性和数据安全。以下是详细步骤及注意事项:
备份所有关键数据
/etc/apache2/或/etc/httpd/)、MySQL配置(/etc/mysql/或/etc/my.cnf)、PHP配置(/etc/php/)。/var/www/html/)及数据库(使用mysqldump导出所有数据库:mysqldump -u root -p --all-databases > all-databases.sql)。检查当前版本
apachectl -v(Ubuntu/Debian)或httpd -v(CentOS/RHEL);mysql --version或mariadb --version;php -v。更新系统包及依赖
sudo apt update && sudo apt upgrade -y;sudo yum update -y(或dnf update -y,Fedora)。sudo apt install build-essential libapr1-dev libaprutil1-dev libpcre3-dev;sudo yum groupinstall "Development Tools" -y && sudo yum install apr-devel apr-util-devel pcre-devel openssl-devel -y。推荐方式:包管理器升级(简单安全)
sudo apt upgrade apache2(常规升级)或sudo apt full-upgrade(解决依赖冲突);sudo yum update httpd。sudo systemctl restart apache2(Ubuntu/Debian)或sudo systemctl restart httpd(CentOS/RHEL)。手动编译升级(可选,适用于特定版本)
wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz;tar -xzvf httpd-2.4.54.tar.gz && cd httpd-2.4.54;./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite=shared;make && sudo make install;/etc/apache2/apache2.conf复制到新版本配置目录/usr/local/apache2/conf/),调整路径后启动新版本:sudo /usr/local/apache2/bin/apachectl start。推荐方式:包管理器升级(避免数据丢失)
sudo apt upgrade mysql-server(若从5.7升级到8.0,需先升级到5.7最新小版本);sudo yum update mariadb-server(或mysql-server,取决于安装的数据库类型)。sudo systemctl restart mysql(Ubuntu/Debian)或sudo systemctl restart mariadb(CentOS/RHEL)。mysql_upgrade命令修复表结构:sudo mysql_upgrade -u root -p(输入密码后自动检查并修复)。手动升级(跨大版本必选,如5.7→8.0)
my.cnf),调整basedir(指向新版本安装目录)、default_authentication_plugin(MySQL 8.0默认使用caching_sha2_password,若需兼容旧版本可设为mysql_native_password);sudo /usr/local/mysql8/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql &;mysql_upgrade并重启:sudo mysql_upgrade -u root -p && sudo systemctl restart mysql。推荐方式:包管理器升级(避免依赖问题)
sudo apt upgrade php libapache2-mod-php php-mysql(如从7.4升级到8.1,需先升级到7.4最新小版本);sudo yum upgrade php php-mysql(需启用EPEL仓库或Remi仓库获取最新版本)。sudo systemctl restart apache2(Ubuntu/Debian)或sudo systemctl restart httpd(CentOS/RHEL)。手动编译升级(可选,适用于特定版本)
wget https://www.php.net/distributions/php-8.1.12.tar.gz;tar -xzvf php-8.1.12.tar.gz && cd php-8.1.12;mbstring、gd、mysqli):./configure --prefix=/usr/local/php8 --with-apxs2=/usr/bin/apxs --enable-mbstring --enable-gd --with-mysqli;make && sudo make install;/etc/php/7.4/apache2/php.ini复制到新版本目录/etc/php/8.1/apache2/php.ini),调整参数后重启Apache。兼容性检查(关键步骤)
php-compatibility composer包:composer require phpcompatibility/php-compatibility && phpcompat check --target=8.1 app/);mysql_query()→mysqli_query()、split()→explode())、参数变化(如foreach循环的list()用法);检查组件版本
apachectl -v(Apache)、mysql --version(MySQL)、php -v(PHP)。测试功能
info.php文件(<?php phpinfo(); ?>),访问该页面确认PHP模块加载正常;/var/log/apache2/error.log或/var/log/httpd/error_log),确认无报错。回滚预案(如有问题)
sudo systemctl restart apache2使用旧配置)。