Termux from AppInventor App


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?

  1. Because we can ;)

  2. 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?

  1. 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.

  2. Termux has a few quirks on Android => 10

  3. 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



#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


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


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




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: (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:


which is the same as


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:


$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)


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.



  1. 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.

  2. 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:

    1. http://<IP ADDR>:8080/index.php

    2. http://<IP ADDR>:8080/command.php?command=ls

  3. There are a couple of environment variables for filepaths you can use:

    1. ${HOME} or $HOME which is the same as /data/data/com.termux/files/home

    2. ${PREFIX} or $PREFIX which is the same as /data/data/com.termux/files/usr

    3. I also created another one for the equivalent of the html directory by typing this at the command line:
      which equates to:
      ${HTML} or $HTML

  4. PHP Server not running/corrupted

    1. 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.pid

    2. You can fix this by running the following three commands in termux:

      1. cd /data/data/com.termux/files/usr/var/run/apache2

      2. mv httpd.pid httpd.pid.back

      3. /data/data/com.termux/files/usr/bin/httpd -k restart

    3. 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.

    4. Credits to Parzibyte's blog for the original termux apache/php howto