Azure Linux Web App and http server

  • Thread starter Thread starter theringe
  • Start date Start date
T

theringe

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:


Code:
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`.


theringe_0-1724229492047.png

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

theringe_1-1724229519483.png

theringe_2-1724229519484.png



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

theringe_3-1724229580444.png



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.

theringe_4-1724229627848.png



Before actual testing, remember to restart the app.

theringe_5-1724229646470.png

theringe_6-1724229646470.png



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.

theringe_7-1724229767325.png

theringe_8-1724229767326.png



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

theringe_9-1724229790298.png

theringe_10-1724229821918.png
theringe_11-1724229821918.png



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`.


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

theringe_12-1724229898388.png

theringe_13-1724229898389.png

theringe_14-1724229898390.png



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.

theringe_15-1724229976438.png



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

theringe_16-1724230044753.png



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

Code:
[config]
SCM_DO_BUILD_DURING_DEPLOYMENT=false

theringe_17-1724230092793.png



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

theringe_18-1724230116680.png

theringe_19-1724230116681.png

theringe_20-1724230116682.png



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

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

theringe_24-1724230216195.png



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

theringe_25-1724230233523.png



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

theringe_26-1724230337515.png

Continue reading...
 
Back
Top