Create, configure, and deploy a PHP web app to Azure
This tutorial shows you how to create, configure, and deploy a PHP web app for Azure, and how to configure Azure App Service to meet the requirements of your PHP web app. By the end of the tutorial, you will have a working Laravel web app running live in Azure App Service.
As a PHP developer, you can bring your favorite PHP framework to Azure. This tutorial uses Laravel simply as a concrete app example. You will learn:
- Deploy using Git
- Set PHP version
- Use a start file that is not in the root application directory
- Access environment-specific variables
- Update your app in Azure
You can apply what you learn here to other PHP web apps that you deploy to Azure.
- Install PHP 5.6.x (PHP 7 support is beta)
- Install Composer
- Install Azure CLI
- Install Git
- Get a Microsoft Azure account. If you don't have an account, you can sign up for a free trial or activate your Visual Studio subscriber benefits.
See a web app in action. Try App Service immediately and create a short-lived starter app—no credit card required, no commitments.
Open a new Windows command prompt, PowerShell window, Linux shell, or OS X terminal. Run the following commands to verify that the required tools are installed properly on your machine.
php --version composer --version azure --version git --version
If you haven't installed the tools, see Prerequisites for download links.
Install Laravel like so:
composer global require "laravel/installer
CDinto a working directory and create a new Laravel application like so:
cd <working_directory> laravel new <app_name>
CDinto the newly created
<app_name>directory and test the app like so:
cd <app_name> php artisan serve
You should be able to navigate to http://localhost:8000 in a browser now and see the Laravel splash screen.
So far, just the regular Laravel workflow, and you're not here to learn Laravel. So let's move on.
"Wait! What if I want to deploy with FTP?" There's an FTP tutorial for your needs.
With the Azure CLI, you can create an web app in Azure App Service and set it up for Git deployment with a single line of command. Let's do this.
Log in to Azure like so:
Follow the help message to continue the login process.
Run the command to create the Azure web app with Git deployment. When prompted, specify the number of the desired region.
azure site create --git <app_name>
If you've never set up deployment credentials for your Azure subscription, you'll be prompted to create them. These credentials, not your Azure account credentials, are used by App Service only for Git deployments and FTP logins.
This command creates a new Git repository on the current directory (with
git init) and connects it to the repository in Azure as a Git remote (with
git remote add).
For your Laravel app to work in Azure, you need to pay attention to several things. You'll do this similar exercise for your PHP framework of choice.
- Configure PHP 5.5.9 or above. See Latest Laravel 5.2 Server Requirements for the whole list of server requirements. The rest of the list are extensions that are already enabled by Azure's PHP installations.
- Set the environment variables your app needs. Laravel uses the
.envfile for easy setting of environment variables. However, since it is not supposed to be committed into source control (see Laravel Environment Configuration, you will set the app settings of your Azure web app instead.
- Make sure that the Laravel app's entry point,
public/index.php, is loaded first. See Laravel Lifecycle Overview. In other words, you need to set the web app's root URL to point to the
- Enable the Composer extension in Azure, since you have a composer.json. That way, you can let Composer worry about obtaining your required packages when you deploy with
git push. It's a matter of convenience. If you don't enable Composer automation, you just need to remove
.gitignorefile so that Git includes ("un-ignores") everything in the
vendordirectory when committing and deploying code.
Let's configure these tasks sequentially.
Set the PHP version that your Laravel app requires.
azure site set --php-version 5.6
You're done setting the PHP version!
Generate a new
APP_KEYfor your Azure web app and set it as an app setting for your Azure web app.
php artisan key:generate --show azure site appsetting add APP_KEY="<output_of_php_artisan_key:generate_--show>"
Also, turn on Laravel debugging in order to preempt any cryptic
Whoops, looks like something went wrong.page.
azure site appsetting add APP_DEBUG=true
You're done setting environment variables!
Wait, let's slow down a bit and explain what Laravel does and what Azure does here. Laravel uses the
.envfile in the root directory to supply environment variables to the app, where you'll find the line
APP_KEY=...). This variable is accessed in
config/app.phpby the code
'debug' => env('APP_DEBUG', false),. env() is a Laravel helper method that uses the PHP getenv() under the covers.
.envis ignored by Git because it's called out by the
.gitignorefile in the root directory. Simply put,
.envin your local Git repository is not pushed to Azure with the rest of the files. Of course, you can just remove that line from
.gitignore, but we've already established that committing this file into source control is not recommended. Nevertheless, you still need a way to specify these environment variables in Azure.
The good news is that app settings in Azure App Service supports getenv() in PHP. So while you can use FTP or other means to manually upload a
.envfile into Azure, you can just specify the variables you want as Azure app settings without a
.envin Azure, like you just did. Furthermore, if a variable is in both a
.envfile and in Azure app settings, the Azure app setting wins.
Starting from the left menu, click App Services > <app_name> > Tools.
Click Extensions > Add to add an extension.
Select Composer in the Choose extension blade (blade: a portal page that opens horizontally).
Click OK in the Accept legal terms blade.
Click OK in the Add extension blade.
When Azure is done adding the extension, you should see a friendly pop-up message in the corner, as well as Composer listed in the Extensions blade.
You're done enabling Composer!
Back in your web app's blade, click Settings > Application Settings.
In the Application Settings blade, note the PHP version you set earlier:
and the app settings you added:
Scroll to the bottom of the blade and change the root virtual directory to point to site\wwwroot\public instead of site\wwwroot.
Click Save at the top of the blade.
You're done setting the virtual directory!
You're ready to deploy your code now. You'll do this back in your command prompt or terminal.
Commit all your changes and deploy your code to the Azure web app like you would in any Git repository:
git add . git commit -m "Hurray! My first commit for my Azure app!" git push azure master
git push, you will be asked to supply your Git deployment password. If you asked to create deployment credentials at
azure site createearlier, type in the password you used.
Let's see it run in the browser by running this command:
azure site browse
Your browser should show you the Laravel splash screen.
Congratulations, you are now running a Laravel web app in Azure.
Here are some the errors you might run into when following this tutorial:
- Azure CLI shows "'site' is not an azure command"
- Web app shows HTTP 403 error
- Web app shows "Whoops, looks like something went wrong."
- Web app shows "No supported encryptor found."
azure site * in the command-line terminal, you see the error
error: 'site' is not an azure command. See 'azure help'.
This is usually a result of switching in to "ARM" (Azure Resource Manager) mode. To resolve this, switch back into "ASM" (Azure Service Management) mode by running
azure config mode asm.
You have deployed your web app to Azure successfully, but when you browse to your Azure web app, you get an
HTTP 403 or
You do not have permission to view this directory or page.
This is most likely because the web app can't find the entry point to the Laravel app. Make sure that you have changed the root virtual directory to point to
site\wwwroot\public, where Laravel's
index.php is (see Configure the Azure web app).
You have deployed your web app to Azure successfully, but when you browse to your Azure web app, you get the cryptic message
Whoops, looks like something went wrong.
To get a more descriptive error, enable Laravel debugging by setting
APP_DEBUG environment variable to
true (see Configure the Azure web app).
You have deployed your web app to Azure successfully, but when you browse to your Azure web app, you get the error message below:
That's a nasty error, but at least it's not cryptic since you turned on Laravel debugging. A cursory search of the error string on the Laravel forums will show you that it is due to not setting the APP_KEY in
.env, or in your case, not having
.env in Azure at all. You can fix this by adding setting
APP_KEY as an Azure app setting (see Configure the Azure web app).
Learn how to add data to your app by creating a MySQL database in Azure. Also, check out more helpful links for PHP in Azure below: