FRITZ!Box running as LAMP-server (Linux, Apache, MySQL and PHP)

Spread the love

FRITZ!Box 7360In this tutorial you will learn how to run a complete LAMP-configuration (Apache, MySQL and PHP) at your FRITZ!Box. In this article a ‘FRITZ!Box 7360’ is used. However you can follow the same steps for other types when it is supported by Freetz besides of that, there must be enough memory and CPU-power available in the FRITZ!Box. Freetz is used to create an image which contains a BusyBox version with CHROOT-support. When you have another way to give your box CHROOT-support, you can skip the Freetz-steps.

First you will learn how to set up your Freetz-build environment, configure Freetz and build your custom FRITZ!Box image. Next this image will be installed at your box. After that a ‘debootstrap’ is prepared at your Debian-based workstation. Here you can use the virtual machine which was configured while building Freetz or your own Debian/Ubuntu system. After that the second stage debootstrap is run at the FRITZ!Box itself. A minimal Debian (Wheezy) system will be configured at your USB-stick or USB-harddisk. Now you can start the CHROOTed-environment at the FRITZ!Box and install/configure Apache, MySQL and PHP. Creating and testing some PHP-scripts will conclude this tutorial. For most of these steps you do need a working Internet connection.

DISCLAIMER: I’m not responsible for any damage you cause at your FRITZ!Box or any service interruptions.

Reading assistance

Watch out, we use different environments to accomplish our goals. Please don’t get confused.

  • Debian based workstation
  • Freetz virtual machine, which is started at a Linux or MS Windows host using VirtualBox and can also be used as Debian based workstation. Always accessed via SSH or SCP.
  • FRITZ!Box webinterface (http://fritz.box:80)
  • Freetz webinterface (http://fritz.box:81)
  • Apache webserver in CHROOTed environment (http://fritz.box:83)
  • FRITZ!Box telnet
  • CHROOTed environment at FRITZ!Box, started within telnet session

Configure your Freetz-build-environment

I decided to build Freetz in a virtual machine running in VirtualBox, but you can build Freetz also at a native Linux-system.

  1. Download the freetz-linux VirtualBox image from:
    http://sourceforge.net/projects/freetz-linux/

    wget "http://downloads.sourceforge.net/project/freetz-linux/freetz-linux-1.3.1/freetz-linux-1.3.1.ova?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Ffreetz-linux%2F&ts=1384081012&use_mirror=netcologne" -O freetz-linux-1.3.1.ova
  2. Import virtual machine ‘freetz-linux’ into VirtualBox
    1. Start VirtualBox
    2. File->Import Appliance…
    3. Open Appliance…
    4. Select the OVA-file you downloaded: ‘freetz-linux-1.3.1.ova’
    5. Click next
    6. Click Import
    7. Select the imported virtual machine (freetz-linux) and press ‘Start’
  3. Login into console of virtual machine
    Username: freetz
    Password: freetz

    Hint: It’s a German keyboard mapping, so press the Y when you want to type a Z.

  4. Find the IP-address for eth0. It’s on the screen as welcome message or you can query it by executing the command:
    ifconfig eth0

    Notice: The virtual network interface card is configured in bridged mode and the IP-settings are obtained from your router by DHCP. Make sure your router is configured as DHCP-server.

  5. Grab your mouse pointer again by pressing the right CTRL-key.
  6. Optional: Now you can move the GUI of VirtualBox and the console of the virtual machine to another workspace, so they no longer bothers you.
  7. Login into the virtual machine by SSH. In my case this is:
    ssh freetz@192.168.178.21

    Password is: freetz

  8. Make sure you are running an up-to-date system by executing the command:
    sudo apt-get update && sudo apt-get upgrade -y && sudo reboot
  9. After reboot of virtual machine (some seconds to minutes) login again using SSH.

Build your Freetz based image

  1. Checkout Freetz
    svn co http://svn.freetz.org/branches/freetz-stable-2.0 freetz-stable-2.0
  2. Go to the Freetz-directory
    cd freetz-stable-2.0
  3. Configure Freetz
    make menuconfig

    Configure Freetz by using the arrow- and enter-keys. ‘Hardware type’ and ‘Firmware language’ must of course reflect your particular FRITZ!Box.

    • Level of user competence->Expert
    • Hardware type->7360
    • Firmware language->en – international
    • Firmware version->FRITZ!OS 05.5x
    • Other patches->
      — FREETZMOUNT
      –Automount filesystems
      — ext3
      — fat
    • Busybox applets->Applets->Coreutils->chroot

    Exit – Do you wish to save your new configuration ? -> Yes

  4. Start the make process.
    make
  5. Take a coffee
  6. In case you get the error: ‘image too big’, check the tips-section at the end of this article.
  7. After a while a brand new custom image for your FRITZ!Box is ready. Find it in:
    /home/freetz/freetz-stable-2.0/images
  8. Exit the SSH-session to the virtual machine (exit)

Flash the FRITZ!Box with the custom image

WARNING: Don’t power off your FRITZ!Box during these steps!

  1. Download your custom Fritzbox image from the virtual machine to your workstation. Execute at your workstation:
    scp freetz@192.168.178.21:/home/freetz/freetz-stable-2.0/images/* .

    Password: freetz

  2. Now access the webinterface of your FRITZ!Box using a browser:
    Point your browser to: http://fritz.box (and login)
  3. Select menu: Overview->System->Firmware Update
  4. Select ‘Firmware File’
  5. Click button: ‘Saving Settings’ Make sure you know where these settings are saved and store them at a safe location.
  6. Click ‘Browse’ and select your custom image
  7. Click ‘Start Update’
  8. After a while you will see this message in your browser:
    “Attention! – The specified file does not contain firmware released for this device by AVM.”
    Click ‘Continue Update’
  9. Now wait till the Info-LED at the FRITZ!Box is not blinking any more.
  10. Check the versions running at your FRITZ!Box:
    • Login into the FRITZ!Box using your browser: http://fritz.box
      Check in the right corner in the Overview pane the version: ‘FRITZ!OS 05.51’. Click it to check the firmware version: ‘111.05.51’.
    • You can access the Freetz webinterface in your browser at port 81 of the FRITZ!Box: http://fritz.box:81
      Username: admin
      Password: freetz
      Here you will find the Freetz version: ‘Freetz: 2.0-11219’ and again the firmware version: ‘111.05.51 rev25025’.

Congratulations you are running your custom FRITZ!Box image with built-in Freetz webinterface and chroot-support at this moment!

Create CHROOTed-environment

Now you are ready to create your Debian based chrooted-environment. To do so, execute these steps on a Debian based system (Debian/Ubuntu):

  1. apt-get install debootstrap
  2. Insert USB-stick in Debian-system
  3. Type the command: dmesg to find the device binding of the USB-stick. In my case this was: /dev/sdc Use this in the next steps.
  4. Format USB-stick as Ext3:
    mkfs.ext3 /dev/sdc
  5. Mount USB-stick
    sudo mkdir /mnt/usb
    sudo mount /dev/sdc /mnt/usb
  6. Put Debian-debootstrap on stick
    mkdir /mnt/usb/debian
    sudo debootstrap --arch=mips --foreign wheezy /mnt/usb/debian http://http.debian.net/debian/
  7. Umount USB-stick:
    sudo umount /mnt/usb/
  8. Remove USB-stick from Debian-system
  9. Insert the Ext3 formated USB-stick with first stage debootstrap into FRITZ!Box
  10. Telnet into FRITZ!Box
    telnet fritz.box
    Username: root
    Password: Use password which was set by webinterface at FRITZ!Box (port 80)
  11. Find where USB-stick is mounted: df
    Output contains i.e.: /var/media/ftp/uStor00
  12. Now start second stage of ‘debootstrap’ Use path to USB-stick which was found in previous step, supplemented with ‘debian’:
    export MY_CHROOT="/var/media/ftp/uStor00/debian"
    chroot ${MY_CHROOT} /debootstrap/debootstrap --verbose --second-stage
    
  13. Take your second coffee, this can take a while!
  14. Start the chrooted-environment:
    mount -t proc proc $MY_CHROOT/proc
    mount -t sysfs sysfs $MY_CHROOT/sys
    cp /proc/mounts $MY_CHROOT/etc/mtab
    chroot $MY_CHROOT /bin/bash
    

Next steps are executed from within the CHROOTed environment.

Update CHROOTed-environment

  1. Create a new file /etc/apt/sources.list containing these entries:
    deb http://ftp.nl.debian.org/debian stable main contrib non-free
    deb http://ftp.debian.org/debian/ wheezy-updates main contrib non-free
    deb http://security.debian.org/ wheezy/updates main contrib non-free
    
  2. Do the update:
    apt-get update && apt-get upgrade

Install Apache, MySQL and PHP into the CHROOTed-environment

  1. Install the packages:
    apt-get install apache2 php5 libapache2-mod-php5 mysql-server mysql-client php5-mysql

    Use as password for the MySQL root user: root. We will only allow database connections from localhost so this is safe enough. Installation of Apache2 will fail because port 80 is in use already. Later we will configure Apache so it will run at another port. Installation of MySQL will fail because of memory problems. In the next step we will disable InnoDB-storage engine and use the MyISAM as engine. So for now, ignore these errors and just continue the installation.

  2. Configure MySQL: Disable InnoDB and use MyISAM as storage engine. Create/edit the file local.cnf
    vi /etc/mysql/conf.d/local.cnf

    Add these lines to it:

    [mysqld]
    ignore-builtin-innodb
    default-storage-engine = myisam
    

    Now let MySQL finish the installation

    apt-get install mysql-server
    dpkg-reconfigure mysql-server-5.5
  3. Configure Apache
    As we told before you have to change the port of Apache from the default one: 80 to another one which is free, like 83. In the next steps we assume you use 83. Edit the files ‘ports.conf’ and ‘000-default’. Replace in these file the port 80 with 83.

    vi /etc/apache2/ports.conf
    NameVirtualHost *:83
    Listen 83
    
    vi /etc/apache2/sites-enabled/000-default
    <VirtualHost *:83>
  4. (Re)start apache using:
    /etc/init.d/apache2 restart
  5. Test the Apache webserver from your home-network. Use a browser to navigate to: http://fritz.box:83/ You must see a page containing the text: ‘It works!’
  6. Test PHP
    1. Create a new file:
      echo "<?php phpinfo(); ?>" > /var/www/test.php
    2. Access this file using your browser:
      http://fritz.box:83/test.php Now you must see a page starting with: ‘PHP Version’ and a lot of information about your Apache/PHP installation.
  7. Test MySQL + PHP
    1. Create a new file in /var/www/testDatabase.php containing these contents:
      <?php
      $con=mysqli_connect("127.0.0.1","root","root","mysql");
      if (mysqli_connect_errno($con)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      } else {
      $result = mysqli_query($con,"SELECT * FROM db");
      print "<pre>";
      while($row = mysqli_fetch_array($result)){
      print_r($row);
      }
      print "</pre>";mysqli_close($con);
      }
      ?>
    2. Access this file using your browser:
      http://fritz.box:83/testDatabase.php When everything runs smoothly, you must see now some information from table ‘db’ in database ‘mysql’.
  8. Configure the FRITZ!Box firewall
    When you would like to access the webserver from the Internet, you have to punch a hole in your firewall. We will open TCP port 83 for incoming traffic.Hint: When you don’t have any forward rule configured in your FRITZ!Box, create a dummy one and remove it after this step. In that case it is easier for you to add your custom forward-rule because you can lookup: ‘forwardrules’ in the firewall configuration and append your rule.’

    1. Create a forward rule using the webinterface of the FRITZ!Box: Internet->Permit Access->Port Forwarding
    2. Click button: ‘New Port Forwarding’
    3. Create a temporary entry.

    Create the ‘permanent’ forward from the public Internet port 83 to your Apache server which is running in the CHROOTed-environment. Do this by editing the file ar7.cfg by using the telnet connection to the FRITZ!Box.

    1. nvi /var/flash/ar7.cfg
    2. Find line: forwardrules =
    3. Add a line like this, please check the format of the file, keep using the same syntax (!):
      "tcp 0.0.0.0:83 0.0.0.0:83 0 # Chroot HTTP-Server",
    4. Reboot FRITZ!Box
  9. Start CHROOTed-environment – It’s good practice to put these lines in a script (chroot.sh) in the root-directory of the USB-stick so you only have to execute that script.
    export MY_CHROOT="/var/media/ftp/uStor00/debian"
    mount -t proc proc $MY_CHROOT/proc
    mount -t sysfs sysfs $MY_CHROOT/sys
    mount -t dev dev $MY_CHROOT/dev
    mount --bind /dev/pts $MY_CHROOT/dev/pts
    cp /proc/mounts $MY_CHROOT/etc/mtab
    chroot $MY_CHROOT /bin/bash
  10. Start servers
    • Apache server: /etc/init.d/apache2 start
    • MySQL server: /etc/init.d/mysql start
  11. Access HTTP-Server from the Internet or from inside your home network
  12. Create your files at /var/www

Congratulations your basic LAMP-configuration is ready!

Next step is to design a (dynamic) website using HTML, CSS, PHP and MySQL. You also have to fine tune your configuration (number of running Apache servers, memory, security). Unfortunately these things are out of scope of this article.

Tips

In this paragraph you will find some miscellaneous tips for the FRITZ!Box and Freetz:

      • Logging – When logged into the FRITZ!Box using telnet you can use these commands to receive some logging:
        cat /dev/debug
        dmesg
      • Recover FRITZ!Box – In case your box won’t boot any more, you can try to push a recover image from the Freetz-working-directory to your FRITZ!Box. This is without the MS Windows-recover tool. The computer containing the Freetz software must be configured so it is connected to the same subnet as the FRITZ!Box. Execute this command:
        make recover RECOVER=eva to push the recover image to the FRITZ!Box running a 2.6. For older boxes (kernel 2.4), execute the command:
        make recover RECOVER=adam.
      • After ‘making’ Freetz, I get the error (Fehler): “ERROR: kernel image is xxxxx bytes too big. See http://freetz.org/wiki/FAQ#Filesystemimagetoobig for details.” Solution is to remove some unneeded packages from your Freetz-build.
        Execute the command:
        make menuconfig and enable some settings like:

        • Removal patches
        • Remove AURA (AVM USB Remote-Architecture)
        • Remove help
        • Remove kids (usermand/contfiltd)

        Try again to build the Freetz image:
        make, hopefully it is sufficiently shrunken so it will fit at the FRITZ!Box.

      • Enable telnet – In case telnet is not enabled by default at your FRITZ!Box, you can use a to the box connected phone and dial:
        #96*7* this will start the telnet-daemon.
      • Capture network traffic in Wireshark format:
        http://fritz.box/html/capture.html

Read more

Leave a comment