PHP website using Zend framework can be deployed to Azure in many ways. For frontend, we may utilize either of Virtual Machines, Virtual Machine Scale Sets, Web Apps for Linux, Azure Container Instance or Azure kubernetes services. The first 2 here are Infrastructure as a service offering (IAAS) , while others are Platform as a service (PAAS). In this article, we will focus on deploying Websites using PHP open source framework, Zend (Version 2), to various azure PAAS offerings.
The end result would be like below –
Also we will be focusing on immutable deployment here. We will take the ARM template file which is part of commit, create environment from scratch and restore/deploy database/frontend. Everything will be done by VSTS and we will highlight the steps in detail on how its done.
Before we look into VSTS, lets take a quick look on our repository
below will pull an existing lamp image, copy my code to image, download the config file containing mysql credentials that is created as part of deployment and then start lamp servers.
COPY . /var/www/html
CMD curl http://containersforphpprereq.blob.core.windows.net/settings/config.ini -o /var/www/html/public/config.ini && /usr/sbin/run-lamp.sh
DockerFile of the image that i have used can be found here –
this file pulls the docker image and update aks cluster.
Before we see the steps in details, please note 3 azure components are created separately and are not part of this deployment. VNET, Storage and ACR. This is because, in my opinion, these resources should be planned beforehand and setup to meet application/enterprise requirements. Rest everything else can be taken care of later. However if you want, you can ignore this and add extra steps to ARM template to create these resources.
Now let us look at our build pipeline which will carry out the following steps
- create docker image which will be mounted on webapp, ACI and AKS
- push this image to ACR
- create the config file which will have mysql credentials and store it to a storage account
- run ARM template to deploy azure resources.
- deploy code to ACI, AKS and webapps
- restore database to MYSQL
- delete the config file from azure storage.
Below are the variables setup for my build pipeline. I have set all the credentials and keys here so that i don’t have to hard code them in my ARM template and I keep them blank in ARM. We will see later below how to override ARM template parameters.
1. In step 1 and 2, we will create the docker image and push it to ACR. We also have option to any other container registry like docker.
2. next we create the config.ini file. This file has credentials to mySQL and it will be pulled by containers when they start. website will read this config.ini file to connect to database.
3. then we use our ARM template to rollout the azure environment. this will also set docker image made in step 1 to ACI. Note how in Override Template Parameters step, I have overridden few sensitive parameter values like SQL creds, ACR creds and SPN. I keep them blank in templates as i don’t want to save this to git.
4. Once my environment rolls out, i restore my database to MYSQL server. my mysql backup file is committed along with code in git. In many scenarios, it is saved as blobs to storage. Just update the bash script in this step if your database backup is stored on blob.
5. next we deploy our code to web app. Look at the option ‘App Service Type’ in this task. Selecting web app for containers allows us to use a docker image and mount it on web app.
6. now we deploy code to AKS. we use the yaml file which is part of our code in git and apply it to AKS. YAML file, which has deployment details will take the image from ACR and apply it to AKS. See 2 key steps here. One in line 2 where we get the credentials and then in line three where we set the ‘secretname’ value to ACR creds. See above on how this secret is used to get the image for deployment.
7. Lastly we remove the file. Note we can skip this file creation step altogether by passing SQL creds to docker image as ENV variables and then using those values in php code.