Apache2 Termux : Install and Use Apache2 Server in Termux

The World Wide Web is built using Servers and with web Servers, The Internet became accessible for everyone, so in this post I am going to show you how you can create your own Apache Webserver in Termux. You will learn how to install Apache2 in Termux as well as I will show you how you can Practice HTML using it👾.


What is Apache2 in Termux?

The Apcahe2 web server is an Open source software that helps you to create and run your webserver on almost any device. The Real name of this software is apache HTTP Server. Almost 40% of websites on the internet are still running using the Apache webserver.

Apache2 Termux : Install and Use Apache2 Server in Termux

You can easily run apache on Linux and since termux is a Linux Terminal emulator we can use it to Run Apache webserver. The installation and use are very simple on a basic level. You can create your own Websites using HTML on Termux and you can run Them on your Temux apache2. By default, this Server will run on your localhost at port 8080 but you can also use port forwarding to make it live.

If you like to visit the official apache site then here is the link: apache.org


Install Apache2 Server In Termux :

apache2 is a basic package in termux and To install apache2 in the Termux app you just have to use the basic pkg command. The Archive file of the Apache2 server in termux is around  5.5MB so it won't take much time to install. Type Below command to install.
pkg install apache2
Apache2 Termux : Install and Use Apache2 Server in Termux

Run Apache2 Server In Termux :


Step 1: 

Everything in the apache2 termux server will come auto set and for testing the server you just have to type the below command. This command will run the apache server, So any time you wanna re-run your Termux-Apache Server then you will use the below command. 
apachectl
Apache2 Termux : Install and Use Apache2 Server in Termux




Step 2: 

Now open your Chrome Browser and type localhost:8080, if you see "It works!" on your screen that means the Termux Apahe2 server is Running Properly.
localhost:8080


Stop Apache2 Server In Termux :

To stop the Apache2 server you just have to use a Single one-line command in termux. After entering this command if you will refresh your webpage in chrome you will see "This site can't be reached" so if you see this anytime that means your server is stopped.
apachectl -k stop
Apache2 Termux : Install and Use Apache2 Server in Termux




 

Edit HTML files in Apache2 Termux Webserver :

Now, the webserver is Up and running you should know that the HTML file we are getting on the local host is actually present in your Termux and you can edit it to Learn Html in your termux.  First of all, you need to learn how you can edit this webpage and print your own name instead of its works. 

Step 1:

All the apcahe2 webservers files are stored in your usr/share/apache2/default-site/htdocs folder in termux. and you will have to first move into that folder to edit the Index.html file present there. use the below command to move into the htdocs path.
cd /$PREFIX/share/apache2/default-site/htdocs
Apache2 Termux : Install and Use Apache2 Server in Termux

Type ls and you will be able to see that index.html file in the htdocs folder.

Apache2 Termux : Install and Use Apache2 Server in Termux





Step 2:

Now if you already have any Text Editor in your Termux then you can skip this part but if you don't, then you need to install a text editor, nano is a good text editor and it is lightweight too. use the below command to install nano in your termux.
pkg install nano
Apache2 Termux : Install and Use Apache2 Server in Termux


Step 3:

Now type the below command and you will see the apache2 default HTML webpage's Source code.
nano index.html
Apache2 Termux : Install and Use Apache2 Server in Termux


Step 4:


If you know basic HTML coding then you can edit this page or you can just change it works! text with your name, In Termux you cannot move the cursor without the extra keys so enable that extra keys in Termux you will have to read this post [Enable Extra Keys in Termux with a Single Command].

Apache2 Termux : Install and Use Apache2 Server in Termux

Now Edit this page and then press CTRL+S to Save and CTRL+X to close the nano editor.





Step 5:

Open Chrome ones again and refresh the already opened localhost page, if you don't see your localhost page then type localhost:8080 in chrome and you will the page. if you still facing any error try running the server again by typing apachectl.

Apache2 Termux : Install and Use Apache2 Server in Termux

If you do everything right then you will see your name in chrome like in the above picture.


Add Your Own Webpage in Termux Apache2 Webserver :

Now This part is really Interesting, If you understand this part, you will be able to create a full website with multiple articles and pages in your termux. 


Step 1:

To add your own webpage, you have to create a new HTML file in the htdocs folder. This file will be another page on your website. So to create the new HTML file you just have to use nano command.
nano learntermux.html
Apache2 Termux : Install and Use Apache2 Server in Termux

You need to change the green part with your page name.



Step 2:

Now start Writing Html code here, If you don't know HTML and wanna learn it there is no website better than W3School.  Below you can also see my HTML code.

Apache2 Termux : Install and Use Apache2 Server in Termux

Make sure your press CTRL + S to save your code and CTRL + X to close the nano editor.



Step 3:

Now, we have a new page on our server and to open that page you have to type localhost followed by your new filename.html. 

localhost:8080/learntermux.html

Apache2 Termux : Install and Use Apache2 Server in Termux



Tip:

You can create as many pages as you want and you can link them to all using anchor tags, You can even use your index.html to index every link on your server. If You get any error then try restarting Your server. Read This Post To Make Your Server Accessible like any other Website.



Conclusion :

Apache2 is an Awesome software to make a webserver on your Termux. Apache2-termux is your own web server so you can do anything you want. you can practice the HTML code here.You can Even Make Your Server Live using Ngrok Try to make multiples webpages and then link them in an index where you can choose your pages, make sure you link them back with the index so that you can click the link to go back on the index page. If you are serious about learning then this is the best tool to learn about servers and web pages. If you need any help regarding this then you can always reach out to me. If you wanna know about Encryption then you can Read this Post [ Encrypt and decrypt Files in Termux ]. Thanks for reading Guys, Stay inspired and as always Stay Ethical👾.

Post a Comment

3 Comments

  1. Bro termux space(15gb) bhahut le Raha clear data Karne par Sara kuch delete ho ja raha koi tool or app batao jishase backup le sake

    ReplyDelete
    Replies
    1. "du -h" lekho terminal mai or dekho kya jyadha space use kar raha hai, Kam ka nhi hai toh delete kardo

      Delete