This lesson will introduce you to a very important topic to Microsoft Azure cloud services. Azure Virtual Machine offers IaaS with Linux operating systems. The use of key pairs or creating virtual instances is slightly different compared to other clouds e.g. Amazon EC2 or OpenStack Nova. In this lesson, we focus on a simple tutorial on running a Python Flask Web Framework on Azure Virtual Machine. We expect that readers gain basic knowledge of using Azure cloud services and have some experience of building services on the cloud.
Tip
Duration: 50 minutes
In order to conduct this lesson you should have:
We will create a VM instance on Azure cloud, and start a Flask Web Framework on the Ubuntu 14.04 machine image. Our conditions are:
Microsoft Azure provides web management tool, Azure Management Portal to control Azure cloud services. We will use this web interface to create, list or delete a VM instance.
Ubuntu
tab.tutorial-azure
,
you may have dns name, tutorial-azure.cloudapp.net
.[unique
vm name].cloudapp.net
. Unque name means that there is no duplication
across entire Azure cloud services.provide password
and disable ssh key pair authentication
azureuser
as a user name.Once you successfully launched your instance, it will be available in a few moments.
[your vm name].cloudapps.net
.Use your ssh client tool to login to the virtual server.
azureuser
as a user nameroot
or create a new user.Since we used password authentication, we need to switch it to SSH key pair
authentication. This requires a few steps with root
.
puttygen.exe
authorized_keys
file which holds a list of public key strings.
If you register your public key in this file, you will be able to login to
this machine using a pair, your private key.echo [your public key string] >> $HOME/.ssh/authorized_keys
/etc/ssh/sshd_config
with your editor e.g. nano, emacs, or vi.sudo vi /etc/ssh/sshd_config
PasswordAuthentication yes
to PasswordAuthentication no
and save the file.sudo service ssh restart
It’s time to install and run Flask Web Framework. It is a minimal software to
run a web server using Python. We will try to use a sample code hello.py
from the Flask official site.
We assume that you know how to use some basic Linux commands, editors, and Python. We will use the following commands 1) sudo, 2) su, 3) apt-get, and 4) service, 5) python to install and run Flask.
sudo apt-get update
sudo apt-get install python-pip
pip
.sudo apt-get install virtualenv
virtualenv
.virtualenv $HOME/FLASK
FLASK
environment for Pythonsource $HOME/FLASK/bin/activate
FLASK
environment.Write a python script in hello.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
python hello.py
* Running on http://localhost:5000/
,
you successfully run your Flask on the localhost with 5000 port number.We will make a small change to provide Flask in public.
This way, anyone on the internet can see your Hello World!
message.
root
account to use system port 80.sudo su -
root
account. Try pwd
command to confirm that youare in
/root
azureuser
home directory and enable virtualenvcd /home/azureuser
source /home/azureuser/FLASK/bin/activate
FLASK
environment.app.run()
function to app.run(host='0.0.0.0', port=80)
0.0.0.0
is a way to specify any IPv4-host at all. You Flask will
provide web service through your internal/external network of your virtual
server.port=80
is a way to tell Flask that you are using HTTP default port.Hello World!
Page on the Web¶Hello World!
page, you have now working Flask Web Framework
on Azure.If you completed your jobs on Azure cloud, you need to shutdown all of your
cloud resources and return the lease. There is delete Cloud Service
command or menu. If you do not terminate your resources, you will receive a
charge of using cloud services e.g. Virtual Machine, Cloud Service, or Storage.
Additional (Optional) Study Material