Running PHP on uHTTPd on GL-AR150

It is possible to run PHP under CGI within the OpenWRT default uHTTPd singlethreated webserver.

In the basis you can follow the instructions within the article “Set up a LAMP stack on OpenWrt” on https://wiki.openwrt.org/doc/howto/http.lamp and “PHP” on https://wiki.openwrt.org/doc/howto/php.

You can follow the instructions at the sections:

Additional PHP settings can be edited within /etc/php.ini. I use "nano" for that (can be installed with "opkg install nano").

Then you can test PHP by creating a very tiny PHP script, by the followin g the instructions at the section “Testing PHP” at Set up a LAMP stack on OpenWrt [Old OpenWrt Wiki]

Then I got an issue with setting up the timezone correctly. I live within the timezone “Europe/Amsterdam”, but PHP told me on the phpinfo-page that the timezone isn’t recognized. Even UTC+1 wasn’t recognized.
Luckily after a Google search I found this page explaining the solution: http://forum.arduino.cc/index.php?topic=221261.msg1608848#msg1608848
I had to install timezone configuration into OpenWRT. After that PHP did recognize the correct timezone setting.

Then I wanted to test whether my other scripts can be translateded into PHP and whether PHp can drive my Blink1 LED’s and GL-AR150 onboard LED’s: yes, it’s possible!

I’ll place the scripts in seperate reply’s to maintain the overview. The forum doesn’t support tabs, so when this becomes a issue please mail me at one of the mailadresses at http://www.thebigsite.nl/?p=39. The form currently doesn’t work!

Script to test whether PHP works and to display all PHP settings. You can save this for example as “phpinfo.php”.

<?php
phpinfo();
?>

My test-script to see whether my Blink1 led can be driven by a PHP page. I saved it as “blink1.php”.

<?php
if($_GET["c"]=="red"){
exec("/data/local/blink1/blink1-tool --red",$outputvalue);
?>
Executed &quot;red&quot;:
<pre>
<?=print_r($outputvalue); ?>
</pre>
<?php
}
else if($_GET["c"]=="green"){
exec("/data/local/blink1/blink1-tool --green",$outputvalue);
?>
Executed &quot;green&quot;:
<pre>
<?=print_r($outputvalue); ?>
</pre>
<?php
}
else{
?>
Unrecognized color or color not chosen.
<?php
}
?>
<hr />
<p>Please select a color:</p>
<p><a href="?c=red" style="color: red">Red</a> <a href="?c=green" style="color: green">Green</a></p>

My test-script to see whether PHP can be used to drive the GL-AR150 onboard LED’s. I saved it as “morsecode.php”.

I have based this script on my topic “Doing funny things with the onboard leds of the GL-AR150 :slight_smile:
"Important to know that this is a very dangerous script when you have connected your GL-AR150 directly to the internet without any other protection. The same for big networks like the network of your employer. Someone could execute naste commands on your GL-AR150 when abusing this script!

<html>
<head>
<title>GLi GL-AR150 PHP Morse-code</title>
</head>

<body>
<h1>GLi GL-AR150 PHP Morse-code</h1>
<form method="post">
<table>
<tr>
<td>UCI Led:</td>
<td><input type="text" name="uciled" value="<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
echo $_POST["uciled"];
}
else{
echo "@led[1]";
}
?>" size="30" /></td>
</tr>
<tr>
<td>System Led:</td>
<td><input type="text" name="systemled" value="<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
echo $_POST["systemled"];
}
else{
echo "gl_ar150:wlan";
}
?>" size="30" /></td>
</tr>
<tr>
<td>Morsecode:</td>
<td><input type="text" name="morsecode" value="<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
echo $_POST["morsecode"];
}
else{
echo "SOS";
}
?>" size="30" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submitbutton" value="Morse submit"><input type="submit" name="submitbutton" value="Restore netdev"></td>
</tr>
</table>
</form>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
?>
<hr>
<h2>Output</h2>
<?php
if($_POST["submitbutton"]=="Morse submit"){
?>
<h3>Morse</h3>
<h4>Setup led</h4>
<?php
exec("uci set system.".$_POST["uciled"].".trigger=morse",$output);
?>
<pre><?=print_r($output); ?></pre>
<h4>Commit configuration</h4>
<?php
exec("uci commit system",$output);
?>
<pre><?=print_r($output); ?></pre>
<h4>Restart LED daemon</h4>
<?php
exec("/etc/init.d/led start",$output);
?>
<pre><?=print_r($output); ?></pre>
<h4>Send morsecode</h4>
<?php
exec("echo \"".$_POST["morsecode"]."\" > /sys/class/leds/".$_POST["systemled"]."/message",$output);
?>
<pre><?=print_r($output); ?></pre>
<?php
}
else if($_POST["submitbutton"]=="Restore netdev"){
?>
<h3>Morse</h3>
<h4>Setup led</h4>
<?php
exec("uci set system.".$_POST["uciled"].".trigger=netdev",$output);
?>
<pre><?=print_r($output); ?></pre>
<h4>Commit configuration</h4>
<?php
exec("uci commit system",$output);
?>
<pre><?=print_r($output); ?></pre>
<h4>Restart LED daemon</h4>
<?php
exec("/etc/init.d/led start",$output);
?>
<pre><?=print_r($output); ?></pre>
<?php
}
else{
?>
<p>Unrecognized command</p>
<?php
}
}
?>
</body>
</html>

 

php performance should be pretty good, right?

For now it looks like runnig smoothly, but I didn’t run a stressfull PHP-script.

Also the GL-AR150 has very little memory to store PHP-websites and databases on.
Since it has only 1 USB-port and I already use this port for my Blink1 LED and don’t want to use a space consuming USB-hub my GL-AR150 will never run heavy PHP-scripts. :stuck_out_tongue:

Maybe an idea for a new version of a small GLi router? The ability to add a Micro-SD-card? Would be nice if the card can be populated as part of the system where MySQL writes. Although, actually USB-memory-sticks are better for the tons of read/writes of MySQL.

For Micro-SD card, you can use our big router: GL-AR300 and GL-AR750.

For small router, GL-MT300A also support MicroSD card by using a small daughter board. And it has more powerful CPU and RAM.

The GL-MT300A looks like not having the ability to add an PoE board? And the GPIO solder points are on the left instead of top?
Also I can’t find an option to buy. Not that I want to buy it right now (pretty broke), but just for the case when I have the money for it.

Knowing what the GL-AR150 is capable to the GL-AR300 and GL-MT750 are pretty interesting. But those 2 models don’t fit my original needs: very small and handy to take with you everywhere. But I will think about it in the future. :wink: