dwyl / learn-microsoft-azure

:cloud: Learn how to deploy your Web Application & Database Server to Microsoft Azure

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Learn Microsoft Azure

windows-azure-cloud

Learn how to deploy your Web Application & Database Server to Microsoft Azure.

Why?

The decision was made to deploy your work/app to Azure1, 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.
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

azure-no-vms

  1. Click on the "Burger" (☰) Menu to expand the service menu
  2. Click on "Virtual Machines" in the services menu
  3. 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

azure-create-ubuntu-16 04-lts

  1. Search for "Ubuntu LTS"
  2. Select the LTS Version which is currently 16.04 LTS
  3. 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.
  4. Click "Create" button to start the creation process.

azure-create-button

4. Input the Configuration Details for your VM

You should then see a screen similar to this:

azure-create-vm-details

  1. 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...)
  2. VM disk type: SSD (keep the default)
  3. User name: azure (they don't let you have the username root even though which ever username you do pick here has root priviledges!... I use azure so it's clear which platform my VM is hosted by/on.)
  4. Authentication Type: SSH public key (obviously more secure than password)
  5. 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
  1. Subscription: Free Trial (obviously)
  2. Resource Group: [x] Create new. Called it phoenix-cluster
  3. Location: West Europe (pick what ever is closest to your end-users)
  4. Click "OK" (finally!)

5. Size: Choose Virtual Machine Size

azure-create-vm-2

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):

azure-leave-default-settings

Click on "OK" to confirm.

nothing.else.is.available@outlook.com

7. Instance Summary

azure-create-vm-4

Once you have confirmed the details, click "OK" to launch your instance.

8. Wait for the Instance to be "Provisioned"

Now Wait ...

azure-instance-creating

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

ssh into Azure instance

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:

azure-vm-dashboard

Once you are viewing the details for your VM:

azure-click-network-interface-then-interface

  1. Click on "Network Interfaces"
  2. Click on the name of the interface for you instance in our case: phoenix-prod-1594
  3. Click on "Network Security Group"
  4. Click on the the name of your Security Group in our case "phoenix-prod-1-nsg"

azure-click-network-security-group

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.

azure-create-inbound-rule

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:

azure-add-inbound-rule-http

  1. Click on the "+ Add" button
  2. Select HTTP from the list of services (option "drop-down" list)
  3. Click "OK" button to create the new rule.

You should now see the rule in the Inbound security rules list:

azure-http-rule-added-success

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: nginx-install Then: nginx-install Then: nginx-start-status

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:

nginx-working-on-azure

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

azure-define-port

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

azure-port-redirect-80-to-4000

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:

deploy-app-to-azure

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

phoenix-app-working-on-azure

Part 3: Using the Azure PostgreSQL-as-a-Service Database with Phoenix

We are still pending a decision on this feature ... see: #5


Background Reading






1See: /tldr.md

About

:cloud: Learn how to deploy your Web Application & Database Server to Microsoft Azure