在 Ubuntu 上搭建 LAMP(Linux + Apache + MySQL/MariaDB + PHP) 环境后,调试通常分为 服务状态、日志、配置、PHP、数据库、网络 几个方面。下面给你一套系统化的调试方法。
sudo systemctl status apache2
启动 / 重启 / 停止:
sudo systemctl start apache2
sudo systemctl restart apache2
sudo systemctl stop apache2
测试是否监听:
sudo netstat -tlnp | grep :80
# 或
curl http://localhost
sudo systemctl status mysql
# 或
sudo systemctl status mariadb
登录测试:
mysql -u root -p
查看版本:
php -v
Apache 是否加载 PHP:
apache2ctl -M | grep php
sudo tail -f /var/log/apache2/error.log
访问日志:
sudo tail -f /var/log/apache2/access.log
编辑 php.ini:
sudo nano /etc/php/8.x/apache2/php.ini
确保:
display_errors = On
error_reporting = E_ALL
log_errors = On
PHP 日志位置:
error_log = /var/log/php_errors.log
创建测试文件:
sudo nano /var/www/html/info.php
内容:
<?php
phpinfo();
浏览器访问:
http://localhost/info.php
如果打不开:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
ls -ld /var/www/html
查看启用站点:
ls /etc/apache2/sites-enabled/
测试配置:
sudo apache2ctl configtest
重启生效:
sudo systemctl restart apache2
常见原因:
查看 MySQL 绑定:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 127.0.0.1
授权示例:
GRANT ALL ON dbname.* TO 'user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
sudo ufw status
允许 HTTP:
sudo ufw allow 80
sudo ufw allow 443
| 问题 | 可能原因 |
|---|---|
| 403 Forbidden | 目录权限 / AllowOverride |
| 404 Not Found | DocumentRoot 错误 |
| 500 Internal | PHP 语法 / .htaccess |
| 页面空白 | PHP 报错被隐藏 |
| 无法连接 DB | 用户权限 / 防火墙 |
sudo systemctl status apache2 mysql
sudo apache2ctl configtest
tail -f /var/log/apache2/error.log
如果你愿意,可以告诉我:
我可以直接帮你定位问题。