系统缺少中文字体的情况下,代码使用中文字体便会乱码。检查系统中是否存在字体,查看 /usr/share/fonts 。分两种情况:
一、当不存在该目录,运行以下命令安装 fontconfig
yum -y install fontconfig
完了之后有目录了,而且里面自带了 DejaVuSans 字体。看名字很容易误以为是宋体,其实不是,并没有中文字体在里面。
二、有 fonts 目录,但里面没有中文字体。首先在电脑中找到字体文件,windows 在C:\windows\fonts 下面,macOS 打开“字体册”应用,左边展开找到“宋体”,然后在有复制字体路径出去。将后缀为 ttf 或者 ttc 的文件传到服务器上。
然后按照步骤修改配置.
-
创建目录 /usr/share/fonts/zh-cn , 将字体文件移到 zh-cn 下面,修改权限
chmod -R 755 zh-cn
-
修改 /etc/fonts/fonts.conf
在下面添加一行
<!-- Font directory list -->
<dir>/usr/share/fonts</dir>
<dir>/usr/share/fonts/zh</dir> <-- 添加的行
<dir>/usr/share/X11/fonts/Type1</dir> <dir>/usr/share/X11/fonts/TTF</dir> <dir>/usr/local/share/fonts</dir>
<dir prefix="xdg">fonts</dir>
<!-- the following element will be removed in the future -->
<dir>~/.fonts</dir>
3. 清除缓存 fc-cache
操作完成。