如何在 Debian 11 上使用 Nginx 安装 phpMyAdmin

在本教程中,我们将向您展示如何在 Debian 11 上使用 Nginx 安装 phpMyAdmin。对于那些不知道的人,phpMyAdmin 是一个免费的、开源的、基于 Web 的应用程序,用于管理 MySQL 数据库、用户帐户和特权、执行 SQL 语句、以各种数据格式导入和导出数据,以及从 Web 界面执行更多操作。

本文假设您至少具有 Linux 的基本知识,知道如何使用 shell,最重要的是,您将站点托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户中运行,否则您可能需要添加 ‘sudo‘ 到获得 root 权限的命令。 我将向您展示在 Debian 11 (Bullseye) 上逐步安装 phpMyAdmin。

在 Debian 11 Bullseye 上使用 Nginx 安装 phpMyAdmin

步骤 1. 在我们安装任何软件之前,请务必通过运行以下命令来确保您的系统是最新的 apt 终端中的命令:

sudo apt update sudo apt upgrade

步骤 2. 安装 LEMP 堆栈。

如果您的服务器上尚未安装 LEMP(Linux + Nginx+ MySQL/MariDB+ PHP),您可以在此处按照我们的指南进行操作。

步骤 3. 配置 MySQL。

现在我们为 phpMyAdmin 创建一个新的超级用户帐户:

sudo mysql -u root -p

这将提示您输入密码,因此请输入您的 MariaDB 根密码并点击 Enter. 登录到数据库服务器后,您需要为 phpMyAdmin 安装创建一个数据库:

MariaDB> CREATE DATABASE app_db; MariaDB> CREATE USER 'app_user'@'localhost' IDENTIFIED BY 'your-strong-password'; MariaDB> GRANT ALL PRIVILEGES ON app_db.* TO 'app_user'@'localhost' WITH GRANT OPTION; MariaDB> FLUSH PRIVILEGES; MariaDB> EXIT;

第 4 步。 在 Debian 11 上安装 phpMyAdmin。

默认情况下,Debian 11 Bullseye 存储库中不提供 phpMyAdmin,因此您需要从官方页面手动下载 phpMyAdmin:

wget https://files.phpmyadmin.net/phpMyAdmin/5.1.1/phpMyAdmin-5.1.1-all-languages.tar.gz

接下来,将 phpMyAdmin 存档解压缩到您的 Web 服务器根目录:

tar xvf phpMyAdmin-5.1.1-all-languages.tar.gz sudo mv phpMyAdmin-5.1.1-all-languages /usr/share/phpMyAdmin

步骤 5. 配置 phpMyAdmin。

现在我们复制示例 phpMyAdmin 配置文件并重命名如下:

sudo cp -pr /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php

接下来,编辑配置文件:

sudo nano /usr/share/phpMyAdmin/config.inc.php

生成河豚秘密 并更新配置文件中的秘密:

$cfg['blowfish_secret'] = 'eDjtEzAk8N3Rk}AFY.vBW}UtYL7VPbGo'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

另外,取消注释 phpMyAdmin 存储设置:

/** * phpMyAdmin configuration storage settings. */ /* User used to manipulate with storage */ $cfg['Servers'][$i]['controlhost'] = 'localhost'; // $cfg['Servers'][$i]['controlport'] = ''; $cfg['Servers'][$i]['controluser'] = 'pma'; $cfg['Servers'][$i]['controlpass'] = 'pmapass'; /* Storage database and tables */ $cfg['Servers'][$i]['pmadb'] = 'phpmyadmin'; $cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark'; $cfg['Servers'][$i]['relation'] = 'pma__relation'; $cfg['Servers'][$i]['table_info'] = 'pma__table_info'; $cfg['Servers'][$i]['table_coords'] = 'pma__table_coords'; $cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages'; $cfg['Servers'][$i]['column_info'] = 'pma__column_info'; $cfg['Servers'][$i]['history'] = 'pma__history'; $cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs'; $cfg['Servers'][$i]['tracking'] = 'pma__tracking'; $cfg['Servers'][$i]['userconfig'] = 'pma__userconfig'; $cfg['Servers'][$i]['recent'] = 'pma__recent'; $cfg['Servers'][$i]['favorite'] = 'pma__favorite'; $cfg['Servers'][$i]['users'] = 'pma__users'; $cfg['Servers'][$i]['usergroups'] = 'pma__usergroups'; $cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding'; $cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches'; $cfg['Servers'][$i]['central_columns'] = 'pma__central_columns'; $cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings'; $cfg['Servers'][$i]['_templates'] = 'pma___templates';

步骤 6. 为 phpMyAdmin 配置数据库和用户。

现在我们通过运行以下命令来创建配置存储数据库和表:

sudo mysql < /usr/share/phpMyAdmin/sql/create_tables.sql -u root -p

接下来,使用以下命令连接到 MariaDB shell:

sudo mysql -u root -p

连接后,将所有必要的权限授予 phpMyAdmin 数据库:

CREATE USER 'pma'@'localhost' IDENTIFIED BY 'pmapass'; GRANT ALL PRIVILEGES ON phpmyadmin.* TO 'pma'@'localhost' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;

步骤 7. 为 phpMyAdmin 配置 Nginx。

现在我们为 phpMyAdmin 创建一个 Nginx 虚拟主机配置文件:

sudo nano /etc/nginx/conf.d/phpMyAdmin.conf

添加以下文件:

server {    listen 80;    server_name pma.your-domain.com;    root /usr/share/phpMyAdmin;     location / {       index index.php;    }  ## Images and static content is treated different    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {       access_log off;       expires 30d;    }     location ~ /.ht {       deny all;    }     location ~ /(libraries|setup/frames|setup/libs) {       deny all;       return 404;    }     location ~ .php$ {       include /etc/nginx/fastcgi_params;       fastcgi_pass unix:/run/php/php7.4-fpm.sock;       fastcgi_index index.php;       fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin$fastcgi_script_name;    } }

Save 和 close,然后创建一个 tmp phpMyAdmin 的目录,然后更改权限:

sudo mkdir /usr/share/phpMyAdmin/tmp sudo chmod 777 /usr/share/phpMyAdmin/tmp

接下来,为 phpMyAdmin 目录设置适当的所有权:

sudo chown -R www-data:www-data /usr/share/phpMyAdmin

最后,重启 Nginx 和 PHP-fpm 服务:

sudo systemctl restart nginx sudo systemctl restart php7.4-fpm

步骤 8. 访问 phpMyAdmin Web 界面。

安装成功后,打开浏览器浏览 https://your-domain.com/ 并且您的 phpMyAdmin 会询问您安装 MySQL 的用户名和密码,您可以使用 root 作为用户和 root MySQL 密码。

在 Debian 11 Bullseye 上使用 Nginx 安装 phpMyAdmin

恭喜! 您已成功安装 phpMyAdmin。 感谢您使用本教程在 Debian 11 Bullseye 上使用 Nginx 安装最新版本的 phpMyAdmin。 如需更多帮助或有用信息,我们建议您查看 phpMyAdmin 官方网站.