Ubuntu下ThinkPHP兼容性问题的排查与解决
一 环境一致性与基础检查
php -v。sudo apt-get install php php-fpm php-mysql php-mbstring php-xml php-curl php-gd php-zip。curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composer。composer create-project topthink tp6。sudo chown -R www-data:www-data /var/www/html/tp6 && sudo chmod -R 755 /var/www/html/tp6。二 Web 服务器与 URL 重写配置
sudo a2enmod rewrite && sudo systemctl restart apache2。<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
server {
listen 80;
root /var/www/html/tp6/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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 nginx -t && sudo systemctl reload nginx。try_files 即可。s 参数:if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
}
fastcgi_pass 路径与版本匹配。三 常见兼容性问题与修复
sudo apt-get install php-mysql php-gd php-mbstring。php -m | grep -E 'pdo_mysql|gd|mbstring'。php --ini 定位),必要时在 [extension] 处取消注释并重启 PHP-FPM。sudo apt-get install php-gd,重启 Apache/Nginx/PHP-FPM。ob_end_clean();。display_errors = On、error_reporting = E_ALL;Web 端还需确认 PHP-FPM 与 Apache/Nginx 加载了相同配置。四 版本与依赖管理建议