Deploy a Python web app to a VM in Azure Stack Hub
You can create a VM to host your Python web app in Azure Stack Hub. In this article, you set up a server, configure the server to host your Python web app, and then deploy the app to Azure Stack Hub.
This article uses Python 3.x running Flask in a virtual environment on an Nginx server. Use Ubuntu Server 18.04 LTS from the Azure Stack Hub Marketplace.
Create a VM
Set up your VM in Azure Stack Hub by following the instructions in Deploy a Linux VM to host a web app in Azure Stack Hub. Use Ubuntu Server 18.04 LTS from the Azure Stack Hub Marketplace.
In the VM network pane, make sure that the following ports are accessible:
Port Protocol Description 80 HTTP Hypertext Transfer Protocol (HTTP) is the protocol that's used to deliver webpages from servers. Clients connect via HTTP with a DNS name or IP address. 443 HTTPS Hypertext Transfer Protocol Secure (HTTPS) is a secure version of HTTP that requires a security certificate and allows for the encrypted transmission of information. 22 SSH Secure Shell (SSH) is an encrypted network protocol for secure communications. You use this connection with an SSH client to configure the VM and deploy the app. 3389 RDP Optional. The Remote Desktop Protocol (RDP) allows a remote desktop connection to use a graphic user interface on your machine. 5000, 8000 Custom The ports that are used by the Flask web framework in development. For a production server, you route your traffic through 80 and 443. In the Overview pane, select configure under DNS name.
Select static and then name the machine so that you have a DNS name such as:
<yourmachine>.<local>.cloudapp.azurestack.contoso.com
.
Install Python
Connect to your VM by using your SSH client. For instructions, see Connect via SSH with PuTTy.
At your bash prompt on your VM, enter the following command:
sudo apt-get update sudo apt-get -y install python3 python3-dev sudo apt install python3-pip
Validate your installation. While you're still connected to your VM in your SSH session, enter the following command to open Python and note the version number. Then type
quit()
to exit the Python REPL.python3 quit()
Install Nginx, a lightweight web server. While you're still connected to your VM in your SSH session, enter the following command:
sudo apt-get -y install nginx
Install Git, a widely distributed version control and source code management (SCM) system. While you're still connected to your VM in your SSH session, enter the following command:
sudo apt-get -y install git
Deploy and run the app
Set up your Git repository on the VM. While you're still connected to your VM in your SSH session, enter the following commands:
git clone https://github.com/Azure-Samples/azure-stack-hub-flask-hello-world.git cd azure-stack-hub-flask-hello-world
While you're still connected to your VM in your SSH session, enter the following commands to install the dependencies. Install Flask using apt, and then pip to load the modules from
requirements.txt
.sudo apt install python3-flask pip3 install -r requirements.txt export FLASK_APP=application.py flask run -h 0.0.0.0
Go to your new server. You should see your running web application.
<yourmachine>.<local>.cloudapp.azurestack.contoso.com:5000
Update your server
Connect to your VM in your SSH session. Stop the server by typing Ctrl+C.
Enter the following commands:
cd azure-stack-hub-flask-hello-world git pull
Activate the virtual environment and start the app:
export FLASK_APP=application.py flask run -h 0.0.0.0
Next steps
- Learn more about how to develop for Azure Stack Hub.
- Learn about common deployments for Azure Stack Hub as IaaS.
- To learn the Python programming language and find additional resources for Python, see Python.org.