Ubuntu/Debian:
sudo apt update
sudo apt install phpmyadmin
安装过程中会提示选择Web服务器(Apache或Nginx),按需选择并配置。
CentOS/RHEL:
sudo yum install epel-release
sudo yum install phpmyadmin
wget https://www.phpmyadmin.net/downloads/phpMyAdmin-latest-all-languages.tar.gz
tar -xvzf phpMyAdmin-latest-all-languages.tar.gz
mv phpMyAdmin-*-all-languages /usr/share/phpmyadmin
mkdir -p /usr/share/phpmyadmin/tmp
chown -R www-data:www-data /usr/share/phpmyadmin
chmod -R 755 /usr/share/phpmyadmin
创建配置文件 /etc/apache2/conf-available/phpmyadmin.conf
:
Alias /phpmyadmin /usr/share/phpmyadmin
<Directory /usr/share/phpmyadmin>
Options SymLinksIfOwnerMatch
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
启用配置:
sudo a2enconf phpmyadmin
sudo systemctl reload apache2
在站点配置中添加:
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
编辑Apache/Nginx配置,将/phpmyadmin
改为自定义路径如/mydbadmin
在config.inc.php
中添加:
$cfg['Servers'][$i]['AllowDeny']['order'] = 'deny,allow';
$cfg['Servers'][$i]['AllowDeny']['rules'] = array(
'deny from all',
'allow from 192.168.1.0/24'
);
确保phpMyAdmin仅通过HTTPS访问,在配置中添加:
$cfg['ForceSSL'] = true;
编辑php.ini
:
; 提高内存限制
memory_limit = 256M
; 优化执行时间
max_execution_time = 180
; 文件上传大小
upload_max_filesize = 64M
post_max_size = 64M
; OPcache 配置
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
// 使用持久连接
$link = mysqli_connect('p:localhost', 'user', 'password', 'database');
// 预处理语句提高效率
$stmt = $link->prepare("INSERT INTO users (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $name, $email);
$stmt->execute();
// 永远使用预处理语句防止SQL注入
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
$stmt->execute(['email' => $email]);
// 密码哈希
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
// 验证密码
if (password_verify($inputPassword, $storedHash)) {
// 密码正确
}
// CSRF防护
session_start();
if (empty($_SESSION['token'])) {
$_SESSION['token'] = bin2hex(random_bytes(32));
}
$token = $_SESSION['token'];
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';
config.inc.php
中配置正确php.ini
中的upload_max_filesize
和post_max_size
$cfg['UploadDir']
启用分块上传$cfg['MaxExactCount']
限制精确计数$cfg['QueryHistoryDB'] = true
将查询历史存储在数据库中$cfg['PersistentConnections'] = true
持久连接$cfg['Servers'][$i]['pmadb']
使用专用控制用户通过以上配置和技巧,您可以建立一个安全、高效的phpMyAdmin环境,并结合PHP最佳实践开发强大的数据库应用。