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 🙂

48 Comments

  1. Comment by post author

    Sorry for the confusion 🙂 probably they did change something and fix the bug and most probably the version 5.4.1 that I built had it.
    I have thus updated to 5.4.2 so that the bug won’t be there anymore.

    I will release it tonight – I made some tests with curl / exec and it works.

  2. Comment by post author

    Please check the initial post, I have updated it with the download link for 5.4.2 as well as a small change log (curl / openssl now built in).

  3. phpdeveloper

    On Android 4 ICS it does not work anymore.

    [code]
    php /system/var/www/index.php
    [/code]

    results in

    [code]
    Status: 404 Not Found
    X-Powered-By: PHP/5.3.8
    Content-type: text/html

    No input file specified.

    [/code]

    Is this a known problem ?

  4. phpdeveloper

    By the way I did NOT change any config files and used 5.3.8 + lighty 1.4.19.

  5. Comment by post author

    Can you please tell me what kernel version you have ?

  6. phpdeveloper

    Kernel is 3.0.15-i9100-XWLP7-CL340913

    It is pure in the php kernel, it is probably not a lighttpd issue.
    I reproduce it also without lighttpd being loaded.
    when I run :
    [code]
    cat var/www/phpinfo.php | php
    [/code]
    it worrks fine (at least in a terminal.

    But

    [code]
    php var/www/phpinfo.php
    [/code]

    issues the ‘No input file specified’ error.
    Strange. the 5.4.2 has the same issue. Seems that piping / redirecting is different in the 3.0 / ICS 4 kernel.

  7. Comment by post author

    Ok, there are two things:

    a) if you could find a strace that could help what goes on. I have one compiled for 2.6 kernels and it might work : http://hex.ro/tracker/EasyTracker.php?id=46 (just unpack the .zip and copy the binary to /system/xbin)

    Then issue:

    [sourcecode language=””]strace -f php var/www/phpinfo.php > log.txt 2>&1[/sourcecode]

    I’m curious if it is working, then if it produces results, what could be the problem. Please give me a link to the log file if you get one usable.

    2) The trouble is that none of my phones have official ICS – I was able to put a ICS ROM on one of them (the S5570) but it comes with kernel 2.6.35 so … I suspect something in the kernel has changed – since really none of the other Android libs are used by the project.

    I am waiting for S3 to appear then drop price and hopefully be rooted – I can’t afford now a phone with better kernel and ICS.

    Tell me please if you are also using the official ROM of the phone – or some custom made cyanogenmod ROM ?

  8. phpdeveloper

    Hi,

    Maybe it’s easier to communicate via email you should have my email address.
    I tried it:

    [code]
    strace -f php var/www/phpinfo.php > log.txt 2>&1
    [/code]
    did issue an error message that php does not exist.
    So I tried:
    [code]
    localhost:systemfolder # strace -f xbin/php-cgi var/www/phpinfo.php
    strace: xbin/php-cgi: command not found
    localhost:systemfolder # ls -al xbin/php-cgi
    -rwxr–r– 1 root root 10806533 Sep 22 2011 xbin/php-cgi
    localhost:systemfolder #
    [/code]
    Strange that it issues ‘command not found’.
    I have an official Samsung ROM (XWLP7) so no custom ROMs..

  9. phpdeveloper

    Additional options:

    [code]
    php var/www/phpinfo.php
    [/code]
    issues ‘No input specified’ but

    [code]
    cat var/www/phpinfo.php | php
    [/code]

    issues the expected output.
    But in the fcgiserver script changing the script to
    [code]
    cat – | php -b 127.0.0.1:9001 -c /data/systemfolder/php.ini >> /data/systemfolder/var/log/fcgiserver.log 2>&1
    [/code]
    does not work (same ‘no input file specified’ error). I googled around on the ‘no input file specified’ and there are lots of hits, but no tips for this.

  10. phpdeveloper

    BTW if you have an S2 (or another Android phone for which ICS is officially released) you can just install ‘Mobile Odin Pro’ ($2), download an official ICS 4.0 kernel from http://www.sammobile.com. Mobile Odin Pro flashes any kernel (also an official) on the device itself and roots it on the fly. I did this and after 4.0.3 was flashed, the root was retained.

  11. phpdeveloper

    You can also use an Android emulator which is part of the Android dev kit and emulate any official version on your Mac / Linux / Windows PC.

  12. dqroid

    test on SAMSUNG Galaxy Nexus with Android 4.0.4 stock ROM

    when I run :

    cat var/www/phpinfo.php | php

    it worrks fine (at least in a terminal.

    But

    php var/www/phpinfo.php

    issues the ‘No input file specified’ error.

    plz fix it

  13. Hey guys,
    I had the problem with the ‘No input file specified’ too.
    I use at the moment PHP 5.3.6, because the other versions cause problems..
    Hopeful waiting for the fix..
    (Using Galaxy Note with ICS 4.0.3 custom rom from alba)

  14. Comment by post author

    Please tell me what kernel you have on your Galaxy Note ?
    I have a sort of fix for kernels 3.0 but is not perfect … I will have to buy a phone with ICS and Kernel 3.0 and do debugging (since on my ICS but with kernel 2.6 it doesn’t manifest).

  15. Sure 🙂 i dont know which rom was that, but i got the kernel insalled with the rom… currently i am using alba ddlp8.
    Settings -> kernel version:
    3.0.15-franco.Kernel
    francisco@francisko #3

  16. csharper

    Btw viulian, couldn’t you use emulator rather than buy ICS phone? *just saying

  17. Matt

    Hello,

    I have the same problem, with Kernel 2.6

    Tried all the suggestions from phpdev, and I get the same messages.

    Any idea on how to solve this?

    Linux localhost 2.6.35.7-G70XXKPF-CL562492 #2 PREEMPT Fri Sep 16 20:54:18 KST 2011 armv7l GNU/Linux

  18. Matt

    Same thing with:

    Linux localhost 2.6.35.7-G70XXKPF-CL562492 #r14 PREEMPT Sun Mar 11 16:16:26 CLST 2012 armv7l GNU/Linux

    Android 2.3.6

  19. Comment by post author

    Sorry guys, I was out for a vacation and was not up to date. I will investigate the issues and hopefully fix them soon!

  20. xiling

    Hi,could you compile it with gnu readline module,or the cli mode?I use it for script in terminal ,It ‘s very useful.thank you
    I come from China,My English is not very.

  21. zxtyler

    hi,I installed php-5.4.2_lighttpd_1.4.29_armv5.tar.gz and SMF forum,but it says:

    !!
    Critical Error!

    The installer was unable to detect any database support in PHP. Please ask your host to ensure that PHP was compiled with the desired database, or that the proper extension is being loaded.
    when I use other php+sqlite programes it alse has the same words:”Call to undefined function sqlite_open()”
    I modified the php.ini and got rid of the “;” before the line about sqlite,but it didnt’ work.
    It seemed php cgi does not support sqlite database, could u help me with that problem,thanks.

  22. ic

    Hi, I found some bug in “php-5.4.2_lighttpd_1.4.29_armv5.tar.gz”
    It’s problem with ICONV
    First example from php documentation ( http://php.net/manual/en/function.iconv.php )
    works correcty on my localhost PHP 5.4.4, but on Android it failed with //TRANSLIT and //IGNORE flags. It write nothing.
    It can be PHP bug: https://bugs.php.net/bug.php?id=61484 but I’m not sure.

    Is there some chance to fix it? Please, ICONV is important for non-english countries.

  23. phpdevloper

    zxtyler: This PHP does support sqlite. I have downloaded a PHP sqlite manager which works both on my Mac and Galaxy S2. Databases of apps can also be vewed or modified.

    viulian: Did you already have a look at the dir() bug ?

  24. Hemant

    It seems like sqlite is not working with PHP 5.4.2. I tried with PHP sqlite manager as well.
    It gives Fatal error: Call to undefined function sqlite_open()

  25. Comment by post author

    Can you guys post a little snippet that I could use to test sqlite ?

    Thank you,

  26. Hemant

    Here is the code for sqlite:-

    ////////////////////////////
    $dbhandle = sqlite_open(‘test.db’, 0666, $error);
    if (!$dbhandle) die ($error);
    $stm = “CREATE TABLE Friends(Id integer PRIMARY KEY,” . “Name text UNIQUE NOT NULL, Sex text CHECK(Sex IN (‘M’, ‘F’)))”;
    $ok = sqlite_exec($dbhandle, $stm, $error);
    /////////////////////////////

  27. Hmmm I am running cyanogen mod 9 and under 5.4.2 curl is giving me grief not being able to resolve hosts. Anyone can confirm?

  28. example code:

    #!/system/bin/php

    Output:

    X-Powered-By: PHP/5.4.2
    Content-type: text/html

    2012-08-15T13:49:14+02:00
    bool(false)
    array(26) {
    [“url”]=>
    string(21) “http://www.google.com”
    [“content_type”]=>
    NULL
    [“http_code”]=>
    int(0)
    [“header_size”]=>
    int(0)
    [“request_size”]=>
    int(0)
    [“filetime”]=>
    int(-1)
    [“ssl_verify_result”]=>
    int(0)
    [“redirect_count”]=>
    int(0)
    [“total_time”]=>
    float(0)
    [“namelookup_time”]=>
    float(0)
    [“connect_time”]=> float(0)
    [“size_upload”]=>
    float(0)
    [“size_download”]=>
    float(0)
    [“speed_download”]=>
    float(0)
    [“speed_upload”]=>
    float(0)
    [“download_content_length”]=>
    float(-1)
    [“upload_content_length”]=>
    float(-1)
    [“starttransfer_time”]=>
    float(0)
    [“redirect_time”]=>
    float(0)
    [“certinfo”]=>
    array(0) {
    }
    [“primary_ip”]=>
    string(0) “”
    [“primary_port”]=>
    int(0)
    [“local_ip”]=>
    string(0) “”
    [“local_port”]=>
    float(0)
    [“pretransfer_time”]=>
    float(0)
    [“size_upload”]=>
    float(0)
    [“size_download”]=>
    float(0)
    [“speed_download”]=>
    float(0)
    [“speed_upload”]=>
    float(0)
    [“download_content_length”]=>
    float(-1)
    [“upload_content_length”]=>
    float(-1)
    [“starttransfer_time”]=>
    float(0)
    [“redirect_time”]=>
    float(0)
    [“certinfo”]=>
    array(0) {
    }
    [“primary_ip”]=>
    string(0) “”
    [“primary_port”]=>
    int(0)
    [“local_ip”]=>
    string(0) “”
    [“local_port”]=>
    int(0)
    [“redirect_url”]=>
    string(0) “”
    }
    string(38) “Couldn’t resolve host ‘www.google.com'”

    Kernel with uname -a:

    Linux localhost 3.0.8-g54d09f2 #1 PREEMPT Tue Aug 7 23:40:24 PDT 2012 armv7l GNU/Linux

  29. Last but not least, I just tried an ip address getting arround the resolve issue and that just went fine. I got the results I wanted. So it is defenitly something with resolving addresses.

  30. I cross-compile php for android can not open the Internet domain name can help it?
    error info:
    Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /mnt/sdcard/ROOT/var/www/special.php on line 2

    Warning: file_get_contents(http://php.net): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /mnt/sdcard/ROOT/var/www/special.php on line 2

  31. phpdeveloper

    @aite.me:

    This is not an issue of PHP but of your device.
    Create a file in /etc called “resolv.conf”
    with the addresses of DNS servers in it (here for example the DNS servers of Google):
    [code]
    nameserver 8.8.8.8
    nameserver 8.8.4.4
    [/code]

    How did you compile PHP ? Which tools did you use. I have a version in which the dir() and shell_exec() function do not work.

  32. Comment by post author

    @phpdeveloper – what version are you using, as far as I remember dir() and shell_exec() were supposed to be fixed ?

  33. @viulian
    This is my cross-compile php5.4.7(www.279927.com/downloads/almp-special.zip)
    The network dns and shell_exec () not working properly!
    I produced php for android integration, which includes lighttpd, php, mysql.

  34. @viulian
    I arm debian linux gcc compiler in qumu virtual machine used.

  35. phpdeveloper

    I tried the versions 5.4.2 of 17 May and 5.4.7 from aite.me download link above.

    5.4.2 as earlier mentioned, dir() and shell_exec() do not work
    5.4.7 as earlier mentioned, dir() works flawlessly but and shell_exec() not and now file_get_contents() does not work. It issues the following warning:

    Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /mnt/sdcard/website/startpage/google.php on line 44

    Warning: file_get_contents(http://www.google.com/search?): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /mnt/sdcard/website/startpage/google.php on line 44

    Reloading the same page with 5.4.2 works fine.

  36. @phpdeveloper
    I compiled php can open ip, but can not open the domain name.

  37. @viulian
    Can compile lighttpd latest version?
    Specify the temporary directory :/data/local/tmp。
    You compile lighttpd due to the cache directory can not accept large file uploads。

  38. phpdevloper

    @aite.me:
    > I compiled php can open ip, but can not open the domain name.
    When file_get_contents() does open an IP address but not a domain name then there is a problem in resolving domains.
    Did you try it from the prompt ? If yes, try e.g. ping google.com .
    And did you try a website
    [code]
    <?php
    $content = file_get_contents("http://www.google.com&quot;);
    echo "<pre>".htmlentities($content); "</pre>";
    ?>
    [/code]
    And run the website from the browser localhost/path/yourfile.php .

  39. Comment by post author

    Ok guys, I’ll have a look in the weekend. I’m sorry for the inconvenience. I might also ask for some sample code just to make sure I execute the same thing.

    Thanks,

  40. phpdevloper

    @aite.me , @viulian:

    Having a look at this function might help ? Gethostbyname() is IMHO the workhorse behind domain based hostnames in file_get_contents() with a URL.

    http://nl.php.net/%20gethostbyname

  41. @phpdevloper
    I compiled php based on glibc library, android is based Bionic library. Need to refer to the the Bionic library can solve dns problem.

  42. phpdeveloper

    @aite.me: So you got it working ?

  43. SebZ

    Hi guys Thank you very much for all the feedback!! It s working for me on asus tablet tf101. It has some bugs but curl and file_get_contents is working fine. I only have to retart en TAR FX everytime to get it working after every restart! Is there no services install, or something quicker to start it more quicker?

    Again Thank you very much for all the hardwork!

  44. @phpdeveloper
    No, just do dns problem.

  45. @viulian
    Do you use the cross compiler tools? Where to download?

  46. phpdeveloper

    Some (strange) good news: Using the Viulian version of PHP 5.4.2 and setting the document root of lighttpd in the /sdcard tree results in that the dir() function *does* work.
    [code]
    server.document-root = "/data/systemfolder/var/www"
    # dir() not working

    server.document-root = "/sdcard/www"
    # dir working

    [/code]

    I always use the first version. So it seems that the dir() function cannot handle ext4 file systems (native to Android 4.x) and no FAT systems when the document root is symlinked as it was in my case.
    Now I changed my document root and the dir() is working ! But still an issue on ext4.

  47. Comment by post author

    Wow, interesting! I’m happy it works for you with the /sdcard since I’m at a loss why it wouldn’t work with the /system partition.

    Can you please tell me your mount options for /system – since mine is ext4 and dir() works also ?
    After checking your post more carefully – I never tried having the root folder into /data folder, I always used /system/var/www – I will check now with the /data too – as I see in your post above.
    For sure, the /data has more mount options than /system – maybe something here causes the problem. Here’s mine:

    [code]/dev/block/mmcblk0p9 /system ext4 rw,relatime,barrier=1,data=ordered 0 0
    /dev/block/mmcblk0p12 /data ext4 rw,nosuid,nodev,noatime,barrier=1,journal_async_commit, data=ordered,noauto_da_alloc,discard 0 0[/code]

    Thank you very much again for spotting this!

    Later edit: I am still not able to reproduce the problem with the dir() having /data/…/ as the root. Maybe the mount options are different from you ?
    What is the locale of your phone ? Maybe something else is different, since I remember you had the same issue in the past with a different phone.

  48. phpdevloper

    I replied you via pm on xda.

Leave a Reply