Configure Imagick PHP Extension in App Service
This blog explains how to configure Imagick PHP Extension in App Service and most common troubleshooting scenarios.
App Service Linux
PHP 8.x images are pre-built with imagick extension. It will pull the latest available version of imagick when a new tag is available following the App Service OS and runtime patching times. You can check the latest stable version of imagick here.
ImageMagick policies
ImageMagick best practices strongly encourages you to configure a security policy that suits your local environment. You can read more about these policies here.
Policies can be found on /etc/ImageMagick-6/policy.xml
If you desire to modify these policies on Azure App Service Linux, the best option to accomplish this will be to use a startup script.
Here is an example for removing policies for ghostscript:
#!/bin/bash
#Removing ghostscript policies
sed -i '/disable ghostscript format types/,+6d' /etc/ImageMagick-6/policy.xml
#Update Ghostscript
apt-get update && apt-get install -y ghostscript
service nginx reload
And then update the startup script location from Azure Portal:
Troubleshooting
ghostscript
-
Scenario: Uncaught ImagickException: attempt to perform an operation not allowed by the security policy.
PHP Fatal error: Uncaught ImagickException: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408 in /home/site/wwwroot/index.php:11\nStack trace:\n#0 /home/site/wwwroot/index.php(11): Imagick->readImage('random.pdf')\n#1 {main}\n thrown in /home/site/wwwroot/index.php on line 11
Resolution: Add a custom startup script to modify existing policy with read and/or write or removing the policy, check steps above.
-
Scenario: PHP Fatal error: Uncaught ImagickException: FailedToExecuteCommand ‘gs’
PHP Fatal error: Uncaught ImagickException: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' -dFirstPage=1 -dLastPage=1 '-sOutputFile=/tmp/magick-60nJzxfIQqHrXx%d' '-f/tmp/magick-60rZK5yQMK31K7' '-f/tmp/magick-60_Dsu584RGIyH'' (1) @ error/pdf.c/InvokePDFDelegate/291
Resolution: Add a custom startup script to modify existing policy with
apt-get upgrade && apt-get install -y ghostscript
, check steps above.
App Service Windows
Reference: Microsoft Q&A questions
Update 3/2/2023: Running PHP 7.4 or PHP in general on Azure App Service (Windows) is no longer supported since 28 November 2022. It is recommended to migrate your application to App Service Linux. If you need a specific version of PHP you can bring your own runtime but this will be out of support and you will need to update the versions for any vulnerability reported by the PHP community.
Based on your PHP version and Platform (32/64 bits), get the latest php_imagick.dll
from https://windows.php.net/downloads/pecl/releases/imagick/.
Since App Service Windows is running IIS as the webserver, you can select NTS (Non-Thread Safe). This is used when PHP runs on Fast CGI binary.
Custom PHP version
For this scenario php_imagick-3.7.0-7.4-nts-vc15-x64.zip
was selected which targets 7.4, you need to select the correct version for your application.
- Go to Kudu site browsing to https://yoursitename.scm.azurewebsites.net/DebugConsole
- Create a folder named
imagick
insidec:\home\site
orD:\home\site
(in any applicable scenario). -
Unzip the folder and copy all
CORE_RL_*
files toc:\home\site\imagick\
ord:\home\site\imagick\
-
Create two new folders
ext
andini
insidec:\home\site
ord:\home\site
(in any applicable scenario). -
Copy
php_imagick.dll
toc:\home\site\ext\
ord:\home\site\ext\
-
Create a new file named
extensions.ini
insidec:\home\site\ini\
ord:\home\site\ini\
with the following content:extension=C:\home\site\ext\php_imagick.dll
-
Create new file
applicationHost.xdt
file inside thec:\home\site
ord:\home\site
folder and copy the below configuration:<?xml version="1.0"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.webServer> <runtime xdt:Transform="InsertIfMissing"> <environmentVariables xdt:Transform="InsertIfMissing"> <add name="PATH" value="%PATH%;c:\home\site\imagick" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" /> </environmentVariables> </runtime> </system.webServer> </configuration>
-
Add the following App Settings in Azure Portal:
Note: Check if c or d drives applies to you.
- MAGICK_CODER_MODULE_PATH = c:\home\site\imagick
- MAGICK_HOME = c:\home\site\imagick
- PHP_INI_SCAN_DIR = c:\home\site\ini
- If you create a info page, you should see the following results:
<? phpinfo();