Search code examples
phpapachepermissionsimagick

Permission denied while calling 'convert' command from PHP


I am trying to use ImageMagick from PHP. I installed ImageMagick and the PHP extension on a virtual CentOS Server. Executing the convert command from bash works as expected. If I make a PHP file with the content

<?php exec('convert /home/vagrant/test/image.jpg -resize 200x200 /home/vagrant/test/image_converted.jpg'); ?>

and call it in PHP using the command line php convert.php it works as well. But if I start my Apache server and try to access the page trough my browser it returns the error:

convert: unable to open image `/home/vagrant/test/image.jpg': Permission denied @ blob.c/OpenBlob/2480.
convert: missing an image filename `/home/vagrant/test/image_converted.jpg' @ convert.c/ConvertImageCommand/2800.

So, I thought it might be about permissions. Calling ps aux | egrep '(apache|httpd)' showed processes of a user named apache, so I assume that's the server. So I tried changing ownership to this user

sudo chown -R apache /home/vagrant/test/

Again, calling the website I get the error messages. Then I tried giving all users full access

sudo chmod -R 777 /home/vagrant/test/

--> drwxrwxrwx. 2 apache vagrant 4096 Dec 21 16:56 test

Again, the same error occurred. What am I doing wrong, please?


Solution

  • You only change the permissions for the subdirectory. If apache has insufficient privileges for home or home/vargrant, you won't be able to access the file.

    You can check if a user has sufficient privileges using sudo:

    sudo -u apache cp /home/vagrant/test/image.jpg /home/vagrant/test/image_converted.jpg