Ealier I had both ghostscript
and graphicsmagick
installed via apt install ...
, but to support the latest versions I decided to uninstall and instead build and install from source
Before everything worked, but after installing from source graphicsmagick
commands that require ghostscript
fails (when running from nginx
)
Command
gm convert -density 50 input.pdf[0] -background white -resize 140x140 -strip -quality 40 thumb.jpg
The above command works fine when running it as root
, but when running it from the webserver nginx
an error is returned
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.
What is the reason for this and how to fix it?
Build and install (from source)
# 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
update
# type gs
gs is hashed (/usr/local/bin/gs)
The path /usr/local/bin
is already included in /etc/profile
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin
When printing the PATH from nginx (HTTP request)
echo shell_exec('echo $PATH');
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
2
Answers
The problem is that the
nginx
user does not have Ghostscript (i.e. thegs
binary) in their PATH, whereas you and the root user do. So thenginx
user cannot findgs
, and fails toexec
it.The first thing to do is
su
to, or login as,root
(or any user who can runghostscript
), then run:and it will tell you the full path to Ghostscript. Then you need to ensure that the
nginx
user has the directory containinggs
on their PATH.Another way to find the full path to Ghostscript is with:
Alternatively, you can alter the ImageMagick "delegates" file to include the full path to the Ghostscript executable. So, you need to find the
delegates.xml
that ImageMagick is using like this:and that will tell you something like this, though it will be different on debian since I am using homebrew on a Mac:
Then you need to add
delegates.xml
onto that PATH, to give something akin to:Then, after backing up that file, edit it and change where it says
delegate decode=pdf
so that it has the full path toghostscript
there for all users. It looks like this:So, you would need to change it to something more like this with the full path to
gs
:You want:
But
graphicsmagick
does not findghostscript
(mentioned in "Installing GraphicsMagick") when run by thenginx
user, even though thegs
path is included in PATH in/etc/profile
.Since nginx runs as a specific user (usually
nginx
orwww-data
), this user might not have the same PATH as the root user.To check the PATH available to the nginx user, you can modify a script run by nginx to include
echo $PATH > /tmp/nginx_path.txt
. That will print the PATH seen by nginx to a file.If the path to
gs
is/usr/local/bin/gs
, you can modify the startup script of nginx to include this path. For instance:Or, update the script that calls
graphicsmagick
:As a workaround, specify the full path to the
ghostscript
executable in yourgraphicsmagick
command.Make sure the nginx user has execute permissions on the
ghostscript
binary.And check that any security policies or apparmor profiles are not restricting access to the necessary paths for the nginx user.