This is a list of challenges you face if you wish to run PHP code on a modern Android device.
I searched and found some projects implying that PHP can be run on Android
While I can't speak to any closed source projects, there are a few open source one, DroidPHP and more recent ones MyServer and lampTermux. But a bit of history/background is needed to understand what these projects represent.
PHP is written in C/C++. While Android does support C/C++ via the Android NDK, what's missing is that a PHP server expects to be run in a constant power Posix compliant environment which Android does not support out of the box.
The first one DroidPHP
, which appears abandoned by the author, uses Busybox which provides a POSIX compatible environment but requires superuser/root permission which requires 'rooting' the Android device. Note that 'rooting' generally voids device warranties.
With billions of users over the years, the Android ecosystem has increased the security of each version reducing the attack surface for any bad-actor.
The other two github projects use Termux which provide a POSIX-like environment, which can be used with root and in a more limited fashion without root.
Hey that sounds great, so I can use Termux
to run my PHP code.
A few caveats:
Android manufacturers have added in their own battery saving processing resulting in a site like: https://dontkillmyapp.com/ if your app only runs in foreground you are probably safe.
From the Termux repo README regarding Android 12:
NOTICE: Termux is broken on Android 12. Android OS will kill any (phantom) processes greater than 32 (limit is for all apps combined) and also kill any processes using excessive CPU.
- You'll need to host your own packages, if you intend to release a standalone app. From the Termux FAQ
Yes, as Termux is open-source and uses a GNU GPL v3.0 and in some components Apache-2.0 license, you can freely re-use existing components as soon as you met the license requirements.
However license does not cover "free" use of our hosting capabilities which are somewhat limited. You cannot use our package repositories in your own project(s). Please build packages and host them yourself.
Get started with information on developer's wiki pages: https://github.com/termux/termux-packages/wiki.
- Due to increased security restrictions Termux is no longer supported on the Google Play Store. So if the Android app is for commercial release, migrating your project to Java/Kotlin or even JavaScript in a WebView will allow for support in the Google Play Store, given that
Termux
(and its requirements for operation) is currently incompatible.
If you follow the embedded attempt issue thread you'll see that embedding is a involved process.
Sources:
https://www.reddit.com/r/termux/comments/p3exkr/how_can_i_make_an_android_apk_app_based_on_termux/