在CentOS上安装phpMyAdmin是一个相对简单的过程,但需要确保系统已经安装了Web服务器(如Apache或Nginx)、数据库(如MySQL或MariaDB)以及PHP环境,以下是详细的安装步骤:

一、前提条件与准备工作
1. 更新系统

sudo yum update y
2. 安装EPEL库
对于CentOS 7及更高版本,需要安装EPEL库以获取最新的软件包:
sudo yum install epelrelease y
3. 安装LAMP或LEMP环境
可以选择安装LAMP(Apache, MySQL, PHP)或LEMP(Nginx, MySQL, PHP),以下以LAMP为例:
安装Apache sudo yum install httpd y 启动并使Apache在开机时自动启动 sudo systemctl start httpd sudo systemctl enable httpd 安装MySQL sudo yum install mariadbserver mariadb y 启动并使MySQL在开机时自动启动 sudo systemctl start mariadb sudo systemctl enable mariadb 安装PHP sudo yum install php y 重启Apache以加载PHP模块 sudo systemctl restart httpd
二、安装phpMyAdmin
1. 安装phpMyAdmin
sudo yum install phpmyadmin y
2. 配置phpMyAdmin

默认情况下,phpMyAdmin只允许从localhost访问,为了能够远程访问,需要修改配置文件。
对于Apache用户:
编辑配置文件/etc/httpd/conf.d/phpMyAdmin.conf,注释掉带有Require ip XXXX字样的代码行,并用Require all granted替换:
sudo vi /etc/httpd/conf.d/phpMyAdmin.conf
修改后的配置如下所示:
<Directory "/usr/share/phpMyAdmin">
AddDefaultCharset UTF8
<IfModule mod_authz_core.c>
# Apache 2.4
<RequireAny>
#Require ip 127.0.0.1
#Require ip ::1
Require all granted
</RequireAny>
</IfModule>
<IfModule !mod_authz_core.c>
# Apache 2.2
Order Deny,Allow
Deny from All
# Allow from 127.0.0.1
# Allow from ::1
Allow from All
</IfModule>
</Directory>保存文件并退出编辑器,然后重启Apache服务:
sudo systemctl restart httpd
对于Nginx用户:
编辑Nginx配置文件/etc/nginx/conf.d/phpMyAdmin.conf,确保配置如下:
server {
listen 80;
server_name your_domain_or_IP;
location ~ \.php$ {
root /usr/share/phpMyAdmin;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
root /usr/share/phpMyAdmin;
index index.php;
}
}保存文件并退出编辑器,然后重启Nginx服务:
sudo systemctl restart nginx
注意:如果使用的是防火墙,请确保开放相应的端口(如80或443)。
三、访问phpMyAdmin
在浏览器中输入服务器的IP地址或域名,加上/phpMyAdmin,即可访问phpMyAdmin登录界面,http://your_domain_or_IP/phpMyAdmin
使用MySQL的用户名和密码登录即可。
四、安全加固(可选)
为了增强安全性,可以设置IP白名单,只允许特定的IP地址访问phpMyAdmin,编辑/etc/nginx/conf.d/phpMyAdmin.conf或/etc/httpd/conf.d/phpMyAdmin.conf,添加如下配置:
<Directory "/usr/share/phpMyAdmin">
...
<IfModule mod_authz_core.c>
Require ip your_allowed_ip_address
</IfModule>
<IfModule !mod_authz_core.c>
Order Deny,Allow
Deny from All
Allow from your_allowed_ip_address
</IfModule>
</Directory>将your_allowed_ip_address替换为你的公网IP地址,保存文件并重启Web服务器。
五、常见问题解答(FAQs)
Q1: 如何更改phpMyAdmin的默认端口号?
A1: 要更改phpMyAdmin的默认端口号,可以在Nginx或Apache的配置文件中修改listen指令后的端口号,在Nginx中,可以将listen 80;改为listen 8080;,然后重启Nginx服务,确保防火墙允许新的端口号通过。
Q2: 如果忘记MySQL的root密码,如何重置?
A2: 如果忘记了MySQL的root密码,可以通过以下步骤重置:
1、停止MySQL服务:sudo systemctl stop mariadb
2、以安全模式启动MySQL:sudo mysqld_safe skipgranttables &
3、登录MySQL:mysql u root
4、在MySQL命令行中,执行以下SQL语句重置密码:
FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
将new_password替换为你的新密码。
5、退出MySQL并重新启动MySQL服务:sudo systemctl restart mariadb
6、使用新密码登录MySQL。
