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?
Because we can ;)
Juan Antonio's Terminal extension has shown us some of the benefits of being able to access linux commands on our devices, and how to run them from App Inventor. My investigations in using this have been many, but I have found that newer devices do not install some of the packages/programs that we may want to use, specifically: curl, sqlite, ssh, sftp, although there are many others. Termux provides us with a much broader base to work with, and a much fuller linux working environment.
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?
Termux is no longer available on Google Play, which means you will need to sideload Termux onto your device, and should not mix any Google Play termux installations with any FDroid termux installations.
Termux has a few quirks on Android => 10
You need to be comfortable at the command line (preferably the linux command line) in order to get the best out of this method
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 y
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 y
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:
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 y
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
The activityStarter starts Termux in the foreground, you will need to press the device Back Button once (or twice) in order to return to your app. The termux session will keep running in the background. In order to preserve your history file in termux, it is best to close Termux by typing exit, as opposed to using the notification option.
If running termux/your app compiled or via companion over wifi, you can test the http server from your computer browser. Check the ip address of you device on startup of the companion app, then use this:
http://<IP ADDR>:8080/index.php
http://<IP ADDR>:8080/command.php?command=ls
There are a couple of environment variables for filepaths you can use:
${HOME} or $HOME which is the same as /data/data/com.termux/files/home
${PREFIX} or $PREFIX which is the same as /data/data/com.termux/files/usr
I also created another one for the equivalent of the html directory by typing this at the command line:
HTML=/data/data/com.termux/files/user/share/apache2/default-site/htdocs
which equates to:
${HTML} or $HTML
PHP Server not running/corrupted
There may be occasions when for some reason the httpd.pid file becomes corrupted, and this means the php server cannot start and run your commands. You can see this in termux if you run:
apachectl restart
and this generates an error similar to:
AH00058 : Error retrieving pid file /var/run/apache2/httpd.pidYou can fix this by running the following three commands in termux:
cd /data/data/com.termux/files/usr/var/run/apache2
mv httpd.pid httpd.pid.back
/data/data/com.termux/files/usr/bin/httpd -k restart
If you find this is happening a lot, you could add these items to a script to make life easier. You may even want the script to run every time you start a termux session. If so you need to download another termux app - Termux:Boot, then create a folder called ~/.termux/boot, and place your scripts there.
Credits to Parzibyte's blog for the original termux apache/php howto