总体结论与版本匹配
php -v,不符合时安装对应版本(如PHP 7.4),必要时用 sudo update-alternatives --set php /usr/bin/php7.4 切换默认版本。环境与扩展配置
sudo apt install php7.4 php7.4-cli php7.4-fpm php7.4-mysql php7.4-curl php7.4-gd php7.4-mbstring php7.4-xml php7.4-zip php7.4-bcmathphp -m | grep -E 'curl|mbstring|mysql|xml|zip|gd|bcmath'extension=mbstring、extension=curl)。sudo systemctl restart php7.4-fpm(如使用 FPM);如用 Apache,执行 sudo systemctl restart apache2。Web服务器与URL重写
sudo a2enmod rewriteAllowOverride All<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>
sudo systemctl restart apache2server {
listen 80;
server_name your_domain_or_ip;
root /var/www/html/your_project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?s=$uri&$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht { deny all; }
}
sudo nginx -t && sudo systemctl restart nginx。项目部署与权限
curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composercomposer create-project topthink tp6config/database.php)填写正确的 DB_HOST/DB_NAME/DB_USER/DB_PASS/DB_PORT,字符集建议 utf8mb4。sudo chown -R www-data:www-data /path/to/your_projectsudo chmod -R 755 /path/to/your_projectruntime 可写:sudo chmod -R 775 /path/to/your_project/runtimecd tp6 && php think run,访问 http://localhost:8000。常见兼容性问题排查
sudo systemctl status php7.4-fpm;确认套接字路径(如 /var/run/php/php7.4-fpm.sock)与 Nginx 配置一致。try_files $uri $uri/ /index.php?s=$uri&$args;;Apache 需启用 mod_rewrite 且 AllowOverride All 生效。php think clear 清理 runtime 缓存,再重试访问。