Learn how to deploy your Web Application & Database Server to Microsoft Azure.
The decision was made to deploy your work/app to Azure1, now you have the task of making it happen.
A Step-by-Step guide to deploying your first app on Microsoft Azure.
The first part of this guide focusses on getting a Linux Virtual Machine
launched on Azure.
You can then deploy what ever you like to it
and it will work similarly to other cloud providers.
After that we move on to deploying a Phoenix Web Application (because that is our chosen technology stack @dwyl) but the process is the same for any stack.
Developers who need to deploy to Azure because their client/employer requires them to. If you have no prior "Cloud" Infrastructure experience, you will have no expectations or pre-conceptions.
If you don't already have an account on Azure, go register for one now! /register-for-azure-account.md
Visit your Azure Dashboard: https://portal.azure.com
- Click on the "Burger" (☰) Menu to expand the service menu
- Click on "Virtual Machines" in the services menu
- Click on "+ Add" to add a Virtual Machine
The Azure Tutorial(s) all assume you have the CLI installed. e.g: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/tutorial-manage-vm I'm doing a step-by-step GUI tutorial instead because it's way more beginner friendly!
- Search for "Ubuntu LTS"
- Select the LTS Version which is currently
16.04 LTS
- Select the VM from the Right Menu
Note: Dependent on your screen size, you may need to scroll to the Right to reveal the "Create" Button. - Click "Create" button to start the creation process.
You should then see a screen similar to this:
- Name: give your VM a meaningful name e.g:
phoenix-prod-1
(useful if you end up having a cluster of several machines later on...) - VM disk type: SSD (keep the default)
- User name:
azure
(they don't let you have the usernameroot
even though which ever username you do pick here hasroot priviledges
!... I useazure
so it's clear which platform my VM is hosted by/on.) - Authentication Type:
SSH public key
(obviously more secure than password) - SSH public key - paste your public key here. (if you don't have a public key, how are you using GitHub...?! see: http://stackoverflow.com/questions/3828164/how-do-i-access-my-ssh-public-key ...) copy your ssh key into your clipboard using this command:
pbcopy < ~/.ssh/id_rsa.pub
- Subscription: Free Trial (obviously)
- Resource Group: [x] Create new.
Called it
phoenix-cluster
- Location:
West Europe
(pick what ever is closest to your end-users) - Click "OK" (finally!)
Select the cheapest VM available and then click the "Select" button.
Confirm the settings for your new instance (leave the defaults they are fine):
Click on "OK" to confirm.
Once you have confirmed the details, click "OK" to launch your instance.
Now Wait ...
Sadly, Azure takes some time to launch. Go re-fill your water bottle. ;-)
Make a note of the IP address of the instance so that you can login to it in the next step. Ours VM's IP Address is: 52.232.127.28.
In our case the user for the server
(which we defined in step 4.3 above) is azure
and the IP address of our VM/instance is 52.232.127.28
.
So we login using the following command
ssh azure@52.232.127.28
You will be asked to confirm you want to continue connecting. Type Yes and then [return].
In order to allow inbound TCP traffic into the instance
From the Azure Dashboard, Select "Virtual Machines" then click on your Machine:
Once you are viewing the details for your VM:
- Click on "Network Interfaces"
- Click on the name of the interface for you instance in our case: phoenix-prod-1594
- Click on "Network Security Group"
- Click on the the name of your Security Group in our case "phoenix-prod-1-nsg"
Next you will setup a Firewall rule for the VM.
After completing the preceeding step, you will see the network security group Overview.
Click on the (nonsensical) button to Create a new inbound security rule.
Then you will see the "Inbound Security Rules" where you can click on the "+ Add" button:
- Click on the "+ Add" button
- Select HTTP from the list of services (option "drop-down" list)
- Click "OK" button to create the new rule.
You should now see the rule in the Inbound security rules list:
While logged into the remote machine (the azure VM) run the following three commands:
sudo apt-get update
sudo apt-get install nginx -y
sudo service nginx start
sudo service nginx status
Now visit the IP address for your VM in a browser (in our case: http://52.232.127.28/) and you should expect to see the following:
After you've tested with NGINX, if you prefer to remove it (because you don't need it for serving your app) see: https://askubuntu.com/questions/235347/what-is-the-best-way-to-uninstall-nginx
Before deploying your app to the Azure Instance, shut down NGINX (if you still have it running from "Part 1")
sudo service nginx stop
For deploying a Phoenix Framework Web Application, see:
TODO: Link to
advanced-deployment.md
once PR containing instructions is merged!
Your Phoenix Web Application expects to have an environment variable
defined for the TCP PORT which the app will listen on.
In our case we are going to stick with the default and use 4000
.
Run the following command to append the line
export PORT=4000
to your ~/.profile
file:
"echo export PORT=4000" >> ~/.profile
Then run the following command to ensure that ~/.profile
file is loaded:
source ~/.profile
You can confirm that the PORT
environment variable is now define on the VM
by running the printenv
command:
printenv
On the Azure Instance run the following command:
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 4000
To confirm the routing from port 80 to 4000 run the following command:
sudo iptables -t nat --line-numbers -L
Now when you deploy the app to this instance
it will "listen" on PORT 4000,
but the Firewall will re-route http
requests from port 80
to 4000
.
Once you have configured your Phoenix App to deploy using Edeliver,
simply update the settings of your .deliver/config
file to
to the VM IP Address and username of your Azure instance:
PRODUCTION_HOSTS="52.232.127.28"
PRODUCTION_USER="azure"
DELIVER_TO="/home/azure"
Once you have updated the .deliver/config
file with the Azure VM details,
run these two commands from inside your Phoenix Project (on your local machine):
mix edeliver deploy release to production
mix edeliver start production
You should expect to see the following output:
Visit your app by IP Address in your Web Browser. e.g: http://52.232.127.28
We are still pending a decision on this feature ... see: #5
- Install NGINX on Ubuntu on Azure: https://ztirom.at/2016/01/setup-nginx-and-ubuntu-on-azure/
- Opening Ports on your Azure VM: http://stackoverflow.com/questions/38155616/azure-ubuntu-vm-endpoints
- http://www.phoenixframework.org/docs/deployment
- http://dokku.viewdocs.io/dokku/getting-started/installation/
- http://dokku.viewdocs.io/dokku/getting-started/install/azure/
- https://www.microsoft.com/developerblog/real-life-code/2015/10/30/Streamlined-Dokku-Deployment.html
- https://gist.github.com/henrik/c70e32544e09c1a79841
- http://blog.pragtechnologies.com/deploying-phoenix-using-dokku-in-azure/
1See: /tldr.md