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 🙂

16 Comments

  1. The php application is unable find fnmatch function? How could I fix it?

  2. o d

    ive been looking for an alternative to programming in android,

    I know php very well..

    ive been thinking why there is not a packaging system

    that combines

    php executable
    a minimal (or none) httpd server
    a browser for local browsing, embedded in a main program in java
    and php scripts

    to make a apk program

    that could be put in play market, downloaded, installed and run locally?

    the java shell would have a browser window,
    it would run the php script with the php executable,
    show the results directly in the browser window, html returned from the php
    any clicks or input would be collected and sent to the php script again,
    and the html result shown again in the browser,

    no need for learning interface commands or java or other languages,
    all you would need is php and html and you can write full programs

    even graphical things with gd library..

  3. christian

    Hi,

    your work is almost exactly what i’ve been looking for!

    I want to run a webserver on my android phone to serve files to my ipad (storage is expensive there). I tried some webserers from the android marked, but the don’t do the job for video files.

    Lighttpd on linux does the job, so i guess Lighttpd on Android will do as well – unfortunatey my phone has a ARMv7 CPU and reading the posts there seem to be issues with that. Any plans to further investigate the ARMv7 thing? Could you post the options uses to build for ARMv5?

    regards

    Christian

  4. MietekN

    PHP and Lighttpd for Android compiled for ARMv5 also works on ARMv7 architecture.

  5. Joph

    Hello,
    I run your webserver on my android device,but it doesn’t walk well.
    my device don’t have command ‘tar xf’ , busybox either

    so I uncompression your package and push then to my device
    and I run command:
    ##########################################
    root@android:/system/xbin # /system/xbin/fcgiserver &
    [1] 3287
    root@android:/system/xbin # /system/xbin/lighttpd-static -f /system/etc/lighttpd/lighttpd.conf
    [1] + Done (255) /system/xbin/fcgiserver
    [ 141.429666] init: untracked pid 3299 exited
    root@android:/system/xbin #
    #########################################

    then I visit 127.0.0.1
    it shows 500 – Internal Server Error

  6. Stephen

    How can APC be enabled?

  7. Jon

    PHP 5.4+ now contains a built-in web server. Any chance you will compile a CLI version of PHP (ideally 5.5) without lighttpd? I think this would be incredibly useful! Thank you for your work on this.

  8. Comment by post author

    I’ll give it a try 🙂 thank you for the suggestion.

  9. huckleberry

    Thanks for your wonderful work! I see a little typo in the instructions:

    reload-fcgi.sh
    reload-lighttpd.sh

    There should be underscores here instead of hyphens.

  10. Mike

    You can add mbstring module ?

  11. Hi viulian, I am happy to see this project because I want to build my web server for Android for my educational research.
    I want to ask some questions :
    1. Can you provide some references or tutorials about building web server on Android ?
    2. I found this app and I noticed that this app didn’t need a rooted device. It also can run the web server. Here is the app link : https://play.google.com/store/apps/details?id=com.alfanla.android.pws

    I want to know this app is different form yours. Thanks for your help.

  12. Nice, it comes with mod_cgi and mod_fastcgi!

  13. krt

    I am trying to build lighttpd statically but so far no luck. Could you please share the commands you used to build static lighttpd?

  14. ryan

    how to connect mysql db ??

  15. Manish

    Hi Viulian,

    I uncompressed the package in root level of my board (OMAP L138 LCDK (linux arago)),

    and ran,

    root@omapl138-lcdk:/# tar xf /home/root/php-5.4.2_lighttpd_1.4.29_armv5.tar.gz
    root@omapl138-lcdk:/# ./system/xbin/reload_fcgi.sh
    kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec … or kill]
    root@omapl138-lcdk:/# ./system/xbin/reload_lighttpd.sh
    kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec … or kill]
    ./system/xbin/reload_lighttpd.sh: line 2: lighttpd: command not found

    but as this isn’t a android box, I did following,

    cp -r /system/bin/. /bin/.

    cp -r /system/etc/. /etc/.

    cp -r /system/var/www/. /var/.

    then ran,

    root@omapl138-lcdk:/# ./system/xbin/reload_fcgi.sh
    kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec … or kill]
    root@omapl138-lcdk:/# ./system/xbin/reload_lighttpd.sh
    kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec … or kill]

    Now when I go to board’s IP from my windows or Ubuntu 16, I’m getting 503 / 500 HTTP error

    root@omapl138-lcdk:/# which lighttpd
    /bin/lighttpd
    root@omapl138-lcdk:/# ./bin/lighttpd -v
    lighttpd/1.4.29 – a light and fast webserver
    Build-Date: Sep 16 2011 14:23:19

    root@omapl138-lcdk:/# which php
    /bin/php
    root@omapl138-lcdk:/# ./bin/php -v
    Illegal instruction

  16. for file_get_contetns or other functions dns name sever error,
    php_network_getaddresses: gethostbyname failed.
    ( need rooted )just push a resolv.conf to /system/etc/resolv.conf
    resolv.conf contents like below:
    nameserver 8.8.8.8
    nameserver 4.4.4.4

Leave a Reply