如何在 Ubuntu 22.04 LTS 上使用 Nginx 安装 phpMyAdmin

phpMyAdmin 是一个免费的基于 Web 的管理工具,用于管理 MySQL 和 MariaDB 数据库服务器。使用 phpMyAdmin,您可以执行多项任务、管理用户帐户和权限、导入和导出数据、执行 SQL 语句等等。phpMyAdmin

在 Ubuntu 22.04 LTS Jammy Jellyfish 上使用 Nginx 安装 phpMyAdmin

apt步骤 1. 首先,通过在终端中运行以下命令,确保所有系统包都是最新的。

sudo apt update
sudo apt upgrade
sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common

步骤 2. 在 Ubuntu 上安装 LEMP 堆栈。

在开始本教程之前,必须在您的服务器上安装 LEMP 服务器。如果您没有安装 LEMP Stack,您可以在此处按照我们的指南进行操作。

步骤 3. 在 Ubuntu 22.04 上安装 phpMyAdmin。

默认情况下,phpMyAdmin 在 Ubuntu 22.04 基础存储库中不可用。现在运行以下命令将最新版本的 phpMyAdmin 安装到您的 Ubuntu 系统:

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

tar接下来,使用以下命令提取 phpMyAdmin :

tar -zxvf phpMyAdmin-5.2.0-all-languages.tar.gz
sudo mv phpMyAdmin-5.2.0-all-languages /usr/share/phpMyAdmin

步骤 4. 配置 phpMyAdmin。

现在我们重命名 phpMyAdmin 的示例配置文件来配置 phpMyAdmin:

sudo mv /usr/share/phpMyAdmin/config.sample.inc.php /usr/share/phpMyAdmin/config.inc.php

然后,编辑配置文件:

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

接下来,生成一个河豚密码并使用配置文件中生成的密码更新以下行:

$cfg['blowfish_secret'] = '0};Ow}n{nM80t[C/qhA0bpnYhS;Ey4L='; /* 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'] = 'ngadimin';
$cfg['Servers'][$i]['controlpass'] = 'your-strong-passwd';

步骤 5. 配置数据库 phpMyAdmin。

phpMyAdmin 需要数据库表。所以,导入自带的 phpMyAdmin 归档包来为 phpMyAdmin 创建表:create_tables.sql

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

接下来,我们需要使用以下命令登录 MariaDB 控制台:

mysql -u root -p

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

MariaDB [(none)]> CREATE USER 'ngadimin'@'localhost' IDENTIFIED BY 'your-strong-passwd';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON phpmyadmin.* TO 'ngadimin'@'localhost' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> EXIT;

步骤 6. 为 Nginx 支持配置 phpMyAdmin。

运行以下命令以创建可在现有服务器块上使用的 phpMyAdmin 片段:

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

添加以下文件:

server {
   listen 80;
   server_name your-domain;
   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/php8.1-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

保存并关闭文件,然后重新启动 Nginx Web 服务器以进行更改:

nginx -tsudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

然后,为 phpMyAdmin 创建一个tmp目录,然后更改权限:

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

我们将需要更改一些文件夹权限:

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

步骤 7. 配置防火墙。

Ubuntu 22.04ufw默认运行防火墙。通过端口80HTTP 和443HTTPS 启用连接:

sudo ufw allow 'Nginx FULL'
sudo ufw enable
sudo ufw status

步骤 8. 访问 phpMyAdmin Web 界面。

成功安装后,打开您的网络浏览器并输入 URL 。您将被重定向到 phpMyAdmin 登录页面:http://your-domain.com

phpMyAdmin-login

使用数据库用户登录,您应该在以下页面上看到 phpMyAdmin 默认仪表板:

mysql-phpmyadmin-server-version

步骤 9. 使用 Let’s Encrypt SSL 免费证书保护 phpMyAdmin。

首先,使用以下命令在 Ubuntu 22.04 上安装 Certbot:

sudo apt install python3-certbot-nginx

然后,使用以下命令为 Nginx 设置 Certbot:

sudo certbot --nginx --agree-tos --redirect --hsts --staple-ocsp --email you@your-domain.com -d www.your-domain.com

Let’s Encrypt 证书的有效期为 90 天,强烈建议在证书到期前更新证书。要测试此更新过程是否正常工作,您可以运行:

sudo certbot renew --dry-run

感谢您使用本教程在 Ubuntu 22.04 LTS Jammy Jellyfish 系统上安装 phpMyAdmin 数据库管理。如需更多帮助或有用信息,我们建议您查看官方 phpMyAdmin 网站

未经允许不得转载:统信UOS之家 » 如何在 Ubuntu 22.04 LTS 上使用 Nginx 安装 phpMyAdmin

相关文章