Awesome
Learn Microsoft Azure
Learn how to deploy your Web Application & Database Server to Microsoft Azure.
Why?
The decision was made to deploy your work/app to Azure<sup>1</sup>, now you have the task of making it happen.
What?
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. <br /> 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.
Who?
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.
How?
If you don't already have an account on Azure, go register for one now! /register-for-azure-account.md
Part 1: Creating a Linux Virtual Machine (VM) Instance
1. Go to "Azure Portal Dashboard"
Visit your Azure Dashboard: https://portal.azure.com
2. Create a Virtual Machine
- 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!
3. Create Ubuntu 16.04 LTS
Virtual Machine
- Search for "Ubuntu LTS"
- Select the LTS Version which is currently
16.04 LTS
- Select the VM from the Right Menu <br > 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.
4. Input the Configuration Details for your VM
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!)
5. Size: Choose Virtual Machine Size
Select the cheapest VM available and then click the "Select" button.
6. Confirm the Settings for New Instance
Confirm the settings for your new instance (leave the defaults they are fine):
Click on "OK" to confirm.
7. Instance Summary
Once you have confirmed the details, click "OK" to launch your instance.
8. Wait for the Instance to be "Provisioned"
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.
9. Login With SSH
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].
10. Setup Network Security Rule for VM
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.
11. Define Inbound HTTP 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:
12. Install & Run NGINX to Test
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
You should see: Then: Then:
13. Confirm NGINX is Serving the Default Page
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:
Congratulations Your Azure VM Instance is Working!! :-)
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
Part 2: Deploying Your Application
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!
Edit Your ~/.profile
File on Azure Instance to set TCP Port for Phoenix
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
Redirect TCP Port 80 to Port 4000 (where our app is listening)
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
.
Deploy your Phoenix Web App using EDeliver
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:
Confirm the Phoenix App is Working in a Web Browser
Visit your app by IP Address in your Web Browser. e.g: http://52.232.127.28
Part 3: Using the Azure PostgreSQL-as-a-Service Database with Phoenix
We are still pending a decision on this feature ... see: https://github.com/dwyl/learn-microsoft-azure/issues/5
<br /> <!-- ### Clustering Two VMs # "Advanced" Deployment ## Part 4: High Availability Clustering (_including WebSockest!_) ### Load Balancing + Azure **Load Balancer**: https://azure.microsoft.com/en-gb/services/load-balancer - All web traffic is handled through the load balancer which performs _continous_ health checks on the application server(s) and routes requests in a "round-robin" to balance load. > Screenshots + full walkthrough to follow. ### Monitoring > Use default Azure Monitoring. -->Background Reading
- 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/
<br /><br /><br /><br /><br /> <sup>1</sup>See: /tldr.md