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
gs permission

You want:
But
graphicsmagickdoes not findghostscript(mentioned in "Installing GraphicsMagick") when run by thenginxuser, even though thegspath is included in PATH in/etc/profile.Since nginx runs as a specific user (usually
nginxorwww-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
gsis/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
ghostscriptexecutable in yourgraphicsmagickcommand.Make sure the nginx user has execute permissions on the
ghostscriptbinary.And check that any security policies or apparmor profiles are not restricting access to the necessary paths for the nginx user.
The OP clarkk adds in the comments:
That option makes sure GraphicsMagick remembers the locations of external utilities like
ghostscript(gs) during its build process, eliminating the need to set the PATH environment variable or manually pass the full path togsin your commands.That method is particularly useful when dealing with environments like that of
nginx, where setting environment variables can be challenging.This is similar to this option: