Installing and Using PHP

The Onion Omega runs a webserver called uhttpd. It does the heavy lifting for the Omegas built-in running PHP!

We’ve already set up some basic examples of getting PHP to run, including proof-of-concept scripts and installation instructions. This tutorial walks you through the steps of getting these to work.

PHP example and the Onion Omega

All you need is a little imagination on how you can use and abuse this with you Onion Omega.

Here are some ideas to get your brain noodling:

  1. You could post updates to Thingspeak.com
  2. And push updates to PushBullet, so events appear across multiple devices such as a web browser, your iphone, ipad and/or droid
  3. Build a security alarm that takes snapshots using a USB webcam

Installing PHP

Let’s get started

First we need to install the required packages. You can use the console, or ideally via SSH:

opkg update
opkg install php7 php7-cgi

This installs the required PHP packages.

Optionally you can also install the CLI version of PHP using the command below. This allows you to run PHP scripts from the commandline using “php-cli scriptname.php”

opkg install php7-cli

Using PHP

In order to make use of our installed PHP package, we need to do a couple of things: * Edit the config file for uhttpd. * Restart the server to update the configuration. * Create a PHP script and put in where uhttpd can find it. * Start making awesome things!

Editing the Configuration

We recommend you do the editing with Omega’s built-int text editor - vi. The alternative is you can copy the file, edit it on your own computer, then copy it back.

If you have never used vi, know that it does not work like other text editors. We recommend you to brush up on the basics before continuing. If you decide to forge on, remember this: hit ESC and type :q! to quit without saving!

Without further ado, let’s get to configuring our PHP installation.

To use vi to edit, simply run the following:

vi /etc/config/uhttpd

You’ll see something like this:

config uhttpd 'main'
        list listen_http '0.0.0.0:80'
        list listen_http '[::]:80'
        list listen_https '0.0.0.0:443'
        list listen_https '[::]:443'
        option redirect_https '1'
        option home '/www'
        option rfc1918_filter '1'
        option max_requests '3'
        option max_connections '100'
        option cert '/etc/uhttpd.crt'
        option key '/etc/uhttpd.key'
        option cgi_prefix '/cgi-bin'
        option script_timeout '60'
        option network_timeout '30'
        option http_keepalive '20'
        option tcp_keepalive '1'
        option ubus_prefix '/ubus'

To let uhttpd know where our PHP is and what pages to send, add the following lines to the last line of that block of text:

        list interpreter ".php=/usr/bin/php-cgi"
        option index_page 'index.php'

Press ESC on keyboard, and then :wq & Enter to save changes. If you’re editing this on your own computer, simply copy it back to /etc/config/uhttpd and make sure to overwrite it.

Every time the configuration is edited we need to restart the web server if we want the changes to take effect. We do this by running this command:

/etc/init.d/uhttpd restart

And that’s it, the uhttpd server is now capable of parsing PHP files!

Testing the Installation

Create a test file & directory by executing the following command:

mkdir /www/php
cd /www/php
vi index.php

And paste this code into the file & save:

<?php
  echo "hello world, I'm PHP running on my Onion Omega!";
?>

Press ‘ESC’ on keyboard, and then ‘:wq’ & ‘Enter’ to save changes.

Now open your web browser and go to http://omega-ABCD.local/php/

Note: If you receive a directory or file permissions issue, run the following command and try again.

chmod -R 755 /www/php

Going Further

We’ve included some links and projects to do with PHP below to give you some inspiration and experience to make Really Cool Stuff!

Editing php.ini

By default, the PHP configuration file is installed under /etc/php.ini.

You can edit the php.ini file by running this command:

vi /etc/php.ini

Community Resources

Ringmaster has kindly provided a PHP Helper for GPIO. You can read more about this in the community thread.