在 Ubuntu 上 ThinkPHP 访问不了,通常不是代码问题,而是环境或配置问题。下面按最常见原因 + 排查顺序给你说明,基本能解决 90% 的情况。
# Apache
systemctl status apache2
# Nginx
systemctl status nginx
如果没启动:
sudo systemctl start apache2
# 或
sudo systemctl start nginx
ThinkPHP 必须指向 public 目录
✅ 正确:
/var/www/html/project/public
❌ 错误:
/var/www/html/project
ThinkPHP 路由依赖 mod_rewrite
sudo a2enmod rewrite
sudo systemctl restart apache2
编辑 Apache 配置:
sudo nano /etc/apache2/sites-enabled/000-default.conf
确保有:
<Directory /var/www/html/project/public>
AllowOverride All
</Directory>
然后:
sudo systemctl restart apache2
Nginx 必须加:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
改完:
sudo nginx -t
sudo systemctl restart nginx
sudo chown -R www-data:www-data /var/www/html/project
sudo chmod -R 755 /var/www/html/project
php -v
要求:
开启调试:
// .env
APP_DEBUG = true
或:
define('APP_DEBUG', true);
| 检查项 | 命令 |
|---|---|
| Web 服务 | systemctl status nginx/apache2 |
| PHP | php -v |
| 根目录 | 是否 public |
| 伪静态 | rewrite / try_files |
| 权限 | www-data |
| 调试 | APP_DEBUG=true |
为了精准帮你,请回复:
我可以直接帮你写配置文件。