43. Setting up a Local Web Server (Using Python's Built-in Server)
One of the simplest and most common tasks in Termux is serving local files (HTML, images, scripts) via HTTP, often used for quick testing or sharing.
Prerequisites
Ensure you have Python installed (pkg install python).
Serving Files with http.server
Python 3 includes a module called http.server that can turn any directory into a basic web server instantly.
-
Navigate to the Directory: Go to the folder containing the files you want to serve (e.g., a folder containing
index.html).bash $ mkdir website_test $ cd website_test $ echo "
Termux Web Server Running!
" > index.html -
Start the Server: Execute the Python module command, specifying the port (e.g., 8080).
bash $ python -m http.server 8080 Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
Accessing the Server
Once the server is running, you can access it in two ways:
- On the same device: Open your Android web browser and go to
http://localhost:8080. - From another device on the same Wi-Fi network: Find your Termux device's local IP address (e.g., 192.168.1.10) and navigate to
http://[Your IP]:8080.
To stop the server, switch back to the Termux window and press Ctrl + C.