A Working Sample Module

So far, we have covered how to build a very light weight PHP application stack with very few dependencies. But, we have not done anything worthwhile, in terms of application logic.

For our sample module we will quickly go over how to start and stop Apache. This should demonstrate two key issues:

  1. The application has permissions to run root privileged system programs
  2. The application is still accessible even if the system's Web server is off

The module will simply consist of only one service file. On the previous page, you downloaded a package with two modules in it. If you installed the modules into "src/modules" you should have the file “src/modules/main/main.php”.

The skeletal structure of the file looks like this:

class Cgn_Service_Main_Main extends Cgn_Service {

  function mainEvent($req, &$t) {    }

  function stopStartEvent($req, &$t) {   }

  function _loadServiceForm($values=array()) {	}
}

The main event displays the an input form which is made in the _loadServiceForm method. The main event also checks the status of apache by executing “/etc/init.d/httpd extendedstatus”. The stopStartEvent reads the requested form value and either starts or stops Apache. The _loadServiceForm simply creates a form object, it is not called from the client like the mainEvent and stopStartEvent methods.

Point your browser to http://localhost:8443/ again and try to start and stop your own Apache instance. Hopefully, everything works and you can start and stop your server. Make sure you launch nanoweb as root, of course.

Closing Thoughts

If you've followed the code examples on your own up to this point, congratulations! You are a trooper. If not, don't worry, all the code is available from SVN here.

svn co http://niftyphp.svn.sourceforge.net/niftyphp/niftyserver/tags/tutorial-p1 niftyserver

This is just the start of a virtual host control panel. We have not covered the basics of the logic necessary to manage multiple config files on multiple platforms along with different users and their permissions. But, hopefully you now have a grounding in how to build a deployable PHP Web stack with few dependencies on system software as well as root privileges.

In the next installment, we will start to look at building more complex logic to handle virtual hosts and Apache config files.

More Ideas

This type of embedded server is not limited to virtual host control panels. Some people might not think that PHP is a good choice for building desktop applications, but you could use this project as a base to build a desktop hardware configuration utility for Linux. Being allowed to "modprobe" could lead to a wireless network configuration tool. With the permissions to rewrite any configuration files on the system you could write a slick looking, AJAX powered firewall configuration tool as well.

SSL

Important note: there is no SSL support in nanoweb at the moment. This should not be deployed in a production environment 'as-is'. You can find documentation on adding SSL support with the program “stunnel” and running nanoweb under xinetd. Nanoweb 2.3.0 should have built-in SSL support by utilizing the native socket functions in PHP5.