如何在 Ubuntu 18.04 LTS 上安装带有 Nginx 的 phpMyAdmin
本教程适用于这些操作系统版本
- Ubuntu 11.04 (Natty Narwhal)
在此页
- 要求
- 开始
- 安装 LEMP 服务器
- 安装 phpMyAdmin
- 配置数据库
- 配置 Nginx
- 访问 phpMyAdmin
在本教程中,我们将学习如何在 Ubuntu 18.04 服务器上使用 Nginx 安装 phpMyAdmin。
要求
- 运行 Ubuntu 18.04 的服务器。
- 您服务器的静态 IP 地址 192.168.0.111。
- 具有 sudo 权限的非根用户。
入门
在开始之前,您需要使用最新版本更新您的系统。您可以通过运行以下命令来执行此操作:
sudo apt-get update -y
sudo apt-get upgrade -y
更新系统后,重新启动系统以应用更改。
安装 LEMP 服务器
首先,您需要在系统中安装 Nginx、MariaDB 服务器、PHP 和其他 PHP 模块。您可以通过运行以下命令来安装所有这些:
sudo apt-get install nginx php7.2 php7.2-common php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-cgi php7.2-common php-pear php-gettext mariadb-server -y
sudo systemctl start nginx
sudo systemctl start mariadb
sudo systemctl enable nginx
sudo systemctl enable mariadb
安装 phpMyAdmin
默认情况下,phpMyAdmin 在 Ubuntu 18.04 默认存储库中可用。您只需运行以下命令即可安装它:
sudo apt-get install phpmyadmin -y
在安装过程中,安装程序会要求您选择 Web 服务器,如下页所示。
不要选择任何选项,因为我们将使用 Nginx 作为 Web 服务器。因此,单击“确定”按钮。接下来,系统将要求您设置数据库,如下页所示:
单击“否”按钮。您应该会看到以下页面:
现在,为 phpMyAdmin 提供密码以在数据库中注册,然后单击“确定”按钮。系统将要求您再次确认密码。提供相同的密码,然后单击“确定”按钮以完成安装。
配置数据库
首先,使用以下命令登录到 MariaDB shell:
sudo mysql
接下来,创建一个新的管理员用户帐户并使用以下命令授予适当的权限:
MariaDB [(none)]> CREATE USER 'phpadmin'@'localhost' IDENTIFIED BY 'mypassword';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'phpadmin'@'localhost' WITH GRANT OPTION;
用您选择的安全密码替换“mypassword”一词。现在,使用以下命令退出 MariaDB shell:
MariaDB [(none)]>EXIT;
配置 Nginx
接下来,您需要创建一个 Nginx 虚拟主机文件。您可以使用以下命令执行此操作:
sudo nano /etc/nginx/sites-available/phpmyadmin.conf
添加以下行:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name 192.168.0.111;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存并关闭文件。然后,使用以下命令检查 Nginx 是否有任何语法错误:
sudo nginx -t
如果一切正常,您应该会看到以下输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
接下来,使用以下命令启用 phpmyadmin 虚拟主机文件并删除默认虚拟主机文件:
sudo rm -rf /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/phpmyadmin.conf /etc/nginx/sites-enabled/
接下来,使用以下命令重新启动 Nginx 服务以应用更改:
sudo systemctl restart nginx
接下来,使用以下命令检查 Nginx 的状态:
sudo systemctl status nginx
您应该看到以下输出:
? nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-01-15 11:10:29 UTC; 2min 0s ago
Docs: man:nginx(8)
Process: 14871 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 14885 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 14874 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 14886 (nginx)
Tasks: 2 (limit: 1113)
CGroup: /system.slice/nginx.service
??14886 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
??14887 nginx: worker process
Jul 15 11:10:29 ubuntu1804 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jul 15 11:10:29 ubuntu1804 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 15 11:10:29 ubuntu1804 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 15 11:10:29 ubuntu1804 systemd[1]: Started A high performance web server and a reverse proxy server.
访问 phpMyAdmin
phpMyAdmin 现已安装和配置,是时候访问 phpMyAdmin Web 界面了。
打开 Web 浏览器并输入 URL http://192.168.0.111/phpmyadmin/。您将被重定向到以下页面:
现在,提供您的管理员用户名和密码。然后,单击“开始”按钮。您应该在以下页面中看到 phpMyAdmin 默认仪表板:
恭喜!您已成功安装并配置 phpMyAdmin 到您的 Ubuntu 18.04 LTS 服务器。您现在可以通过基于 web 的 phpMyAdmin 界面管理您的 MariaDB 数据库。如果您有任何问题,请随时问我。