下面是详细讲解“Linux CentOS 7下安装 phpMyAdmin 的教程”的完整攻略:
环境准备
在开始安装 phpMyAdmin 之前,需要先确认以下环境准备工作:
- LAMP(Linux+Apache+MySQL+PHP)环境已经搭建完成;
- PHP 的 mysqli 模块已经安装;
- 确认有可用的网络连接;
下载和安装 phpMyAdmin
- 进入 phpMyAdmin 的官网,下载最新版本的 phpMyAdmin。
bash
$ cd /usr/share/nginx/application
$ wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.zip
- 解压下载的文件。
bash
$ unzip phpMyAdmin-5.1.1-all-languages.zip
- 将文件夹重新命名为 phpmyadmin,并将其移动到 Web 根目录下(这里以 Nginx 为例)。
bash
$ mv phpMyAdmin-5.1.1-all-languages phpmyadmin
$ mv phpmyadmin /usr/share/nginx/html
- 为 phpMyAdmin 目录设置文件夹权限。
bash
$ chmod -R 755 /usr/share/nginx/html/phpmyadmin/
$ chown -R www-data:www-data /usr/share/nginx/html/phpmyadmin/
- 在 Nginx 中配置 phpMyAdmin 的虚拟主机。
bash
$ vim /etc/nginx/conf.d/phpmyadmin.conf
在文件中写入以下内容:
“`
server {
listen 80;
server_name
root /usr/share/nginx/html/phpmyadmin;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # PHP 版本根据实际情况来定
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
“`
- 重载 Nginx 配置文件以激活更改。
bash
$ systemctl reload nginx
配置 phpMyAdmin
- 复制 phpMyAdmin 的默认配置文件,作为新的配置文件。
bash
$ cd /usr/share/nginx/html/phpmyadmin
$ cp config.sample.inc.php config.inc.php
- 使用编辑器打开配置文件(config.inc.php),并进行以下修改:
“`php
<?php
$cfg[‘blowfish_secret’] = ‘‘;
/*
* Servers configuration
/
$i = 0;
// MySQL 配置
$i++;
$cfg[‘Servers’][$i][‘auth_type’] = ‘cookie’;
$cfg[‘Servers’][$i][‘host’] = ‘localhost’; // 数据库主机地址
$cfg[‘Servers’][$i][‘user’] = ‘root’; // 数据库用户名
$cfg[‘Servers’][$i][‘password’] = ‘your_password’; // 数据库密码
/*
* Other settings
/
// Use UTF-8 charset
$cfg[‘DefaultCharset’] = ‘utf-8’;
// Directories for saving/loading files from server
$cfg[‘UploadDir’] = ”;
$cfg[‘SaveDir’] = ”;
“`
其中,$cfg['blowfish_secret']
是用于加密 cookie 的字符串,请随机生成一个字符串填入。$cfg['Servers'][$i]
是用于配置连接到 MySQL 数据库的参数,替换其中的主机地址、用户名和密码为实际情况。
- 保存并关闭文件。
测试 phpMyAdmin
访问你配置的 phpMyAdmin 虚拟主机地址http://<your domain name>/phpmyadmin
,输入 MySQL 数据库的用户名和密码,即可成功登录 phpMyAdmin。
示例如下:
-
确认 Nginx 已经启动,访问 http://localhost,能够看到 Nginx 欢迎页面。
-
下载 phpMyAdmin 安装包并解压缩。
bash
$ cd /usr/share/nginx/application
$ wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.zip
$ unzip phpMyAdmin-5.1.1-all-languages.zip
$ mv phpMyAdmin-5.1.1-all-languages phpmyadmin
$ mv phpmyadmin /usr/share/nginx/html
- 为 phpMyAdmin 目录设置文件夹权限。
bash
$ chmod -R 755 /usr/share/nginx/html/phpmyadmin/
$ chown -R www-data:www-data /usr/share/nginx/html/phpmyadmin/
- 在 Nginx 中配置 phpMyAdmin 的虚拟主机。
bash
$ vim /etc/nginx/conf.d/phpmyadmin.conf
写入以下内容:
“`
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html/phpmyadmin;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
“`
- 重载 Nginx 配置文件以激活更改。
bash
$ systemctl reload nginx
- 复制 phpMyAdmin 的默认配置文件,作为新的配置文件。
bash
$ cd /usr/share/nginx/html/phpmyadmin
$ cp config.sample.inc.php config.inc.php
- 使用编辑器打开配置文件(config.inc.php),并进行以下修改:
“`php
<?php
$cfg[‘blowfish_secret’] = ‘a_random_string_for_security’;
/*
* Servers configuration
/
$i = 0;
// MySQL 配置
$i++;
$cfg[‘Servers’][$i][‘auth_type’] = ‘cookie’;
$cfg[‘Servers’][$i][‘host’] = ‘localhost’; // 数据库主机地址
$cfg[‘Servers’][$i][‘user’] = ‘root’; // 数据库用户名
$cfg[‘Servers’][$i][‘password’] = ‘your_password’; // 数据库密码
/*
* Other settings
/
// Use UTF-8 charset
$cfg[‘DefaultCharset’] = ‘utf-8’;
// Directories for saving/loading files from server
$cfg[‘UploadDir’] = ”;
$cfg[‘SaveDir’] = ”;
“`
其中,$cfg['blowfish_secret']
是用于加密 cookie 的字符串,请随机生成一个字符串填入。$cfg['Servers'][$i]
是用于配置连接到 MySQL 数据库的参数,替换其中的主机地址、用户名和密码为实际情况。
- 访问 http://localhost/phpmyadmin,使用 MySQL 数据库的用户名和密码登录。
现在你已经成功安装和配置了 phpMyAdmin。
希望以上步骤能够帮助你完成在 Linux CentOS 7 下安装 phpMyAdmin,并且能够顺利地访问和使用 phpMyAdmin 来管理和维护 MySQL 数据库。