Back

To write PHP applications with Android : use GNURoot Debian

In a previous post I wrote about the possibility to develop PHP applications using a tablet. The main trouble was to find a reliable application to run a web server with a recent version of PHP featuring xDebug and the ability to run unit tests. It’s manageable but not fully satisfying so I kept looking for another solution.

Then I found GNURoot Debian and it changed everything : Debian Jessie running inside a virtual machine, you can’t ask for more.

I tried several Linux emulators and GNURoot Debian was the only one to work easily without tweaking its configuration. For instance, LinuxDeploy seems very powerful but it never managed to write its configuration on the file system. Even the higher level of logging didn’t point out what the real problem was.

First start

The application is really straightforward with its two tabs : one to install and update your emulated system, one to start it and that’s all. Considering the size of the buttons, you may ask yourself why there are displayed on two separated tabs while it could fit in one, but never mind we’re not looking for ergonomic design here, we want to code.

The installation process is simple : click on “install / reinstall”, wait for GNURoot Debian to build the fake root and you’re set. Then you can launch the system and get a root terminal over it. Please note that this root access has root privileges on the emulated system, not the Android device.

From now on you get a Debian Jessie system running with the strict minimum set of packages. Since main Debian repositories are available through apt-get, you can now customise the system to fit your needs. If your plan is to develop PHP applications you may want to install php5-cli, Git and curl from Debian repositories, then use curl to get Composer.

Emulation

An emulated system has interactions with its host and cannot behave like one. So running Debian inside Android implies low degradations you may be aware of, let’s review some of them.

Battery usage

GNURoot Debian drains the battery. It’s probably because Android can’t handle the services and processes launched through the emulated system, like putting them on hold while the device is locked. Be sure to turn it off when you stop using it with the “exit” command.

### File system

A SD card’s default file system is FAT32 for interoperability and so it is for a tablet’s storage. As a consequence you can’t set execution permission to files stored on it and it doesn’t support symbolic links. Formatting the tablet’s storage seems to be a pretty bad idea, so I need to find something else here. I must know where the FAT32 file systems and how to deal with them.

Several things here :

  • df -T doesn’t include / in the file systems list, the output seems to be incomplete
  • /home is a FAT32 file system, otherwise the symbolic link would have been created
  • /usr supports symbolic links, that means its file system is different from /home
  • there is a /data partition, probably the files stored in the inner storage ?

Well maybe. We’ll never know. Let’s see what’s at the upper level :

/sdcard contains my personal data, awesome!

As you can see, it’s not clear which folder is a mount point and what file system is used in it. From what I saw, /home uses FAT32 and so does /sdcard (which is pretty logical), the rest seems to use ext4. So you’ll need to adapt the way you install and use softwares like Composer to take advantage of them. And since Composer is a very good example, I’ll dig into it in the next few paragraphs.

Composer

Composer is provided under the Phar format and can act as an executable file. Some of the dependencies managed through Composer are also coming with an executable, such as PHPUnit.

Installing Composer is as simple on GNURoot Debian as on other Linux systems. Just follow the instructions from http://getcomposer.org, make composer.phar executable in /usr/local/bin and you’re done. Want PHPUnit to run as an executable like you’ve read in the documentation? You’ll have to tweak the system a bit.

Composer’s default working directory is defined through an environment variable called $COMPOSER_HOME which default value is $HOME/.composer. If you ask Composer to install a package in the global scope, Composer stores it in $COMPOSER_HOME/vendor. And if the package provides an executable file (like PHPUnit does) it will be installed in $COMPOSER_HOME/vendor/bin.

In GNURoot Debian the final value is /home/.composer, right in the middle of a FAT32 file system. Adding /home/.composer/vendor/bin to the $PATH variable will only lead to “permission denied” errors, you must change Composer’s home directory to fix it. This is easy, you just need to create /home/.bashrc and write the following line in it :

    export COMPOSER_HOME=/usr/local/composer

And add this to run executable files like phpunit without taping the full path :

    export PATH=$PATH:$COMPOSER_HOME/vendor/bin

Load it (this step won’t be necessary next time you start the emulator) :

    source /home/.bashrc

Now you can install PHPUnit and use it system wide like you use to with a desktop :

    composer global require phpunit/phpunit

Composer will download and install PHPUnit in /usr/local/composer/vendor/phpunit/phpunit then create its executable in /usr/local/composer/vendor/bin. After the process is over, you can type phpunit –version :

Let’s see if it works like expected in a real project :

We’re done.

Network

The hosted system is not allowed to open ports using numbers under 1024 since it’s not an application running with the root permissions on the device. That means Apache and Nginx won’t be able to start using their default configuration and you’ll get a warning during the installation process. The solution is to change the port the web server is listening to and restart it.

Being that said, PHP internal web server works pretty well in Debian. So if you only need to develop PHP applications, you really don’t have to bother with the installation of a web server like Apache. You’ll go through that matter the day you want to perform deployment tests on your device. Which is not really relevant anyway when you consider online platforms like Codeship, Heroku, etc…

Conclusion

GNURoot Debian is my final choice. No need to unlock the device, the application fits in a Android environment and it offers a fully functional Debian system. It sure needs some tweaks, but these are minor things involved by the way Android works. Now if you are interested in applications like an editor or a Git client, I suggest you to read this post.

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy