在Debian系统上配置PHP数据库连接,通常涉及以下几个步骤:
安装PHP和数据库服务器:
sudo apt update
sudo apt install php php-cli php-fpm php-mysql
sudo apt install mysql-server
配置MySQL数据库:
sudo mysql_secure_installation
sudo mysql
CREATE DATABASE mydatabase;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
配置PHP连接数据库:
config.php的文件,内容如下:<?php
$servername = "localhost";
$username = "myuser";
$password = "mypassword";
$dbname = "mydatabase";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>
测试连接:
config.php文件放置在Web服务器的根目录下(例如/var/www/html)。配置Web服务器(如果使用的是Apache):
sudo apt install apache2
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl restart apache2
配置Web服务器(如果使用的是Nginx):
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
/etc/nginx/sites-available/myapp),内容如下:server {
    listen 80;
    server_name mydomain.com;
    root /var/www/html;
    index index.php index.html index.htm;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
通过以上步骤,你应该能够在Debian系统上成功配置PHP数据库连接。