Deploy an Apache Webserver on Ubuntu VM using Bicep

8 mins read

Quickly deploy your Apache Webserver in Azure using Bicep

💪Deploy an Apache Webserver on Ubuntu VM using Bicep

This article will show how you can quickly deploy an Apache Webserver on an Ubuntu VM on Azure using Bicep, the new Domain Specific Language (DSL) for deploying Azure resources declaratively.

Pre-requisites:

Azure Bicep installedAn active Azure subscriptionA resource group created in your subscription

We will use the Azure Linux CustomScript extension to deploy the Apache webserver.

The solution consists of the following files:

Main.bicep: This is our main Bicep templateazuredeploy.parameters.json: This is the parameters file that will be passed on during deployment timeinstall_script.sh: This is the script that will be executed as “Custom script” once the VM is deployed.

Now let’s start working on the Bicep template.

We will define the following parameters:

@description(‘Unique DNS Name for the Storage Account where the Virtual Machine’s disks will be placed.’)
param newStorageAccountName string@description(‘User name for the Virtual Machine.’)
param adminUsername string@description(‘Unique DNS Name for the Public IP used to access the Virtual Machine.’)
param dnsNameForPublicIP string@allowed([
‘18.04-LTS’
‘16.04.0-LTS’
‘14.04.5-LTS’
])
@description(‘The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version. Allowed values: 18.04-LTS, 16.04.0-LTS, 14.04.5-LTS.’)
param ubuntuOSVersion string = ‘18.04-LTS’@description(‘Size of the virtual machine’)
param vmSize string = ‘Standard_A0’@description(‘Location for all resources.’)
param location string = resourceGroup().location@allowed([
‘sshPublicKey’
‘password’
])
@description(‘Type of authentication to use on the Virtual Machine. SSH key is recommended.’)
param authenticationType string = ‘password’@description(‘SSH Key or password for the Virtual Machine. SSH key is recommended.’)
@secure()
param adminPasswordOrKey string

Note we allow the use of Password or SSH Keys to deploy the Ubuntu VM, in this example, we will pass on a value for the Password. You can opt to use SSH Keys.

Now we will define the variables as shown below:

var imagePublisher = ‘Canonical’
var imageOffer = ‘UbuntuServer’
var nicName_var = ‘myVMNic’
var addressPrefix = ‘10.0.0.0/16’
var subnetName = ‘Subnet’
var subnetPrefix = ‘10.0.0.0/24’
var publicIPAddressName_var = ‘myPublicIP’
var publicIPAddressType = ‘Dynamic’
var vmName_var = ‘MyUbuntuVM’
var virtualNetworkName_var = ‘MyVNET’
var subnetRef = resourceId(‘Microsoft.Network/virtualNetworks/subnets’, virtualNetworkName_var, subnetName)
var linuxConfiguration = {
disablePasswordAuthentication: true
ssh: {
publicKeys: [
{
path: ‘/home/${adminUsername}/.ssh/authorized_keys’
keyData: adminPasswordOrKey
}
]
}
}
var networkSecurityGroupName_var = ‘default-NSG’

You can modify the values for the definition of the virtual network and subnets depending on your needs.

Next, we will define the resources:

resource newStorageAccountName_resource ‘Microsoft.Storage/storageAccounts@2021-01-01’ = {
name: newStorageAccountName
location: location
sku: {
name: ‘Standard_LRS’
}
kind: ‘StorageV2’
}resource publicIPAddressName ‘Microsoft.Network/publicIPAddresses@2020-05-01’ = {
name: publicIPAddressName_var
location: location
properties: {
publicIPAllocationMethod: publicIPAddressType
dnsSettings: {
domainNameLabel: dnsNameForPublicIP
}
}
}resource networkSecurityGroupName ‘Microsoft.Network/networkSecurityGroups@2020-05-01’ = {
name: networkSecurityGroupName_var
location: location
properties: {
securityRules: [
{
name: ‘default-allow-22’
properties: {
priority: 1000
access: ‘Allow’
direction: ‘Inbound’
destinationPortRange: ’22’
protocol: ‘Tcp’
sourceAddressPrefix: ‘*’
sourcePortRange: ‘*’
destinationAddressPrefix: ‘*’
}
}
{
name: ‘default-allow-80’
properties: {
priority: 1001
access: ‘Allow’
direction: ‘Inbound’
destinationPortRange: ’80’
protocol: ‘Tcp’
sourceAddressPrefix: ‘*’
sourcePortRange: ‘*’
destinationAddressPrefix: ‘*’
}
}
]
}
}resource virtualNetworkName ‘Microsoft.Network/virtualNetworks@2020-05-01’ = {
name: virtualNetworkName_var
location: location
properties: {
addressSpace: {
addressPrefixes: [
addressPrefix
]
}
subnets: [
{
name: subnetName
properties: {
addressPrefix: subnetPrefix
networkSecurityGroup: {
id: networkSecurityGroupName.id
}
}
}
]
}
}resource nicName ‘Microsoft.Network/networkInterfaces@2020-05-01’ = {
name: nicName_var
location: location
properties: {
ipConfigurations: [
{
name: ‘ipconfig1’
properties: {
privateIPAllocationMethod: ‘Dynamic’
publicIPAddress: {
id: publicIPAddressName.id
}
subnet: {
id: subnetRef
}
}
}
]
}
dependsOn: [
virtualNetworkName
]
}resource vmName ‘Microsoft.Compute/virtualMachines@2020-06-01’ = {
name: vmName_var
location: location
properties: {
hardwareProfile: {
vmSize: vmSize
}
osProfile: {
computerName: vmName_var
adminUsername: adminUsername
adminPassword: adminPasswordOrKey
linuxConfiguration: ((authenticationType == ‘password’) ? json(‘null’) : linuxConfiguration)
}
storageProfile: {
imageReference: {
publisher: imagePublisher
offer: imageOffer
sku: ubuntuOSVersion
version: ‘latest’
}
osDisk: {
name: ‘${vmName_var}_OSDisk’
caching: ‘ReadWrite’
createOption: ‘FromImage’
}
}
networkProfile: {
networkInterfaces: [
{
id: nicName.id
}
]
}
}
dependsOn: [
newStorageAccountName_resource
]
}resource vmName_install_apache ‘Microsoft.Compute/virtualMachines/extensions@2020-06-01’ = {
parent: vmName
name: ‘install_apache’
location: location
properties: {
publisher: ‘Microsoft.Azure.Extensions’
type: ‘CustomScript’
typeHandlerVersion: ‘2.1’
autoUpgradeMinorVersion: true
settings: {
skipDos2Unix: false
fileUris: [
https://gist.githubusercontent.com/daveRendon/72986871085786d04d0cdc2b1065355b/raw/34b2a4b5e05dc32f695c8236c89a2c62ce6213ca/install_apache.sh’
]
}
protectedSettings: {
commandToExecute: ‘sh install_apache.sh’
}
}
}

Note we are leveraging the Custom Scripts functionality. These are scripts that can be embedded in templates or in external script files.

You will be able to reference the script that you want to execute as an “Extension” once your virtual machine is deployed.

You can specify the script language and platform. Currently, Azure PowerShell and Azure CLI deployment scripts on the Linux environment are supported.

In this example, we are going to use a script called “install_apache.sh”.

Custom scripts can be consumed from an external resource such as Gist and preferably by storing them in a storage account and then referencing the URL and the SAS token to access the script.

You can grab the code for the “install_apache.sh” here:

https://medium.com/media/730d1c12f56fe06e481b412a90cef3b4/href

The next step is to define the parameters file. While you can pass them on manually during deployment time, we will use a separate file instead.

The code below shows the definition of the parameters file:

{
“$schema”: “https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#”,
“contentVersion”: “1.0.0.0”,
“parameters”: {
“newStorageAccountName”: {
“value”: “Your-Storage-Accoung”
},
“adminUsername”: {
“value”: “Your-Admin-username”
},
“dnsNameForPublicIP”: {
“value”: “Your-DNS-Name”
},
“adminPasswordOrKey”: {
“value”: “Your-Password-or-Key”
}
}
}

You can find the complete code of the Bicep template here:

https://medium.com/media/237e4ec97fe32efc47d35e69d4431b13/href

Now we will deploy the Bicep template using the command below:

$date = Get-Date -Format “MM-dd-yyyy”
$deploymentName = “AzInsiderDeployment”+”$date”New-AzResourceGroupDeployment -Name $deploymentName -ResourceGroupName azinsider-apache -TemplateFile .main.bicep -TemplateParameterFile .azuredeploy.parameters.json -c

The image below shows the preview of the deployment.

Deployment preview

After a few minutes you should be able to see that the deployment was successfully completed:

Azure Bicep deploy apache webserver on ubuntu

You can now head to your browser and test using the DNS name you assigned to your resource:

Apache 2 Ubuntu default page

Hope you find this easy to deploy!

👉 Join the AzInsider email list here.

-Dave R.

đź’ŞDeploy an Apache Webserver on Ubuntu VM using Bicep was originally published in CodeX on Medium, where people are continuing the conversation by highlighting and responding to this story.

Leave a Reply

Your email address will not be published.

Follow Us