Bringing you here, the most demanding tutorial on how to build a PHP web app in Azure. It’s scalable, self-patching, and easy to set up, making it a solid choice for PHP developers looking for a reliable cloud hosting solution. In this step-by-step tutorial, we’ll take you through the steps of building a PHP web app in Azure, using Azure CLI, and deploying it with Git. The process is straightforward and can be completed in just a few minutes. If you need extra support along the way, a trusted web app development company can help you set things up the right way from the start.
Key Takeaways:
The steps below can be used on Mac, Linux, and Windows. Once the essential requirements, such as PHP and Git, are installed, it will take about five minutes to complete all the steps.
You can create a free Azure account if you don’t have an Azure subscription before beginning.
The Azure Cloud Shell is a free Bash shell that can be run directly within the Azure account. It has Azure CLI already installed and configured so that it can be used with your account. Click on the Cloud Shell button on the upper right of the Azure portal in the menu.
The button will launch an interactive shell that can be used to run all the steps outlined in this topic.
Run az –version to find the version of the CLI you are using.
In the terminal window, run the following commands to copy the sample app repository to the local machine.
git clone https://github.com/Azure-Samples/php-docs-hello-world
Now change to the directory that contains the sample code.
cd php-docs-hello-world
First, run the application locally by opening a terminal window and using the PHP command to launch the pre-built PHP web server.
php -S localhost:8080
Open up any web browser and type the following localhost address: https://localhost:8080
You will see a HELLO WORLD message displayed on the screen from the sample app.
Exit the web server by pressing Ctrl+C
Use the Azure CLI 2.0 to create the resources required to host the app in Azure. Log in to the Azure subscription by using the AZ login command and follow the directions displayed on the screen.
az login
With the AZ web app deployment user command, create the deployment credentials.
A deployment user is necessary for FTP and Local GIT deployment. The username and password will be different from the Azure subscription credentials.
az webapp deployment user set –user-name <username> –password <password>
Replace the username and password with new ones. The name must be unique, and the password must be eight characters long.
It is a one-time process, and the deployment user created can be used for all Azure deployments.
With the AZ group create command, create a resource group.
A resource group is actually a container that holds Azure resources such as web apps, databases, and storage accounts to manage and deploy.
az group create –name myResourceGroup –location westeurope
Create the resource by following the above example.
Generally, the resources are created in a region near you. Run the az appservice list-location command to see the supported locations for the Azure web app.
With the az appservice plan create command, create an Azure app service.
The app service plan specifies the location, features, and size of the web server.
App Service plans define:
The example below creates an app service plan in free pricing.
az appservice plan create –name myAppServicePlan –resource-group myResourceGroup –sku FREE
After the app service plan is created, the AZURE CLI will show some information like this
{
"adminSiteName": null,
"appServicePlanName": "myAppServicePlan",
"geoRegion": "West Europe",
"hostingEnvironmentProfile": null,
"id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
"kind": "app",
"location": "West Europe",
"maximumNumberOfWorkers": 1,
"name": "myAppServicePlan",
< JSON data removed for brevity. >
"targetWorkerSizeId": 0,
"type": "Microsoft.Web/serverfarms",
"workerTierName": null
}
With the AZ webapp create command, create a web app in the app service plan.
The web app will provide a hosting space for the code and a URL to view the deployed app.
az webapp create –name <app_name> –resource-group myResourceGroup –plan myAppServicePlan
In the above command, replace the app name with a unique name.
Once the web app is created, the Azure CLI will show the information below.
{
"availabilityState": "Normal",
"clientAffinityEnabled": true,
"clientCertEnabled": false,
"cloningInfo": null,
"containerSize": 0,
"dailyMemoryTimeQuota": 0,
"defaultHostName": "<app_name>.azurewebsites.net",
"enabled": true,
"enabledHostNames": [
"<app_name>.azurewebsites.net",
"<app_name>.scm.azurewebsites.net"
],
"gatewaySiteName": null,
"hostNameSslStates": [
{
"hostType": "Standard",
"name": "<app_name>.azurewebsites.net",
"sslState": "Disabled",
"thumbprint": null,
"toUpdate": null,
"virtualIp": null
}
< JSON data removed for brevity. >
}
Type the following address in any browser to check your newly created web app.
http://<app_name>.azurewebsites.net
A web app in Azure has been created.
With the az webapp deployment source configuration-git command, configure local Git deployment to the web app.
For quickstart, deploy by using local Git. To deploy content to the web app, the app service offers several ways, such as FTP, Local Git, GitHub, and Bitbucket.
az webapp deployment source config-local-git –name <app_name> –resource-group myResourceGroup –query url –output tsv
Replace app name with the web app’s name in the above command.
The output format will be :
https://<username>@<app_name>.scm.azurewebsites.net:443/<app_name>.git
The username is the deployment username which you have created previously. Save the above URL to use in the next steps.
git remote add azure <URI from previous step>
Add Azure remote to the local GIT repository,
Push to the Azure remote to deploy the app. Ensures the password you entered is the deployment user password, not the password you used to log in to the Azure portal.
git push azure master
The above command will display the following information.
Counting objects: 2, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 352 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: Updating branch 'master'. remote: Updating submodules. remote: Preparing deployment for commit id '25f18051e9'. remote: Generating deployment script. remote: Running deployment command... remote: Handling Basic Web Site deployment. remote: Kudu sync from: '/home/site/repository' to: '/home/site/wwwroot' remote: Copying file: '.gitignore' remote: Copying file: 'LICENSE' remote: Copying file: 'README.md' remote: Copying file: 'index.php' remote: Ignoring: .git remote: Finished successfully. remote: Running post deployment command(s)... remote: Deployment successful. To https://<app_name>.scm.azurewebsites.net/<app_name>.git cc39b1e..25f1805 master -> master
Type the URL below to browse the deployed application in any web browser.
http://<app_name>.azurewebsites.net
Congratulations! You have deployed the PHP app to App Service.
And that’s it, your PHP app is now live on Azure! As you can see, the process is pretty straightforward once you have everything set up. Azure App Service makes it simple to deploy, manage, and scale your PHP applications without a lot of hassle. Whether you’re building something simple or a more complex web solution, Azure gives you a reliable and flexible platform to work with.
Arpatech takes the complexity out of building and deploying PHP apps on Azure. Reach out today and let’s make it happen.