Termux from AppInventor App

INTRO

What is Termux?

Best to have a read of the Termux Wiki - have a good read around to get a feeling for the app.

Why would we want to use Termux from our App Inventor App?

How will we connect to Termux?

With help from others, we attempted to connect using the details provided through the Termux wiki, using Activity Starter, but to date, this has not born fruit. Instead I have used a method of installing an apache2 php server on Termux, and used this to send commands to termux using a web component in the app. This takes a bit of setting up, as you will see, and is not without its foibles.....

Any Extensions required ?

This method is free of extensions.

Any issues?

SETUP Termux

Get the latest Termux app from here ( file size is @ 85mb)

Download the apk and install it on your device


Open Termux and run the following commands, to update all packages and install apache2 and php:

pkg upgrade

pkg install php-apache

Some configuration is now required...

First we edit the httpd.conf file:

nano /data/data/com.termux/files/usr/etc/apache2/httpd.conf

scroll down about 70 lines in this file until you find:

# Example:

LoadModule foo_module modules/mod_foo.so

Enter a new line below and add the following:

LoadModule php_module libexec/apache2/libphp.so

<FilesMatch \.php$>

  SetHandler application/x-httpd-php

</FilesMatch>

Find:

#LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so

and uncomment it to look like this:

LoadModule mpm_prefork_module libexec/apache2/mod_mpm_prefork.so

Find:

LoadModule mpm_worker_module libexec/apache2/mod_mpm_worker.so

and comment it to look like this:

#LoadModule mpm_worker_module libexec/apache2/mod_mpm_worker.so

Finally, scroll down a bit more until you find:

<IfModule dir_module>

and change:

DirectoryIndex index.html

to

DirectoryIndex index.php


Now save and exit the httpd.conf file. You do this by pressing the CTRL button then typing x then

We now need to create a test file to ensure that everything is working, create the file index.php as follows:

nano /data/data/com.termux/files/user/share/apache2/default-site/htdocs/index.php

add this to the file

<?php

phpinfo();

?>

Save and exit the index.php file. You do this by pressing the CTRL button then typing x then

Now we need to start apache, enter the following on the command line:

apachectl start

Now open your browser on your device and go to:

127.0.0.1:8080/index.php  (or localhost:8080/index.php)

If all is well you should see lots of information in a table about your php installation (something like this):

We can now install some of the packages/programs you may need:

pkg install wget curl sqlite openssh

We also need to install a special termux package, that provides access to the shared storage on the device:

termux-setup-storage

You have to give permission to Termux to access your files


This gives a file path of:

/data/data/com.termux/files/home/storage/shared/

which is the same as

/storage/emulated/0/

on the App Inventor side


Nearly done....we now need to create the command.php file that will send linux commands to termux from the php server from the app.

nano /data/data/com.termux/files/user/share/apache2/default-site/htdocs/command.php

add the following:

<?php  


$output = shell_exec($_GET["command"]." 2>&1");

echo $output;


?>

Save and exit the command.php file. You do this by pressing the CTRL button then typing x then


You are done!

(please ensure you read the Notes section below)

SETUP APP

In comparison, the AppInventor app is fairly simple. We use an activity starter in order to be able to start Termux from within the app, and a web component to send the command calls to the termux php server. Buttons and labels for interaction and responses. A textbox is provided for command input. This is the very basic version, developers can create their own methods for interacting with the php server, and create other php files on the server to receive commands.

BLOCKS

NOTES