You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

源码安装ghostscript后,nginx调用graphicsmagick执行PDF转图失败的原因及解决

问题描述

之前通过apt install安装的Ghostscript与GraphicsMagick能正常工作,卸载后改用源码编译安装最新版本后,nginx运行依赖Ghostscript的GraphicsMagick命令时失败,但以root身份执行该命令完全正常。

执行的命令:

gm convert -density 50 input.pdf[0] -background white -resize 140x140 -strip -quality 40 thumb.jpg

报错信息:

execvp failed, errno = 2 (No such file or directory) gm convert: "gs" "-q" "-dBATCH" "-dSAFER" "-dMaxBitmap=50000000" "-dNOPAUSE" "-sDEVICE=pnmraw" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4" "-r50x50" "-dFirstPage=1" "-dLastPage=1" "-sOutputFile=/tmp/gmhjqkzP" "--" "/tmp/gmZaShhP" "-c" "quit". gm convert: Request did not return an image.

Ghostscript源码安装步骤:

# cd /var/bin && wget https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs10021/ghostscript-10.02.1.tar.gz && tar -xvf ghostscript-10.02.1.tar.gz
# cd ghostscript-10.02.1 && ./configure && make -j $(nproc) && make install

补充信息:

  • 执行type gs显示路径为/usr/local/bin/gs
  • /etc/profile已配置PATH包含/usr/local/bin,且确认nginx运行环境的PATH也包含该目录
  • gs文件权限为所有人可执行(-rwxr-xr-x)
问题原因
  • 动态库加载失败:源码编译的Ghostscript依赖的动态库位于/usr/local/lib,而nginx运行用户(如www-data)的动态库搜索路径未包含该目录,导致执行gs时无法加载依赖库,系统误报为"找不到文件"(errno=2)。root用户的环境通常默认包含该路径,所以能正常执行。
  • GraphicsMagick路径硬编码:若GraphicsMagick也是源码安装,可能编译时检测到的是旧版Ghostscript的路径,运行时仍调用旧路径的gs导致失败。
解决方法

方法一:修复动态库搜索路径

  1. 创建Ghostscript的动态库配置文件:
    echo "/usr/local/lib" > /etc/ld.so.conf.d/ghostscript.conf
    
  2. 更新系统动态库缓存:
    ldconfig
    
  3. 重启nginx服务:
    systemctl restart nginx
    

方法二:重新编译GraphicsMagick(若为源码安装)

如果GraphicsMagick是源码编译的,重新编译并指定Ghostscript的路径:

  1. 进入GraphicsMagick的源码目录:
    cd /path/to/graphicsmagick-source
    
  2. 重新配置并编译:
    ./configure --with-gs=/usr/local/bin/gs
    make -j $(nproc) && make install
    
  3. 重启nginx服务。

方法三:确保nginx服务的环境变量正确(可选)

如果是systemd管理的nginx,检查并修改服务文件的PATH配置:

  1. 编辑nginx服务文件:
    vi /etc/systemd/system/nginx.service
    
  2. 在[Service]段添加或修改环境变量:
    Environment="PATH=/usr/local/bin:/usr/bin:/bin"
    
  3. 重载systemd配置并重启nginx:
    systemctl daemon-reload
    systemctl restart nginx
    

内容的提问来源于stack exchange,提问作者clarkk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:23:12