在 Ubuntu 下使用 Apache 出现乱码,通常不是 Apache 本身“坏了”,而是字符编码不一致导致的。下面按常见原因和解决办法说明。
浏览器可能默认用 ISO-8859-1(Latin-1),导致中文乱码。
✅ 解决:在 HTML 中明确声明
<meta charset="UTF-8">
或旧写法:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Ubuntu 上 Apache 默认不一定强制 UTF-8。
✅ 修改 Apache 配置:
sudo nano /etc/apache2/conf-available/charset.conf
确保有:
AddDefaultCharset UTF-8
启用并重启:
sudo a2enconf charset
sudo systemctl restart apache2
例如用 Windows 记事本保存为 GBK。
✅ 查看文件编码:
file -i index.html
✅ 转换为 UTF-8:
iconv -f GBK -t UTF-8 index.html > index.utf8.html
mv index.utf8.html index.html
推荐编辑器:
:set fileencoding=utf-8header("Content-Type:text/html; charset=UTF-8");
同时确保:
常见症状:
???✅ 检查:
SHOW VARIABLES LIKE 'character_set%';
✅ 推荐统一为:
utf8mb4
如果你说的是 终端里 Apache 日志乱码:
locale
如果不是 UTF-8:
sudo apt install locales
sudo dpkg-reconfigure locales
选择 en_US.UTF-8 或 zh_CN.UTF-8
<meta charset="UTF-8">AddDefaultCharset UTF-8如果你愿意,可以告诉我:
?? 还是 æ–‡å— 这种?我可以直接帮你定位到具体配置。