Android, IT

PHP and Lighttpd for Android

Update 7 May 2012: New version released, containing PHP 5.4.2 version (CGI only). It now contains openssl / curl / SQLite modules statically linked within php-cgi executable and thus curlssl works! Beside the version number, all installation instructions below still apply.

If you are already using PHP 5.3.8, just grab the php-cgi file from the 5.4.2 archive system/xbin folder and replace the /system/xbin/php-cgi on the device.

Introduction

Below you can find an bundle of PHP 5.3.8 (CGI only) and Lighttpd 1.4.29 (FastCGI module included) which are cross compiled for ARMv5 CPUs (Android platforms mainly).

As you mail well know, both PHP and Lighttpd offer modules that could be loaded at run-time based on settings within configuration files. The bundle doesn’t include all modules, due to difficulties encountered when compiling for ARM platforms. Those modules that are included are statically linked in the executables (so no additional files are needed) nor configuration files need to be updated to load them.

Screenshots

Below you can see screenshots with the phone browser opening localhost address. Once you unpack the archive, there’s an index.php file which just calls phpinfo() to show the details about the build.

Of course, you can remove the file and put your own scripts 🙂

phpinfo() on Sony Arc

phpinfo() on Sony Arc

phpinfo() on Sony X10 Mini

phpinfo() on Sony X10 Mini

PHP Modules

The list of PHP modules that are statically linked:

[root@arc]/sdcard# php -m
[PHP Modules]
cgi-fcgi
Core
ctype
date
dom
ereg
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
pcre
PDO
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
sockets
SPL
SQLite
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

Lighttpd Modules

mod_indexfile
mod_userdir
mod_dirlisting
mod_status
mod_simple_vhost
mod_evhost
mod_secdownload
mod_cgi
mod_fastcgi
mod_scgi
mod_ssi
mod_proxy
mod_staticfile
mod_evasive
mod_compress
mod_usertrack
mod_expire
mod_accesslog

Download

This comes with NO WARRANTY. Use it at your own risk!

php-5.4.2_lighttpd_1.4.29_armv5.tar.gz

php-5.3.8_lighttpd_1.4.29_armv5.tar.gz

License

Please check README.txt file found in /system/README.txt within the bundle.

PHP executable is offered under PHP license, Lighttpd executable under Lighttpd license, all other files in the bundle are offered under New BSD License (3 clause BSD license).

Installing

The bundle tries to mimic as much as possible the layout of a Debian installation of the two files. However, due to file system layout on Android phones (binaries under /system folder), the folders are one step down, under /system folder:

`-- system
    |-- bin
    |   |-- lighttpd -> /system/xbin/lighttpd-static
    |   `-- php -> /system/xbin/php-cgi
    |-- etc
    |   |-- lighttpd
    |   |   `-- lighttpd.conf
    |   `-- php
    |       `-- php.ini
    |-- var
    |   |-- log
    |   `-- www
    |       `-- index.php
    `-- xbin
        |-- fcgiserver
        |-- lighttpd-static
        |-- php-cgi
        |-- reload_fcgi.sh
        `-- reload_lighttpd.sh

Prerequisites:

  • rooted phone with busybox installed.
  • mount the /system partition as RW.

Using DroidSSHd on the phone and connecting with Putty (or equivalent) makes the whole process faster.

After you copy the archive above into the root of your SD Card, installation is easy:

cd /
tar xf /sdcard/php-5.3.8_lighttpd_1.4.29_armv5.tar.gz
reload-fcgi.sh
reload-lighttpd.sh

You might notice that the owner / group of the files is 1000:1000. Ignore that, as long as you execute them as root, there’s no issue (those are the IDs of my username on the PC I created the tar.gz archive and tar tries to conserve the user / creation time / etc from when the archive was created).

The last two commands start the FCGI server (PHP with 4 children processes), and then launch lighttpd server as daemon.

The default port is 80.
Default htdocs folder is /system/var/www.

The configuration files are under /system/etc/lighttpd/lighttpd.conf and /system/etc/php/php.ini. To restart either PHP or Lighttpd, please use one of the commands above.

It is mandatory that you are on the root folder when you extract the archive. tar will take care of creating all the missing folders, creating the links, etc, as long as you are on the root folder.

You can now open the phone’s browser and point it at localhost 🙂 and the phpinfo() page should appear.

Example usage

Installing Simple Machines Forum 2.0.1 on Android

Before executing the commands below, you need the “Full Install” smf_2-0-1_install.tar.gz archive downloaded from SMF and then copied onto your /sdcard root folder.

# cd /system/var/www
# ls
index.php
# mkdir forum
# cd forum
# tar xf /sdcard/smf_2-0-1_install.tar.gz
# reload_fcgi.sh
# reload_lighttpd.sh

Last two commands are optional, if you already started the FastCGI server as well as lighttpd.

You should now connect to http://localhost/forum and finish installation of the forum.

Enjoy 🙂

49 Comments

  1. secaviar

    Problem with php.ini :
    – in /system/xbin/fcgiserver, if I use “-c /system/etc/php/php.ini” option in php call, phpinfo says :
    . Configuraton file path : /system/etc/php
    . Loaded configuration file : (none)
    – if I write “-c /system/etc/php/”, phpinfo says :
    . Configuration file path ! /system/etc/php
    . Loaded configuration file : /system/etc/php/php.ini

    In either case, configuration options in php.ini are totally ignored.

    Any Idea ?

  2. Comment by post author

    Can you please provide a configuration parameter that you’ve modified and which gets ignored ?

  3. secaviar

    The parameter I wish to modify is session.save_path since my apps use sessions to save variables and, with the default value, an error is fired every time I call session_start in PHP.

    Since I cant update this parameter, I have to code a session_save_path call in every php page of my apps.

  4. Comment by post author

    I’ve tested and it works for me …
    I have my session.save_path set to /data/local/tmp and session_start works.

    The moment I change the session.save_path to an wrong folder (such as /fff ) – I get

    [sourcecode]Warning: session_start(): open(/fff/sess_2b03akgq6f8v5ds2r960v6slt0, O_RDWR) failed: No such file or directory (2) in /system/var/www/session.php on line 4[/sourcecode]

    To me it seems that something is wrong on your configuration, but I can’t tell what from the description..

    Can you please try to move the ini file to another partition ? Such as /data/data/etc for example and try again ? There are reports that sometimes php fails to read certain partitions (I don’t understand yet why, I never had a phone like that to investigate more …)
    A solution would be to move the configuration file to /data/data/etc/php/php.ini for example ?

  5. secaviar

    As you suggested, everything is OK if I move php.ini to another directory (I use /sdcard/data/phpconf on my Galaxy Tab 2).

    Thank you for your help.

  6. anilsoneji

    mysql functions is not work

  7. Maro

    How to make mysql works?

  8. DenisK

    Hello! Please, viulian, can you tell what exactly I need to change in glibc
    for exec() and system() (passthru()) to properly function? I think I must
    change somewhere “/bin/sh” to “/system/bin/sh”. Then crosscompile glibc
    and use it to crosscompile PHP. Also I want to know how to tell PHP
    to use my recompiled glibc during crosscompile. I will very appreciate your help.

  9. MietekN

    Unfortunately, in both editions of php does not work start with proc_open() . Always returns an error number 127 (file not found). Running the same command with the exec() works fine.

  10. DenisK

    Hello! Does someone crosscompile PHP with Android bionic instead glibc? I want to fix the problem with DNS (gethostbyname()).

  11. Comment by post author

    DenisK, using bionic, I think there was another PHP project compiled which was meant to be used as a scripting language as far as I remember.

    For the /system/bin/sh, I need to search the code, but if I remember well, by default it uses a PATH environment variable which contains the /bin/sh as first (and only) entry. This has to be modified to include /system/bin/sh.
    For how to tell PHP to use your glibc it is part of the cross compiling options provided with PHP.

  12. DenisK

    I already solved “/system/bin/sh” problem by editing some files in glibc source and then crosscompiled PHP with it. But gethostbyname() still does not function. I try to create /etc/resolv.conf and /system/etc/resolv.conf on my Adnroid device with DNS ip addresses but nothing happens. The main question is how to make gethostbyname work? I will very appreciate your help. So I think that bionic is needed. Above you wrote that creating /etc/resolv.conf will fix gethostbyname(). Why does it not work in my case? I crosscompile PHP using CodeSourcery toolchain (arm-none-linux-gnueabi). PHP 5.4.11.

  13. yermin

    I install this on S3C2416 demo board, But can not run correctly. I got “500 – Internal Server Error” or “503 – Service Not Available”.
    It simes seems lighttpd is OK, but php do not work.

    [root@spc2416 /]# /system/bin/lighttpd
    2000-01-01 10:44:35: (src/server.c.595) No configuration available. Try using -f option.
    [root@spc2416 /]# /system/bin/php
    Illegal instruction
    [root@spc2416 /]#

  14. Comment by post author

    It could be because I have compiled it with ARMv7 architecture I think .. I will read more about your board.

  15. Love PHP

    Can you help me to create tutorial step by step compile lighttpd for android which binary is ARM and static linked. I have compile with scons there if I check with file build/lighttpd-static not ARM but Intel. Or please sent to me file Sconstruct. I compile with arm-none-linux-gnueabi

  16. Love PHP

    I have success compile lighttpd, but server.dirlisting= “enable” not working. So always return 404 and index file not working so.
    How to fix it? thanks.

  17. phpdevloper

    I finally found an even better PHP binary by installing the KSWEB app.
    https://play.google.com/store/apps/details?id=ru.kslabs.ksweb
    This PHP handles the dir() function correctly in all Android paths. Moreover it supports mysql as well.
    Maybe you can contact the developer about cooperation, etc ?

  18. Comment by post author

    @phpdeveloper – I will do my own build and put everything on the market – yesterday I was able to reproduce the problem with accessing dirs by playing with the cifs module and Midnight Commander. It seems that once my Windows share is mounted, I can’t access the folder using MC (it is the same behavior for PHP, only that for some people it also happens for other folders, like /data and /system or /sdcard).
    It seems to be a bug in the opendir function that doesn’t call the 64 bit stat method, but the old 32 bit one.

    I will fix it as soon as possible ..

  19. dh J

    Thanks great post. possibly, Is there a way? install(or run) without mounting /system partition as RW.

  20. Hi there, I got this message in my device,
    I have run it as a root :

    sh: ./lihgttpd-static: cannot execute – Permission denied

    Any ideas ?
    thanks

  21. Love PHP

    Hi All. I’m using arm-none-linux-gnueabi from code sourcery to compile php and success get a binary file in sapi/cgi/php-cgi
    But if I running script php to exec(), system(), pasthru() not working.
    What should I do to work?
    I saw above should be changed /bin/sh to /system/bin/sh, but I do not know where/which file should I change?
    I hope my friends here can help solve my problem.
    Thanks.

  22. Love PHP

    @vulian:
    Do you have try compile it with code sourcery which cross compiler is arm-none-linux-gnueabi-
    I compile php-cgi with it, but function gethostbyname(), file_getcontent(), and etc which need call to internet not work, I have put file resolv.conf to /system/etc/ and /etc/ but still not work.
    Can you tell me how to fix it step by step.
    I have download and try your binary, it success and no problem, but my binary not work.
    Please tell me step by step.
    Thanks.

  23. Love PHP

    Hi all I have resolved function system, exec, pasthru with compile glibc new, but before compile glibc new I search on all file with grep /bin/sh intead #!/bin/sh and change to /system/bin/sh.
    So now my problem just gethostbyname, gethostbyaddr, etc.

    Thanks for advance.

  24. Comment by post author

    Congrats for the /bin/sh fix 🙂 as far as I remember, the trouble is with the built in PATH variable which points to /bin instead of /system/bin .. but if it works, is perfect.

    About get host by name, I think there’s nothing to fix, it should work by default – I did not touch anything related to that …
    What glibc are you using ? I’ve used buildroot 09/11 ..

  25. Love PHP

    @Vulian: Ohhh I see. May I request file .config.log.old for your buildroot, Because I want to use your configuration buildroot.

    Thanks vulian.

  26. Love PHP

    @Vulian: I have success make menuconfig for configure buildroot, I choose target arm-litleendian and target variant is generic_arm And in Filesystem Image I just select Tar. And after that I run make, I has been wait about 4/5 hours, So after I make, How I must to do for get PHP.

  27. Love PHP

    @Vulian: I have success compile php, but it not static, and how to I change solved to path /bin/sh in order to solved function exec, shell_exec, etc.

    Thanks,

  28. Comment by post author

    @Love PHP

    Congrats! Please check the ucLibc source code for the method exec – and you will see there is a PATH variable which is checked to find the “sh” executable, and the path only contains /bin folder. You will need to add there /system/bin also.

    About static, you don’t need to change anything, just check in the configure of PHP how to build it static …

  29. Love PHP

    Hi vulian, I has been solved with exec, but If I use function file_getcontent, etc which call to internet it’s very long time, about 10-15 second, My connection very stable, Can you help me once again 🙂

    Thanks

  30. Love PHP

    Finally, I cannot success compile, I get error segment fault 🙁

  31. MietekN

    @Vulian: May I request file .config from Your buildroot (probably buildroot-2011.05)?

  32. Comment by post author

    Here’s the a tar.gz containing the .config file for buildroot:
    buildroot-201105.config.tar.gz

  33. MietekN

    Thank You very much. I compiled php-5.4.2 for arm-generic and found the error. In order to function properly proc_open function You should also improve the proc_open.c twice “/bin/sh” to “/system/bin/sh”.

  34. MietekN

    Here are my manula, configuration and patches: http://www.2shared.com/file/3PNFHZ43/php-androidtar.html

  35. Love PHP

    Hi @Vulian.

    I’ve given up with compile php 🙁 . it’s very difficult, now I want to ask how the details to compile lighttpd for android.

    Thank you.

  36. MietekN

    @Love_PHP here is php compile instruction: http://www.4shared.com/file/X0yHbgyS/php-androidtar.html

  37. Hi,
    Thank you so much for this 🙂
    Altought I have one question, I can’t use imagettfbbox() which requies gd plugin, it seems that gd plugin works, exept this one command? any ideas?
    or is there any other way to implement gd? like in linux with gd.so module?
    thankyou for answers and feedback

  38. http://anmpp.net/(chinese)
    Android Nginx MySQL PHP-FPM PostgreSQL

  39. MietekN

    PHP Plug-in for the PAW server containing my compilation of PHP 5.4.2 compiled for ARM processors and also for x86 processors (PHP-5.4.2_arm, PHP-5.4.2_i386): http://www.4shared.com/zip/WU0_AIzW/paw_php_plugin.html

  40. yermin

    hi, all
    I got a “500 – Internal Server Error” after about 400-600 requests, and the “/system/bin/php” process exit.here is lighttpd log:

    2013-07-20 00:21:23: (src/log.c.166) server started
    2013-07-20 00:23:38: (src/mod_fastcgi.c.2566) unexpected end-of-file (perhaps the fastcgi process died): pid: 0 socket: tcp:127.0.0.1:9001
    2013-07-20 00:23:38: (src/mod_fastcgi.c.3354) response not received, request sent: 1036 on socket: tcp:127.0.0.1:9001 for /index.php?, closing connection
    2013-07-20 00:23:38: (src/mod_fastcgi.c.2566) unexpected end-of-file (perhaps the fastcgi process died): pid: 0 socket: tcp:127.0.0.1:9001
    2013-07-20 00:23:38: (src/mod_fastcgi.c.3354) response not received, request sent: 1036 on socket: tcp:127.0.0.1:9001 for /index.php?, closing connection
    2013-07-20 00:23:38: (src/mod_fastcgi.c.2566) unexpected end-of-file (perhaps the fastcgi process died): pid: 0 socket: tcp:127.0.0.1:9001
    2013-07-20 00:23:38: (src/mod_fastcgi.c.3354) response not received, request sent: 1036 on socket: tcp:127.0.0.1:9001 for /index.php?, closing connection
    2013-07-20 00:23:38: (src/mod_fastcgi.c.2566) unexpected end-of-file (perhaps the fastcgi process died): pid: 0 socket: tcp:127.0.0.1:9001
    2013-07-20 00:23:38: (src/mod_fastcgi.c.3354) response not received, request sent: 1036 on socket: tcp:127.0.0.1:9001 for /index.php?, closing connection
    2013-07-20 00:23:38: (src/mod_fastcgi.c.2904) establishing connection failed: Connection refused socket: tcp:127.0.0.1:9001
    2013-07-20 00:23:38: (src/mod_fastcgi.c.2910) backend is overloaded; we’ll disable it for 1 seconds and send the request to another backend instead: reconnects: 0 load: 1
    2013-07-20 00:23:38: (src/mod_fastcgi.c.3595) all handlers for /index.php? on .php are down.
    2013-07-20 00:23:40: (src/mod_fastcgi.c.2852) fcgi-server re-enabled: tcp:127.0.0.1:9001

    my php file:
    <?php
    session_start();
    if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+1;
    else
    $_SESSION['views'] = 1;
    echo $_SESSION['views'];
    echo '’;
    echo ‘hello world, now is ‘;
    echo date(“Y-m-d H:i:s”);
    echo ”;
    echo ‘current path is ‘;
    system(‘pwd’);
    echo ”;
    echo ‘proc:’;
    echo “”;
    echo system(‘top -n 1’);
    echo ”;
    echo ‘dir /:’;
    echo “”;
    echo system(‘ls /’);
    echo ”;
    ?>

  41. cloud

    I setted /etc/resolv.conf to 0755 permission and file_get_contents worked… I’m running paw server…
    The contents of resolv.conf is:
    nameserver 8.8.8.8
    nameserver 8.8.4.4

  42. dinakaran

    OS:centos I need to cross compile lighttpd server for ARM platform using scratchbox, While compiling, it shows the following packages were not installed, pcre,zlib,bzip2 but I have already those packages in my system. then i use –without-pcre,zlib,bzip2, but while deploying in android it shows mod_indexfile.so is missing. any solution?

  43. Comment by post author

    Have you tried to statically build everything ?

  44. How to compile lighttpd to be static for android?
    I looking for at google, untill now not success.

  45. Comment by post author

    Hello,

    What is the error that you get for lighttpd ? The process is rather tedious since they use scons to compile as far as I remember, and you have to spend a while to read about it.

  46. Hello @viulian.

    Please give me your config file “SConstruct” at root lighttpdsource and file “SConscript” at src folder lighttpdsource.

    Thank you

1 Pingback

  1. Linux on Cliq2

Leave a Reply