[Server] Android phones use Termux to build web services

5a2585dded9b416fb4ea58637b42ed39.png

Yan-yingjie’s homepage

Awareness of the past is not admonishment, but knowledge of the future can be traced

C++ Programmer, 2024 Electronic Information Graduate

Directory

overview

1. Build apache

2. Install cpolar intranet penetration

3. Public network access configuration

4. Fixed public network address

5. Add a site


Overview

Termux is an Android terminal emulation application, which is used to build a complete Linux environment on Android mobile phones. It can realize many basic operations under Linux, and Termux can run normally without root permissions.

Apache is an open source web server software that is widely used due to its cross-platform and security, and is one of the most popular web server software. We can use Termux to build a web server on an Android phone, and at the same time do intranet penetration, so that users on the public network can also access it, turning Android into a small cloud server.

The following describes how to build an apache service on Android Termux to create a personal site and combine it with the cpolar intranet penetration tool to achieve public network access.

1. Build apache

Execute the command to install apache, the installation is relatively simple, one-click installation

pkg install apache2

Then start apache

apachectl start

When starting up, if the following problems occur

image-20230510150038202

The solution is to modify the configuration file. If vim is not installed, execute the command to install it

pkg install vim

Edit and modify the configuration file

vim $PREFIX/etc/apache2/httpd.conf

Find ServerName to untie the comment, change www.example.com to 127.0.0.1:8080, you can change the port number to your favorite

image-20230510151133736

Start again after modification, you can

image-20230510151344686

Open the browser, enter http://127.0.0.1:8080, you can see the apahe welcome page,

image-20230510151540948

stop apache

apachectl stop

restart apache

apachectl restart

2. Install cpolar intranet penetration

cpolar official website: https://www.cpolar.com

After successfully creating an apache container running the site, we then use cpolar to penetrate the internal network to achieve access in the public network environment.

cpolar is a secure intranet penetration cloud service, supports http/https/tcp protocols, can be used permanently for free without restricting traffic, and supports port 80/443 mapping. It can expose local services to the public network by creating a secure tunnel, so that public network users can also access intranet services normally, without the need for a public network IP or setting up a router.

Create a sources.list.d folder:

mkdir -p $PREFIX/etc/apt/sources.list.d

Add cpolar to download source files

echo "deb [trusted=yes] http://termux.cpolar.com termux extras" >> $PREFIX/etc/apt/sources.list.d/cpolar.list

update warehouse

pkg update

install cpolar

pkg install cpolar

Install termux service

Note: After the installation is complete, remember to close and restart termux to take effect!!

pkg install termux-services

After restarting termux, then start cpolar

sv up cpolar

Set up autostart

sv-enable cpolar

This is to stop the cpola service

sv down cpolar

cpolar.yml main configuration file path location

$PREFIX/etc/cpolar/cpolar.yml

Then enter http://localhost:9200 in the mobile browser to see the cpolar management interface, and log in with the email account registered on the cpolar official website

image-20230509155942139

3. Public network access configuration

The mobile browser opens the cpolar web ui management interface, we click the tunnel management on the left dashboard – create a tunnel, because we modify the configuration file on apache to port 8080 (the article is 8080, the specific port number is Your own port prevails), so we need to create an http tunnel pointing to port 8080:

  • Tunnel name: customizable, be careful not to repeat

  • protocol: http

  • Local address: 8080

  • Domain Type: Choose a random domain name

  • Region: Select China VIP

Click Create

image-20230510153916787

After the creation is successful, open the online tunnel list, and you can see the address accessed by the public network. There are two access methods, one is http, the other is https

image-20230510155122769

We use the http method, enter the public network address in the browser, and the access is successful

image-20230510163512036

4. Fixed public network address

Since the above is a free random tunnel, the generated public network address will change randomly within 24 hours. In order to facilitate long-term and stable connection, we can fix this public network address and configure a fixed second-level subdomain name of cpolar [no filing required] , or you can configure to use your own domain name to access, here we take configuring a fixed second-level subdomain name as an example.

You need to upgrade to the basic package or above to support the configuration of second-level subdomains

Log in to the background of the cpolar official website, click Reserved on the left dashboard, find Reserve the second-level subdomain name, and reserve a second-level subdomain name for the http tunnel.

  • Region: select server region

  • Name: Fill in the second-level subdomain name you want to reserve (customizable)

  • Description: Remarks, which can be customized

image-20230510171738839

In this example, a second-level subdomain named mywebsitetest is reserved. After the subdomain name is successfully reserved, we copy the subdomain name, and then we need to configure it in the tunnel.

image-20230510171806128

Log in to the cpolar web ui management interface, click Tunnel Management on the left dashboard–Tunnel List, find the tunnel that needs to be configured with a second-level subdomain name, and click edit

image-20230510171929758

Modify the tunnel information and configure the second-level subdomain name into the tunnel:

  • Domain name type: choose secondary subdomain name instead

  • Sub Domain: Fill in the second-level subdomain we just reserved (in this case mywebsitetest)

After modification, click Update

image-20230510172124742

After the tunnel is successfully updated, click Status on the left dashboard–Online Tunnel List, and you can see the public network address of the tunnel, which has been updated to a second-level subdomain.

image-20230510172216408

After fixing, we use the fixed public network address, open it in the browser, and you can see the apache default page, so that a fixed public network address access is set up

image-20230510172352811

5. Add a site

The page we visited above is the default page of apache, not the page we want. We can put the page we like on it and enter the apache htdocs folder

cd $PREFIX/share/apache2/default-site/htdocs

Install the download tool wget

pkg install wget

To demonstrate here, we download a test site

wget https://www.cpolar.com/static/downloads/meditation-app-master.tar.gz

Unzip after downloading

tar xzf meditation-app-master.tar.gz

Then we enter our public network address above plus the resource path /meditation-app-master/index.html on the browser, and we can see our personal site.

Now as long as the tunnel is kept online, public network users can access the web site we built on termux through this fixed public network address.