Do you want to turn your Android phone into a fully functional web server? With Termux, you can run Nginx or Apache to host a live website locally—or even make it public! This in-depth guide will walk you through setting up both web servers, configuring custom websites, and exposing them to the internet.
Why Use Termux for Web Hosting?
Portable web server – Host websites directly from your phone.
Great for testing & development – Perfect for web developers on the go.
Low-resource & efficient – Nginx and Apache run smoothly on Android.
Public or local hosting – Share with others via LAN or the internet.
Prerequisites
Before we begin, ensure you have:
Termux installed (Download from F-Droid)
A stable internet connection
Basic knowledge of Linux commands
Option 1: Hosting a Website with Nginx
Nginx is a lightweight, high-performance web server perfect for Termux.
Step 1: Install Nginx
Update packages and install Nginx:
pkg update && pkg upgrade -y
pkg install nginx -y
Step 2: Start Nginx
Run the server:
nginx
Verify it’s working:
curl http://localhost:8080
You should see the default Nginx welcome page.
Note: Nginx runs on port 8080 in Termux (Android restricts ports below 1024).
Step 3: Access Your Website Locally
Open a browser and visit:
http://localhost:8080
Step 4: Replace Default Page with a Custom Website
Navigate to Nginx’s web directory:
cd $PREFIX/share/nginx/html/
Remove the default files:
rm -rf *
Create a new index.html:
nano index.html
Paste this sample HTML:
<!DOCTYPE html>
<html>
<head>
<title>My Termux Website!</title>
</head>
<body>
<h1>🚀 Website Hosted on Termux!</h1>
<p>Powered by <b>Nginx</b></p>
</body>
</html>
Save with Ctrl+O → Enter → Ctrl+X.
Restart Nginx:
nginx -s reload
Refresh http://localhost:8080 to see your site!
Option 2: Hosting a Website with Apache
Apache is another powerful web server with .htaccess support and modularity.
Step 1: Install Apache
Run:
pkg install apache2 -y
Step 2: Start Apache
apachectl start
Verify it’s running:
curl http://localhost:8080
You should see the Apache default page.
Note: Apache also runs on port 8080 in Termux.
Step 3: Access Your Website Locally
Visit in a browser:
http://localhost:8080
Step 4: Customize Your Website
Navigate to Apache’s web directory:
cd $PREFIX/share/apache2/default-site/htdocs/
Remove default files:
rm -rf *
Create a new index.html:
nano index.html
Paste this sample HTML:
<!DOCTYPE html>
<html>
<head>
<title>Apache on Termux!</title>
</head>
<body>
<h1>🔥 Website Hosted on Termux!</h1>
<p>Powered by <b>Apache</b></p>
</body>
</html>
Save (Ctrl+O → Enter → Ctrl+X).
Restart Apache:
apachectl restart
Refresh http://localhost:8080 to see your new site!
Making Your Website Public
Want others to access your site? Here’s how:
Option 1: Local Network (LAN) Access
Find your phone’s local IP:
ifconfig
Look for wlan0 (Wi-Fi) and note the inet address (e.g., 192.168.x.x).
Others on the same network can visit:
http://[YOUR_LOCAL_IP]:8080
Option 2: Public Access with Ngrok (Best for Testing)
Install Ngrok:
pkg install wget -y
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
unzip ngrok-stable-linux-arm.zip
Run Ngrok (requires Ngrok account):
./ngrok http 8080
You’ll get a public URL like https://xyz.ngrok.io.
Now, your website is accessible worldwide!
Stopping the Servers
When done, stop the servers:
For Nginx:
nginx -s stop
For Apache:
apachectl stop
Nginx vs. Apache in Termux: Which is Better?
FeatureNginxApachePerformanceFaster, lower memory usageSlightly heavier, more featuresConfigSimpler configurationSupports .htaccessUse CaseHigh-traffic, static sitesDynamic sites, PHP apps
Recommendation:
Use Nginx for speed and simplicity.
Use Apache if you need .htaccess or PHP support.
Bonus: Running PHP with Apache
Want to host PHP websites? Install PHP:
pkg install php -y
Then, restart Apache:
apachectl restart
Now, you can run PHP scripts in htdocs/!
Final Thoughts
You’ve now learned how to:
Host a website in Termux using Nginx & Apache
Customize your site with HTML
Access it locally or publicly via Ngrok
Try it out and turn your Android into a web server today!
Follow Codelivly for more Termux & Cybersecurity guides!
No Responses