How to setup Laravel5 on Azure

Laravel has become a leading PHP framework because of its many significant features. When deploying to Azure, there are a few nuances. Here we attempt to cover them.

Steps for Laravel on Azure:

  1. Install PHP version >= 5.4
  2. Install Composer
  3. Perform Directory Setup
  4. URL Rewriting

1. Install PHP version >= 5.4
Laravel5 requires PHP version >= 5.4. Azure provides an option to install the needed PHP version as per our project. To change the PHP version in Azure, follow the below steps

  • Log into Azure, and click the web service link. It will list all settings under your web service.
  • Go to `SETTINGS` and click `Application Settings` Link. It will list all app settings.
  • Go to `General Settings and choose the `PHP version` and save.

2. Install Composer
Composer is an important package manager in PHP. It is mainly used with Laravel for package management. A composer can be easily installed on Azure via its extension manager.

  • Click your web service link. It will list all settings under your web service.
  • Go to `DEVELOPMENT TOOLS` and click on `Extensions`.
  • Click on the `add` link and select the option `Choose Extension`. It will list all Extensions related to your web service.
  • Select Composer from the list and do the installation process (you must agree to the license etc.).

3. Perform Directory Setup
Directory setup is an important process in Laravel projects. Laravel lifecycle always starts from the file `public/index.php`. Azure deploys everything you push into the `wwwroot` directory.

To change the project start up point in Azure, follow the below steps:

  • Click your web service link. It will list all the settings under your web service.
  • Go to `SETTINGS` and click ` Application Settings` Link. It will list all app settings.
  • Go to `Virtual applications and directories and provide project folder path as (site\wwwroot\public)

4. URL Rewriting
Generally, if we type an invalid URL on a laravel based site, we get redirected to 404 page.This occurs because of URL mod_rewrite rule.Laravel assumes that it is dealing with an Apache Web-Server, therefore it processes the mod_rewrite rules specific to that.But Azure Web Apps use Microsoft’s IIS.Hence, we need to include the below-mentioned changes for URL rewriting.
Create a new file called web.config in public directory with the below content.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>
<system.webServer>
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
<staticContent>
<remove fileExtension=".svg" />
<mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<clientCache httpExpires="Mon, 30 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>
<handlers>
<remove name="OPTIONSVerbHandler" />
<remove name="PHP54_via_FastCGI" />
<add name="PHP54_via_FastCGI" path="*.php" verb="GET,HEAD,POST,OPTIONS" modules="FastCgiModule" scriptProcessor="D:\Program Files (x86)\PHP\v5.4\php-cgi.exe" resourceType="Either" />
</handlers>
<rewrite>
<rules>
<rule name="Laravel5" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Show your support

Clapping shows how much you appreciated Saravanan’s story.