Run a Live Website in Termux Using Nginx! 🚀

Run a Live Website in Termux Using Nginx!

Introduction

Host a highly customizable web server on Termux. The web is fueled by servers, and Nginx is one of the most powerful and lightweight options out there! Whether you're testing websites or hosting your projects locally, you can create a fully functional Nginx web server in Termux. In this post, I’ll guide you through installing, running, and making your website live in Termux using Piggy for port forwarding. Let's get started!

What is Nginx?

Nginx (pronounced Engine-X) is an open-source web server software designed for high performance and scalability. It’s widely used for hosting websites, acting as a reverse proxy, load balancing, and more. If you’re experimenting with servers in Termux, Nginx is a must-have tool.

With Termux + Nginx, you can:

  • Host your own website locally.
  • Learn and practice web development.
  • Make your site live using port forwarding.

Install Nginx in Termux

Installing Nginx in Termux is super simple. It’s available in the Termux package repository, and you can get it with one command:

pkg install nginx && pkg install termux-services

Once installed, the Nginx server files will be available in the /data/data/com.termux/files/usr/share/nginx/ directory. The termux-services package allows us to manage Nginx services with the sv command.

Run Nginx in Termux

Step 1: Start the Nginx Service

Type the following command to start the Nginx server:

sv up nginx

This will start the Nginx service and make it accessible on your localhost:8080.

Step 2: Check If Nginx Is Running

Open your browser and type:

http://localhost:8080

You should see the default Nginx welcome page that says "Welcome to Nginx."

Step 3: Stop the Nginx Service

If you want to stop your Nginx server, you can use this command:

sv down nginx

Stopping the service will make your server unavailable until you restart it.

Edit Nginx Web Pages

The default HTML page served by Nginx is located in:

/data/data/com.termux/files/usr/share/nginx/html

Step 1: Navigate to the Nginx Directory

cd $PREFIX/share/nginx/html

Step 2: Open the HTML File

Install a text editor like Nano if you don’t have it already:

pkg install nano

Now, open the file:

nano index.html

Edit the contents of index.html as per your requirements. Save the file by pressing CTRL+O, then ENTER, and exit with CTRL+X.

Step 3: Restart Nginx

After making changes to your HTML file, restart the Nginx service to reflect the updates:

sv restart nginx

Making Your Website Live

By default, Nginx will only be accessible locally on your Android device. To make it live and accessible from anywhere, you'll need to use a port forwarding service like Ngrok or PageKite.

Step 1: Install Ngrok

pkg install wget && wget https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-stable-linux-arm.zip && unzip ngrok-stable-linux-arm.zip

Step 2: Authenticate Ngrok

Sign up at ngrok.com, copy your authentication token, and run:

./ngrok authtoken YOUR_AUTH_TOKEN

Step 3: Start Port Forwarding

To make your site live, forward port 8080:

./ngrok http 8080

Ngrok will generate a public URL that you can share with others to access your website.

Conclusion

And that's it! You now have a fully functioning web server running on Termux using Nginx. Whether you want to test your web development projects or host a small website, this setup provides a robust and portable solution. If you found this guide helpful, don’t forget to share it!

Post a Comment

0 Comments