Codelivly

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

  1. Navigate to Nginxโ€™s web directory:
   cd $PREFIX/share/nginx/html/
  1. Remove the default files:
   rm -rf *
  1. 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.

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

  1. Navigate to Apacheโ€™s web directory:
   cd $PREFIX/share/apache2/default-site/htdocs/
  1. Remove default files:
   rm -rf *
  1. 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).

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

  1. Find your phoneโ€™s local IP:
   ifconfig

Look for wlan0 (Wi-Fi) and note the inet address (e.g., 192.168.x.x).

  1. Others on the same network can visit:
   http://[YOUR_LOCAL_IP]:8080

Option 2: Public Access with Ngrok (Best for Testing)

  1. Install Ngrok:
   pkg install wget -y
   wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip
   unzip ngrok-stable-linux-arm.zip
  1. 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?

FeatureNginxApache
PerformanceFaster, lower memory usageSlightly heavier, more features
ConfigSimpler configurationSupports .htaccess
Use 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!

Our Latest Update