Here are the steps to create a web server with tiangolo/nginx-rtmp module and Python, where RTMP is served on port 1935 and Python on port 80:

  1. Install Docker on your server.
  2. Create a new directory to store your Dockerfile and other configuration files.
  3. Create a new file called “Dockerfile” in this directory and paste the following code into it:
FROM tiangolo/nginx-rtmp

# Install Python and pip
RUN apt-get update && \
    apt-get install -y python3 python3-pip && \
    rm -rf /var/lib/apt/lists/*

# Install Flask, a Python web framework
RUN pip3 install Flask

# Copy your Python script to the container
COPY app.py /app.py

# Expose ports 80 and 1935
EXPOSE 80
EXPOSE 1935

# Start Nginx and your Python script
CMD service nginx start && python3 /app.py

4. Create a new file called “nginx.conf” in the same directory and paste the following code into it:

worker_processes 1;

events {
  worker_connections 1024;
}

rtmp {
  server {
    listen 1935;
    chunk_size 4096;

    application live {
      live on;
      record off;
    }
  }
}

http {
  server {
    listen 80;

    location / {
      # Serve static files
      root /usr/share/nginx/html/;
      index index.html index.htm;
    }

    location /api {
      # Pass requests to your Python script
      proxy_pass http://localhost:5000;
    }
  }
}

This configuration file sets up Nginx to serve RTMP on port 1935 and a static website on port 80. Requests to the “/api” endpoint will be passed to your Python script, which will be running on port 5000.

5. Create a new file called “app.py” in the same directory and paste the following code into it:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

This is a simple Python script that starts a Flask web server and serves a “Hello, World!” message on the root endpoint.

6. Build the Docker image by running the following command in your terminal:

docker build -t my-web-server .

This will build a Docker image with the name “my-web-server” using the Dockerfile and configuration files in your current directory.

7. Run the Docker container by running the following command in your terminal:

docker run -p 80:80 -p 1935:1935 my-web-server

This will start the Docker container and map ports 80 and 1935 on your server to the corresponding ports in the container.

You should now be able to access your static website at http://localhost:80 and your RTMP server at rtmp://localhost:1935/live. You can also make requests to your Python script at http://localhost/api.

Thank you for reading

Leave a Reply

Your email address will not be published. Required fields are marked *