phpMyAdmin是一个基于web的MySQL/MariaDB管理工具,可以方便地通过浏览器管理数据库。下面是在LEMP(Linux, Nginx, MySQL/MariaDB, PHP)服务器上安装phpMyAdmin的详细步骤。
sudo apt update
sudo apt upgrade -y
sudo apt install phpmyadmin -y
在安装过程中会提示以下配置选项:
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
编辑Nginx配置文件(通常位于/etc/nginx/sites-available/your_domain
):
server {
listen 80;
server_name your_domain.com;
root /var/www/html;
index index.php index.html index.htm;
location /phpmyadmin {
alias /usr/share/phpmyadmin;
try_files $uri $uri/ =404;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
注意:根据你的PHP版本修改fastcgi_pass
行中的PHP版本号。
sudo nginx -t
sudo systemctl restart nginx
sudo mysql -u root -p
在MySQL提示符下:
CREATE USER 'pma_user'@'localhost' IDENTIFIED BY 'your_strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'pma_user'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
编辑配置文件:
sudo nano /etc/phpmyadmin/config.inc.php
找到并修改以下部分:
$cfg['Servers'][$i]['controluser'] = 'pma_user';
$cfg['Servers'][$i]['controlpass'] = 'your_strong_password';
在同一文件中,找到:
$cfg['blowfish_secret'] = ''; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
填入一个随机字符串,例如:
$cfg['blowfish_secret'] = 'xkljasdfoiuywerqwejrklj2342kjh';
限制访问IP:
location /phpmyadmin {
allow 192.168.1.100; # 你的IP
deny all;
# 其他配置...
}
启用HTTPS访问
在浏览器中访问:
http://your_server_ip/phpmyadmin
或如果你配置了域名:
http://your_domain.com/phpmyadmin
502 Bad Gateway错误:
sudo systemctl status php8.1-fpm
404 Not Found错误:
登录问题:
通过以上步骤,你应该已经成功在LEMP服务器上安装并配置了phpMyAdmin,现在可以通过web界面轻松管理MySQL/MariaDB数据库了。