在 CentOS 上配置 LibreOffice,通常分为 安装、运行环境配置、服务化(可选) 三个部分。下面按常见使用场景说明。
# CentOS 7
yum install -y libreoffice
# CentOS 8 / Stream
dnf install -y libreoffice
安装完成后检查:
libreoffice --version
⚠️ 系统仓库版本通常较旧,但稳定。
从官网下载 RPM 包:
wget https://download.documentfoundation.org/libreoffice/stable/24.2.5/rpm/x86_64/LibreOffice_24.2.5_Linux_x86-64_rpm.tar.gz
tar -xzf LibreOffice_24.2.5_Linux_x86-64_rpm.tar.gz
cd LibreOffice_24.2.5.x.x/RPMS
yum localinstall -y *.rpm
CentOS 服务器通常无 GUI,需要指定用户和 headless 模式:
libreoffice --headless --convert-to pdf example.docx
如报字体或权限问题,建议:
useradd louser
su - louser
libreoffice --headless --convert-to pdf test.docx
CentOS 默认缺少中文字体,转换会乱码。
yum install -y wqy-zenhei-fonts wqy-microhei-fonts
或复制 Windows 字体:
mkdir /usr/share/fonts/windows
cp *.ttf /usr/share/fonts/windows
fc-cache -fv
yum install -y java-11-openjdk
检查 Java:
libreoffice --headless --version
如需要指定 JVM:
vim ~/.config/libreoffice/4/user/config/javasettings_Linux_X86_64.xml
libreoffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;"
/etc/systemd/system/libreoffice.service
[Unit]
Description=LibreOffice Headless
[Service]
User=louser
ExecStart=/usr/bin/libreoffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;"
Restart=always
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start libreoffice
systemctl enable libreoffice
yum install -y xorg-x11-server-Xvfb
Xvfb :1 &
export DISPLAY=:1
不要用 root 直接运行 LibreOffice。
删除用户配置:
rm -rf ~/.config/libreoffice
如果你能说明:
我可以给你精确配置方案。