Events
Apr 8, 3 PM - May 28, 7 AM
Sharpen your AI skills and enter the sweepstakes to win a free Certification exam
Register now!This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Azure App Service provides a highly scalable, self-patching web hosting service using the Linux operating system. This tutorial shows how to create a secure PHP app in Azure App Service that's connected to a MySQL database (using Azure Database for MySQL Flexible Server). When you're finished, you'll have a Laravel app running on Azure App Service on Linux.
In this tutorial, you learn how to:
To follow along with this tutorial, clone or download the sample application from the repository:
git clone https://github.com/Azure-Samples/laravel-tasks.git
If you want to run the application locally, do the following:
In .env, configure the database settings (like DB_DATABASE
, DB_USERNAME
, and DB_PASSWORD
) using settings in your local Azure Database for MySQL Flexible Server database. You need a local Azure Database for MySQL Flexible Server instance to run this sample.
From the root of the repository, start Laravel with the following commands:
composer install
php artisan migrate
php artisan key:generate
php artisan serve
In this step, you create the Azure resources. The steps used in this tutorial create an App Service and Azure Database for MySQL Flexible Server configuration that's secure by default. For the creation process, you'll specify:
https://<app-name>.azurewebsites.net
.Sign in to the Azure portal and follow these steps to create your Azure App Service resources.
Instructions | Screenshot |
---|---|
In the Azure portal:
|
![]() |
In the Create Web App + Database page, fill out the form as follows.
|
![]() |
The deployment takes a few minutes to complete, and creates the following resources:
|
![]() |
The creation wizard generated app settings for you to use to connect to the database, but not in a format that's useable for your code yet. In this step, you edit and update app settings to the format that your app needs.
Instructions | Screenshot | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
In the App Service page, in the left menu, select Configuration. |
![]() |
||||||||||
In the Application settings tab of the Configuration page, for each of the following settings, select Edit, update the Name field with new values and select OK.
|
![]() |
||||||||||
Create a new MYSQL_ATTR_SSL_CA database setting:
|
![]() |
||||||||||
Create the following extra app settings by following the same steps, then select Save.
|
![]() |
In this step, you'll configure GitHub deployment using GitHub Actions. It's just one of many ways to deploy to App Service, but also a great way to have continuous integration in your deployment process. By default, every git push
to your GitHub repository will kick off the build and deploy action. You'll make some changes to your codebase with Visual Studio Code directly in the browser, then let GitHub Actions deploy automatically for you.
Instructions | Screenshot |
---|---|
In a new browser window:
|
![]() |
In the GitHub page, open Visual Studio Code in the browser by pressing the . key. |
![]() |
In Visual Studio Code in the browser, open config/database.php in the explorer.In the mysql connection, see that the app settings you created earlier for the MySQL connection are already used (DB_HOST , DB_DATABASE , DB_USERNAME , DB_PASSWORD , MYSQL_ATTR_SSL_CA ). |
![]() |
Back in the App Service page, in the left menu, select Deployment Center. |
![]() |
In the Deployment Center page:
.github/workflows directory. |
![]() |
In the Deployment Center page:
|
![]() |
To make any changes to your code, go to Visual Studio Code in the browser:
Tip The GitHub action is defined by the file in your GitHub repository, in .github/workflow. You can make it faster by customizing the file. |
![]() |
The creation wizard puts the Azure Database for MySQL Flexible Server instance behind a private endpoint, so it's accessible only from the virtual network. Because the App Service app is already integrated with the virtual network, the easiest way to run database migrations with your database is directly from within the App Service container.
Instructions | Screenshot |
---|---|
In the App Service page:
https://<app-name>.scm.azurewebsites.net/webssh/host instead. |
![]() |
In the SSH terminal:
|
![]() |
Laravel application lifecycle begins in the /public directory instead. The default PHP 8.0 container for App Service uses Nginx, which starts in the application's root directory. To change the site root, you need to change the Nginx configuration file in the PHP 8.0 container (/etc/nginx/sites-available/default). For your convenience, the sample repository contains a custom configuration file called default. As noted previously, you don't want to replace this file using the SSH shell, because your changes will be lost after an app restart.
When you're finished, you can delete all of the resources from your Azure subscription by deleting the resource group.
Pricing for the create resources is as follows:
To connect to an Azure Database for MySQL Flexible Server database, you can use several methods based on the tools and environments at your disposal:
mysql
command from the app's SSH terminal for basic access.Take the autogenerated workflow file from App Service as an example, each git push
kicks off a new build and deployment run. From a local clone of the GitHub repository, you make the desired updates push it to GitHub. For example:
git add .
git commit -m "<some-message>"
git push origin main
The autogenerated workflow file from App Service defines build-then-deploy, two-job run. Because each job runs in its own clean environment, the workflow file ensures that the deploy
job has access to the files from the build
job:
build
job, upload files as artifacts.deploy
job, download the artifacts.Most of the time taken by the two-job process is spent uploading and download artifacts. If you want, you can simplify the workflow file by combining the two jobs into one, which eliminates the need for the upload and download steps.
In this tutorial, you learned how to:
Events
Apr 8, 3 PM - May 28, 7 AM
Sharpen your AI skills and enter the sweepstakes to win a free Certification exam
Register now!