Allow Apache PHP to access Unix Command or software.

Recently i was working with ImageMagick software (Open Source), basically a very powerful image suite to create, edit, and compose bitmap images. It can manipulate any sort of image just by using command line. Image using Photoshop with command line.

But calling an ImageMagick command using PHP Web pages required some configuration in the visudo files. Apparently, by default unix os will restrict any call or process that do not have any permission to access the internal command. If you wish to execute ImageMagick, you either required to use root user or user who is allowed externally to access the command. One of the way is to enable apache user, remove the Required TTY with visudo and add this to line into visudo (APACHE ALL = (ALL) NOPASSWD). I won’t say this is the only way, but this is one of the most easier. You can either use the PHP ImageMagick library or create and assign a group for apache that are only allowed it to access certain thing in the unix.

But the problem with using ImageMagick PHP library packages is that you can’t have much flexibility and control compare of using the command line.

THIS STEPS BELOW ARE NOT RECOMMENDED IF YOU DO NOT WANT TO RISK YOUR WEB SERVER.THIS SOLUTION ARE ONLY FOR TEMPORARILY RELIEVED UNTIL YOU FOUND A SAFER SOLUTION.

1. Open up your sudo configuration using visudo command.
2. Find the line Required TTY and comment it out using #Required TTY
3. Add another line that remove required password when apache trying to use the command. : APACHE ALL = (ALL) NOPASSWD
4. Restart your apache and you are done with service httpd restart. Check your apache error log if you still can’t run the command to access ImageMagick.

Here are the list of ImageMagick PHP Packages :
———————————————-
MagickWand for PHP (PHP)
IMagick (PHP)

Advertisements