Blog Post

Apps on Azure Blog
4 MIN READ

Azure Linux Web App and http server

theringe's avatar
theringe
Icon for Microsoft rankMicrosoft
Aug 21, 2024

The advantage of using open-source solutions is that you can access many up-to-date features (e.g., using Python and related packages to operate big data models). However, the downside is that sometimes you need to understand the system architecture a bit, not just the code itself. This article will introduce how to perform initial port configuration on an Azure Linux Web App and how to modify default values.

 

TOC

HTTP Server and Startup Script

backend example: python

frontend example: node

 

HTTP Server and Startup Script

Azure Web App (App Service) exposes ports 80/443, with 443 being the encrypted port commonly used. This means that regardless of which port your locally developed application listens to, you need to perform port forwarding when deploying to the Web App.

 

A typical Windows Web App has an IIS built-in as the HTTP server, listening on port 80 by default. Regardless of the stack used (e.g., node, php, .net), IIS serves as the HTTP server. Therefore, since the web app has pre-configured forwarding rules on the platform side, users are not aware of the HTTP server and port forwarding during the operation.

 

However, the situation is different for Linux Web Apps. Different stacks have different HTTP servers (e.g., node can use pm2, php can use nginx, python can use uvicorn, etc.). The same HTTP server can also be used for multiple stacks (e.g., nginx can be used for php or python). Some languages can even start a simple server for port listening through an interpreter and corresponding modules (e.g., python with http.server). This makes it important to understand which HTTP server your application is running on.

 

You might recall that in different development environments, you need to enter some commands in the console to start the app before testing an application, such as:

uvicorn app:test --reload
npm start

 

This is because different tutorials usually use the corresponding HTTP server (or tools with HTTP server functionality) as a prerequisite for starting. Since these tools are constantly updated, the command line instructions or suffix parameters vary greatly. Therefore, a Startup Command feature is needed in Azure Linux Web App to manage these commands.

 

In the following sections, I will provide two examples and describe how to deploy your application to Azure Linux Web App and run it successfully under different stacks and HTTP servers.

 

 

backend example: python

For a Linux Python app, this example uses Python's http.server package as the HTTP server. The command I use in the local development environment is `python server.py`.

import os
import http.server
import socketserver
from http import HTTPStatus

class Handler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.send_response(HTTPStatus.OK)
        self.end_headers()
        msg = 'scale %s' % (self.path)
        self.wfile.write(msg.encode())

port = 8000
print('Listening on port %s' % (port))
httpd = socketserver.TCPServer(('', port), Handler)
httpd.serve_forever()

 

After confirming the HTTP server and command, you can create a Linux python web app and deploy the code directly.

 

Remember the port mapping mentioned earlier? Since the python environment provided by Azure Web App forwards port 8000 to the exposed port 443 by default, for this code, you don't need to set additional port forwarding rules after deployment. Just modify the startup command.

 

Before actual testing, remember to restart the app.

 

If your application needs to change the default listening port, for example, to 7999, in this case, besides modifying the corresponding port name in the code, you also need to add `WEBSITES_PORT=7999` in the App Setting to override the default port forwarding rules.

 

To confirm that your application is indeed listening on port 7999, you can go to the Kudu site and run the command:

netstat -tulpn | grep LISTEN


 

frontend example: node

For a Linux node app, this example uses pm2 as the HTTP server. The command I use in the local development environment is `npm start`.

npx create-react-app reactfailreact-app
cd  reactfailreact-app
npm start

 

Azure Linux node Web App has a built-in pm2, a lightweight process manager with an HTTP server, suitable for experimentation. You can create a Linux node web app, compile the code, and deploy it.

 

Since the node environment provided by Azure Web App forwards port 3000 to the exposed port 443 by default, for this code, you don't need to set additional port forwarding rules after deployment. Just modify the startup command.

 

Run the following command to compile the project. After execution, a build subfolder will be created.

npm run build

 

Create or modify `build/.deployment` with the following content:

[config]
SCM_DO_BUILD_DURING_DEPLOYMENT=false

 

Publish your project, specifying the build subfolder during the publishing process.

 

Go back to the app, navigate to Configuration, and specify the Startup Command as follows:

pm2 serve /home/site/wwwroot --no-daemon --spa 

 

After restarting the web app, you can visit your webpage using a browser.

 

If your application needs to change the default listening port, for example, to 2999, in this case, besides specifying the listening port in the startup command, you also need to add `WEBSITES_PORT=2999` in the App Setting to override the default port forwarding rules.

pm2 serve /home/site/wwwroot --no-daemon --spa --port 2999

 

You can also go to the Kudu site and run the command:

netstat -tulpn | grep LISTEN

 

Published Aug 21, 2024
Version 1.0
No CommentsBe the first to comment