之前我通过
ghostscript
安装了 graphicsmagick
和 apt install ...
,但为了支持最新版本,我决定卸载并从源代码构建和安装
在一切正常之前,但从源安装后
graphicsmagick
需要ghostscript
的命令失败(当从nginx
运行时)
命令
gm convert -density 50 input.pdf[0] -background white -resize 140x140 -strip -quality 40 thumb.jpg
上面的命令在运行时工作正常
root
,但是当从网络服务器运行时 nginx
会返回错误
execvp 失败,errno = 2(没有这样的文件或目录) gm 转换:“gs” “-q” “-dBATCH” “-dSAFER” “-dMaxBitmap=50000000” “-dNOPAUSE” “-sDEVICE=pnmraw” “-dTextAlphaBits=4” “-dGraphicsAlphaBits=4” “-r50x50”“-dFirstPage=1”“-dLastPage=1”“-sOutputFile=/tmp/gmhjqkzP” “--”“/tmp/gmZaShhP”“-c”“退出”。 gm 转换:请求未返回 一张图片。
这是什么原因以及如何解决?
问题在于
nginx
用户的 PATH 中没有 Ghostscript (即 gs
二进制文件),而您和 root 用户却有。因此 nginx
用户无法找到 gs
,也无法 exec
。
要做的第一件事是
su
或以 root
身份登录(或任何可以运行 ghostscript
的用户),然后运行:
type gs
它会告诉您Ghostscript的完整路径。然后,您需要确保
nginx
用户的 PATH 上有包含 gs
的目录。
或者,您可以更改 ImageMagick “delegates” 文件以包含 Ghostscript 可执行文件的完整路径。所以,你需要找到
ImageMagick使用的
delegates.xml
,如下所示:
identify -list configure | grep CONFIGURE_PATH
这会告诉你类似这样的信息,尽管在 debian 上会有所不同,因为我在 Mac 上使用 homebrew:
CONFIGURE_PATH /opt/homebrew/Cellar/imagemagick/7.1.1-21/etc/ImageMagick-7/
然后您需要将
delegates.xml
添加到该 PATH 上,以提供类似于:
/opt/homebrew/Cellar/imagemagick/7.1.1-21/etc/ImageMagick-7/delegates.xml
然后,备份该文件后,对其进行编辑并更改其显示
delegate decode=pdf
的位置,以便为所有用户提供指向 ghostscript
的完整路径。看起来像这样:
<delegate decode="pdf" encode="ps" mode="bi" command=""gs" -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sPDFPassword=%a" "-sOutputFile=%o" "-f%i""/>
因此,您需要将其更改为类似这样的内容,并包含
gs
的完整路径:
<delegate decode="pdf" encode="ps" mode="bi" command=""/FULL/PATH/TO/gs" -sstdout=%%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sPDFPassword=%a" "-sOutputFile=%o" "-f%i""/>