July 9, 2016
In this tutorial, we’ll be creating the simplest possible PHP app, and then deploying it to the Azure App Service using Git. Although there are already several PHP-on-Azure tutorials online, many of them are either overly complex, or outdated.
You’ll only need a few things:
- An Azure account; free and trial accounts will be fine for this exercise. If you don’t have an Azure account yet, go and create a free account before proceeding.
- Git must be installed on your machine.
- You should be comfortable using the command line.
Begin by signing in to the Azure admin portal. Once you have done this, you’ll see this menu:
Click on App Services. You’ll then see this screen:
Next, click on Add to create a new app. When you do, you’ll see this:
Enter a name for your app. It’ll have to be a name that nobody has yet used on azurewebsites.net. If you don’t see a green check mark, change the name of your app until you’ve found one that is unique.
If you have any existing subscriptions, you can choose between them in the drop-down box. If not, just leave the default setting.
If you have any existing Azure resource groups, you can click ‘Use Existing’ to choose one of them. Otherwise, choose ‘Create New’ and enter a resource group name. Don’t worry if you don’t know what a resource group is – we’ll cover that in a future tutorial.
When you’re done, click the Create button. You will be redirected to the App Services screen. You’ll see several status messages pop up at the top right of your web browser. They’ll look something like this:
Wait until you see a message telling you that your application is ready to use. When that happens, click the Refresh button:
When the page refreshes, you should see your new application in the list:
Click on your application to load its setting screen. It will look like this:
Then, click on Application Settings, and you’ll see the following panel:
Change the PHP version to 7.0. You can leave it set to the default of 5.4 if you prefer, but unless you need to use an older PHP version, I suggest version 7. Click Save when you’ve done this. Next click on Deployment Credentials in the Settings menu. You’ll see the following screen:
Enter a user name and password. Remember these credentials, because you’ll need them to deploy via Git in a few minutes. When you’re done, click Save. When you’ve saved your credentials, click on Deployment Source in the Settings menu. You’ll see this:
Click on Choose Source, and you’ll see this:
Next, click Local Git Repository, and then click the Ok button to save your choice. Next, click Properties in the Settings menu. On the right, you’ll see field marked GIT URL. Click on the button beside it to copy it to the clipboard.
You’re done with the Azure configuration! All that’s left is to create your application and upload it.
Start by opening a command line or terminal window. Navigate to the directory where you’re going to create your Azure PHP app. Create a subdirectory for it by typing the following:
Then change into the newly created directory by typing:
Now that we’re in the newly created directory, initialize a git repository by typing:
Next, we’ll add our app’s Azure repository as a Git remote. Type
git remote add azure https://*****@phptest***.scm.azurewebsites.net:443/PhpTest47988.git
but paste in your own repository address after the word azure. It should still be on your clipboard, but if not, go back to the Azure admin panel and copy it again.
Once you’ve added the remote repository, you’re ready to create your ultra-simple PHP app. Using the text editor of your choice, create a file named index.php in your application’s directory and put the following content in it:
<title>PHP on Azure</title>
<h1>Hello from PHP on Azure!</h1>
echo 'The date is: ' . date('Y-m-d H:i:s');
Save the file, and go back to your terminal/command prompt. Type
git add .
git commit -m "initial commit"
At this point, you’re almost done! There’s just one last command to type:
git push azure master
When you hit Enter, Git will ask for your credentials. Use the user name and password you set on the Deployment Credentials screen. Enter these, and Azure will receive your Git push and deploy your site!
To see it in action, just navigate to https://your-site-name.azurewebsites.net, replacing your-site-name with the name you chose for your application. You should see this:
And you’re in business! You’ve just deployed your first PHP app to Azure. It doesn’t do much yet, but the hardest part is out of the way. Watch for a future tutorial on adding Azure SQL Database functionality to our test application.