Logo of PHP

Installation and Configuration of PHP on Fonz fun_plug 0.5

German version of this tutorial

This tutorial is deprecated and should only be used with fonz fun_plug 0.5!
Please check the tutorial page for updated tutorials on this topic!

PHP is a server-side scripting language that is widely used for developing dynamic web sites. PHP allows custom software to run on the server (in our case the NAS) hosting the web site.

Logo of PHPPHP scripts are typically embedded in HTML pages. When a browser requests such a HTML page, the scripts are executed by the PHP add-in module of the HTTP server (here: lighttpd). Usually the PHP code inserts some additional HTML content into the version of the HTML page that gets sent to the browser. The browser (“client”) itself thus doesn’t see the scripts which were executed on the server.

In this tutorial we assume that the lighttpd HTTP server is already installed on the NAS (see tutorial on installing lighttpd On bigger systems, PHP is sometimes used in combination with an Apache server (instead of the leaner lighttpd) and an SQL database. This widely-used bundle of software tools is sometimes known as LAMP: Linux, Apache, MySQL, PHP. The installation of MySQL on the NAS is covered in another tutorial.

Contents

PHP uses

With regular HTML pages, your web server can only provide static content: all users see the same set of pages and the pages don’t change until someone (e.g. manually) updates the stored HTML pages. A server-side scripting language like PHP helps if you need to add dynamic content, such as a visitor counter (see example code below) or maybe news-of-the-day.
Public domain as well as proprietary PHP software is available for various applications including:

Setting up PHP

Installation

Uli kindly provided a packaged version of php for the NAS in his repository.

PHP is not installed as part of fun_plug by default, but you should already have downloaded a copy as part of the general tutorial on how to download, install and upgrade packages. Let’s make sure you still have the latest version (as Uli upgrades his repository regularly). Note that the rsync command could take a while because it can download multiple packages depending on what is already in your /ffp/pkg/packages directory. Also note that you have to install curl additionally! As of PHP 5.2.17 you also have to install libiconv which is in fonz repository.

cd /ffp/pkg
funpkg -i /ffp/pkg/additional/dev-lang/php-*.tgz
funpkg -i /ffp/pkg/additional/net-misc/curl-*.tgz
funpkg -i /ffp/pkg/packages/libiconv-*.tgz

If you have a version of php installed that is outdated, you will need to run in upgrade mode instead (see here for help). Still, make sure you have curl and libiconv installed!

funpkg -u /ffp/pkg/additional/dev-lang/php-*.tgz
funpkg -u /ffp/pkg/additional/net-misc/curl-*.tgz
funpkg -u /ffp/pkg/packages/libiconv-*.tgz

Configuring PHP

PHP is configured with a file called php.ini. You thus need to copy one of the example-files from /ffp/etc/examples/ to /ffp/etc/ while renaming it to php.ini:

cp /ffp/etc/examples/php.ini-wolfuli /ffp/etc/php.ini

Configuring Lighttpd

To use PHP with the lighttpd-webserver, you have to use another configuration file as explained in the section on config files in the tutorial.

If you have a NAS other than the DNS-320, you execute the following:

cp /ffp/etc/examples/lighttpd.conf-with-php /ffp/etc/lighttpd.conf

If you have a DNS-320, you execute:

cp /ffp/etc/examples/lighttpd.conf-dns320 /ffp/etc/lighttpd.conf

Activation

Restart lighttpd to load the new configuration – including the PHP module. This can be done by rebooting the entire NAS or using

cd /ffp/start
sh lighttpd.sh restart

Testing PHP

You can test your installation of PHP by placing a file called index.php in the server.document-root directory (e.g. /mnt/HD_a2/www/pages) with the following content:

<H1>This is normal HTML</H1>
But the <U>following table</U> is generated by PHP:
<?php
phpinfo();
?>

PHP configuration information
PHP configuration information
Now go to the website on your NAS using the configured address and e.g. http://CH3SNAS:80. You should see a long page with configuration information for the PHP server as shown in the picture). This output is generated by the function call “phpinfo()“.

Using PHP

Let us now go back to a somewhat less intimidating and possibly even boring example: the standard “Hello World” in PHP. Copy and paste the following text to a file named helloworld.php and store it in the server.document-root:

<?php
echo "Hello World";
?>

Then go to the website on your NAS using the configured address and port and add /helloworld.php (the address may look like this: http://CH3SNAS/helloworld.php).

The following example shows a more complete web page (adapted from the lighttpd tutorial). It adds 3 features:

  • visitors are greeted using their IP address (using the $_SERVER['REMOTE_ADDR'])
  • any viewing of the web page triggers updates to a file (counter.txt) stored on the NAS
  • the web site uses counter.txt to show how often the page has been viewed

This requires three fragments of PHP code, each enclosed between and ?> tokens:

 
<?php
$fname = "counter.txt";             // The file where the number of hits gets stored
if(!file_exists($fname)) {          // If file doesn't exist..
    $countfile=fopen($fname,"a");   // .. create it
    $counter=0;                     // .. and initialize hit counter to zero
}
else {
    $countfile=fopen($fname,"r+");  // Open for read and write
    $counter=fgets($countfile,100); // Load number of hits by reading first 100 bytes
    rewind($countfile);             // Reset the file pointer to overwrite old counter value
}
$counter++;                         // Increment counter by one
fputs($countfile,$counter);         // Write the new value to the file
fclose($countfile);                 // Close the File
?>
 
<html>
    <head>
        <title>Hello PHP World!</title>
        <style type="text/css">
        <!--
                        h1 {text-align:center; font-family:Arial, Helvetica, Sans-Serif;}
                        h2 {text-align:center;}
                        p  {text-indent:20px;}
        -->
        </style>
    </head>
 
    <body bgcolor = "#ffffcc" text = "#0000ff">
        <h1>Welcome, <?php echo $_SERVER['REMOTE_ADDR']; ?>, <BR>to the PHP world</h1>
        <h2>This page was viewed <?php echo $counter; ?> times</h2>
        <p><A HREF="page1.html">Link to page1</A></p>
        <p><A HREF="page2.html">Link to page2</A></p>
        <p><A HREF="https://nas-tweaks.net/">external link</A></p>
    </body>
</html>

Output of final example
Output of final example
HTML output as visible using the browser's View Source feature
HTML output as visible using the browser's View Source feature

The first and by far the longest PHP section looks to see if a file name counter.txt exists in the server.document-root If it doesn't exist, it creates it and decides there have been zero hits so far ($counter=0). Note that variables in PHP do not need to be declared and start with a $ and that no clear distinctions are made between numeric and string variables: conversions are done on demand.

If the file already exists, the first 100 bytes (should be enough for a decent number) are copied into $counter. Then $counter is incremented, written to the counter.txt file and the file is closed.

The remaining HTML code is similar to the code in the lighttpd tutorial. In the level one header (H1), a string is generated by a PHP echo command which prints the IP address of the remote client. In the level two header the value of $counter that was previously computed is used.

Optional: Using shared extensions

PHP allows the use of additional modules, so called "shared extensions". E.g. there is calendar for calendar-related functions. The modules are stored in /ffp/lib/php/extensions/no-debug-non-zts-20060613/.

Available modules

You can list the available modules using:

ls -al /ffp/lib/php/extensions/no-debug-non-zts-20060613/*.so

As of version php-5.2.6-3 the following modules are available:

Module Description Requires installation of ffp package
calendar functions related to days/months/years and Unix timestamps -
ctype character type checking -
ftp File Transfer Protocol -
gd image processing libjpeg, libpng
mbstring manipulation of non-ASCII strings (e.g. unicode) -
mysql MySQL database access mysql
pdo PHP data objects -
pdo_mysql PDO interface to the MySQL database mysql
pdo_sqlite PDO interface to the SQLlite database -
sqlite SQLlite database access -
tokenizer access to PHP tokens found by the lexical analyzer -
zlib .gz file compression -

Editing php.ini

If you want to add one or more modules, you need to edit the file /ffp/etc/php.ini.
Open php.ini and find:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

And add below the extension you want. E.g. to enable the calendar extension, you add:

; Linux extensions
extension=calendar.so
;extension=ctype.so
;extension=ftp.so

Afterwards you have to restart lighttpd to load the changes.

Demo of calendar extension

To see whether or not calendar was indeed loaded, you could run the PHPinfo.php script shown above.
Or run a script calendar.php containing:

<?php
for ($year=2000;$year<2011;$year++) {
    $days = cal_days_in_month(CAL_GREGORIAN,2,$year);
    echo "February in $year has $days days <BR>";
}
?>

Notes

PHP and HTML

A page with PHP code is generally stored on disk as HTML code with one or more fragments of embedded PHP. When the HTTP sends delivers the actual page to the browser ("client") the PHP sections have been interpreted and removed. In general they have been replaced by additional HTML code (see the counter example above). The receiving client thus does not see the PHP code and cannot even directly know whether PHP has been used to generate the served page.

Short open tag

If you want to use "<?" instead of "<?php" to mark the start of PHP code fragments, you need to change change the value of "short_open_tag" (line 131) in /ffp/etc/php.ini file to On:

short_open_tag = On

This is a matter of taste and convenience. When you distribute PHP code for use in other servers, you need to keep in mind that they may be set to short_open_tag = Off.

File extensions and default file names

HTML pages containing PHP code should have a .php extension. This tells the lighttpd server to pass the code via the PHP preprocessor before sending it across the network.

When you access the lighttpd server without providing a specific file name, it successively checks for files in the server.document-root directory(typically /srv/www/pages/) named

  • index.php
  • index.html
  • index.htm
  • default.htm

This is defined in a parameter called index-file.names in the lighttpd.conf file. As the list suggests, if you have both an index.php file and an index.html file in the same directory, the index.php file has precedence. If you explicitly ask for index.html, however, you will get that file instead of index.php.

Stateless

Note that after a page has been processed, when a new request for the same page is made by the same or another client, all computation starts from scratch: PHP is "stateless" in the sense that variables such as $counter are all lost after the page request has been completed. So the only way to track state in a PHP server is to store information in a file (as in counter.txt above), to store information in a database (another tutorial), or to assume the client maintains any relevant information between one page request and the next one.

Learning the PHP language

There are numerous books (printed and online) on PHP. See for example php.net.

Dealing with PHP errors

PHP syntax errors messages are appended to the Lighttpd error.log file. So if you are experimenting with- or developing PHP code, it can help to keep an eye on that file.

26 thoughts on “Installation and Configuration of PHP on Fonz fun_plug 0.5”

  1. The php test isn’t working for me.. I’m not sure where to look… my first problem was that I’m using a DNS 320, but didn’t have the /ffp/etc/examples/lighttpd.conf-dns320 file, found one on the web, but get a TLS error when use it.

    What can I check / look at ??

      1. Uli,

        I did follow the lightttpd install first, then this one.. but no luck on the php (lighttpd will serve up webpages, just the php portion is not being run).

        G.

  2. Just wanted to say thanks for all the tools, tutorials and maintenance work you do. It’s a stellar effort that greatly enhances the fun of having an NAS.

  3. Hi, my name is Jose, I am from Spain so please be patient with my English, I will try to do my best.
    I have a DNS-323, I just started to install ‘fonz fun_plug’ and as fas as I know everything has gone well, I didn’t get any error.
    Right know I have finished to install the lighttpd and that’s why I am writting this message.
    It is supposed that my ports has changed from 80 to 81 (I mean to connect the standard configuration screen) and I will enjoy my new web server on 80. But nothing at all has happened in that way.
    I still have the configuration screen in 192.168.1.11 as always (wich is my local ip for the NAS), even I have rebooted the system.
    Althought, I created the three files; index, page1 and page2.html as the manual indicates me and located them into /www I can not reach them, of course.
    Does anybody know what was wrong? I had read very carefully all the steps many times and I think I have not forgotten any of them.
    It is like ‘sh /ffp/start/kickwebs.sh’ were not doing its job.
    Thanks for your help.
    I will be looking for an answer.
    Kind regards.
    Jose.

    1. jose,

      I have recently tweaked my DNS-320 by installing lighttpd and other packages. Try to login using ssh. If you are successful then verify by ps -A that there are two webservers running – lighttpd and lighttpd-angel and that they are running from /ffp/sbin path. I guess that is the starting point. If not then check that you have set the exec permissions for corresponding scripts in /ffp/start and that you have correct configuration files in /ffp/etc . With DNS-320 I have also noticed that doing reboot from ssh login sometimes does not start the funplug stuff (occurred to me on two occasions) so try to restart the NAS from original control webpage. I guess there isn’t much difference between 320 and 323 regarding these instructions.

  4. I was wondering if anybody managed to make some simple php photo gallery run on DNS-320 ? I tried a couple of them and I always end up with processor running at 100% and page loading only half of the contents. I noticed that the process clogging up the system is /ffp/php-cgi. I doubt there is a problem with the php script as the situation is the same regardless of the gallery I tried. Could it be problem with some settings of php, lighttpd etc. ? I would like to have this running on my DNS-320: http://sye.dk/sfpg/
    Any suggestions or experiences with php galleries would be much appreciated.

  5. Hi,
    I have instalated webserver, php, mysql. PHP Info function refers to the mail program with the location of “Path to sendmail /FFP/ sbin/sendmail -t -i”, but the program does not exist. It can be added into the system?

  6. Tell me, what’s the problem? I do everything according to instructions.
    Everything works until you reboot. After rebooting the nas on port 80 again comes built-in Web server.
    Dlink DNS-320

  7. Hey guys, just going through the guides for setting up my web server with php, found a problem in the guide, in my case at least.

    when i try to write the line:
    funpkg -i /ffp/pkg/additional/net-misc/curl-*.tgz

    it returns fatal error, hence the file does not excist..

    Went looking for the file and found out that it’s instead located in /ffp/pkg/additional/net-libs

    so in my case the following line did the job:
    funpkg -i /ffp/pkg/additional/net-libs/curl-*.tgz

    thanks again for the awesome effort guys 🙂

  8. Is there a way to get PHP 5.3.9 (rather than 5.2.17) on FFP on a DNS-325? I am trying to install OwnCloud3 and it requires 5.3 or above for the calendar. Any help is appreciated.

  9. Hi.
    Thanks for great tutorial.

    Just one note.
    On my installation (DNS-320) line
    funpkg -u /ffp/pkg/additional/net-misc/curl-*.tgz
    fails with “file not found”
    I search and found that curl package actualy is in
    /ffp/pkg/additional/net-libs/
    So the proper path is:
    funpkg -i /ffp/pkg/additional/net-libs/curl-*.tgz

    Without this package lighttpd.sh won’t start with php support

  10. Thanks very much for these awesome tutorials. I’ve been able to set up lighttpd and php using your tutorials. I’m using FFP 0.7, so things have changed a little, but I’ve still managed to get it up and running. Thanks again for all the work you have put in.

    1. How did you get it to work on 0.7??
      I’m having issued getting it to launch correctly. issues with fastcgi??

      Thanks,
      Steve

      1. Hmmm. It’s been a few months, so I can’t really remember, sorry. I don’t recall having any major issues though. I probably used Slacker to install the most recent version of the required packages.

        1. Ok, so I got lighttpd to run and I can browse the directory via URL. Now I’m trying to run eXtplorer, but when I launch I get the following:

          The extension ‘zlib’ couldn’t be found. Please make sure your version of PHP was built with ‘zlib’ support.

          I’ve been using funpkg to do my installs. If I run Slacker I get FATAL: Empty Cache

          Thoughts?

          1. Ah, this sounds familiar. I think I had to disable zlib support or download the zlib library. Sorry for being vague. I think I may have found something about zlib on the DNS-323 forum. There are some 0.7 packages on there in one of the threads.

  11. Wow!
    Excellent tutorial.
    I had already tried several other tutorials without success, but this one is perfect!!!
    This was really super helpful, everything worked exactly as shown.
    Thanks very much for spending time in this article.

  12. Hola. sigo todos los pasos para activar el fun plug 0.5 y todo ya anda bien pero cuando quiero activar el mysql me dice fatal error.. y veo que no esta en el paquete… quiero saber como puedo descargarlo y poder instalarlo y ponerlo en funcionamiento. gracias por su ayuda

  13. Hi there,

    I bought an 320l and used ffpg 0.7, I followed the installation of Lighttpd (with both NAS admin and webserver), php and mysql tutorials. However,php not work properly!

    Getting this erros

    server started
    2013-06-16 01:37:24: (mod_fastcgi.c.1732) connect failed: Connection refused on unix:/tmp/php-fastcgi.socket-0
    2013-06-16 01:37:24: (mod_fastcgi.c.3002) backend died; we’ll disable it for 1 seconds and send the request to another backend instead: reconnects: 0 load: 1
    2013-06-16 01:37:24: (mod_fastcgi.c.1103) the fastcgi-backend /ffp/bin/php-cgi failed to start:
    2013-06-16 01:37:24: (mod_fastcgi.c.1107) child exited with status 16 /ffp/bin/php-cgi
    2013-06-16 01:37:24: (mod_fastcgi.c.1110) If you’re trying to run your app as a FastCGI backend, make sure you’re using the FastCGI-enabled version.
    If this is PHP on Gentoo, add ‘fastcgi’ to the USE flags.
    2013-06-16 01:37:24: (mod_fastcgi.c.2819) ERROR: spawning fcgi failed.

  14. Hi,

    I have a similar problem to the person above. The lighttpd web server works fine, but I unable to get PHP working with it. I get the same errors (spawning FCGI failed) in the log file. I have a DNS-320 and the config file I am using with lightppd is lighttpd.conf-with-php

  15. Comments are closed as 0.5 is deprecated and no longer supported. Please do not use it on 0.7

    Best Regards,
    Uli

Comments are closed.