Debian PHP开发环境搭建指南
一 准备与版本选择
sudo apt updateapt-cache policy phpsudo apt install php8.2 php8.2-cli php8.2-fpmsudo apt install -y software-properties-common ca-certificates lsb-release apt-transport-https wgetwget -qO - https://packages.sury.org/php/apt.gpg | sudo gpg --dearmor | sudo tee /usr/share/keyrings/php-archive-keyring.gpg >/dev/nullecho "deb [signed-by=/usr/share/keyrings/php-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list >/dev/nullsudo apt update && sudo apt install php8.2 php8.2-cli php8.2-fpmsudo apt install php8.2-{mysql,zip,gd,mbstring,curl,xml,bcmath,opcache,json}二 选择 Web 服务器
sudo apt install nginxsudo systemctl start nginx && sudo systemctl enable nginx/etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
sudo nginx -t && sudo systemctl reload nginxsudo apt install apache2 libapache2-mod-php8.2sudo a2enmod php8.2 && sudo systemctl restart apache2三 数据库与基础验证
sudo apt install mariadb-server && sudo systemctl enable --now mariadbsudo apt install mysql-server && sudo systemctl enable --now mysqlsudo mysql_secure_installationecho "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.phphttp://服务器IP/info.php,应看到 PHP 信息页sudo ufw allow 'Nginx Full'sudo ufw allow 'Apache Full'php8.2-fpm 运行且 Nginx 中 fastcgi_pass 指向正确路径(如:/run/php/php8.2-fpm.sock)/var/www/html 可被 www-data 读取(开发环境可临时 chmod -R 755 /var/www/html 并排查所有者)。四 开发与调试配置
/etc/php/8.2/cli/php.ini 与 /etc/php/8.2/fpm/php.inidisplay_errors = Onerror_reporting = E_ALLmemory_limit = 256M(或更高)upload_max_filesize = 100Mpost_max_size = 100Msudo systemctl restart php8.2-fpm(FPM)或 sudo systemctl restart apache2(Apache)sudo apt install php8.2-xdebugzend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
sudo systemctl restart php8.2-fpm