Webserver with PHP and Streaming Webcam on Netgear WGT634u Running OpenWrt

Posted by Max Power | Posted in Netgear WGT634u | Posted on 17-11-2011-05-2008

0

In this post I will outline the process I used to setup a streaming usb webcam with a web server running php on a Netgear WGT634u running OpenWrt Backfire.

Webcam

First we will setup the webcam and make sure that we can view the stream on the lan.  Check out my other post (http://wp.yellowkazoo.com/?p=57) for details.  Note: When you setup the webcam make sure that you set it up on a different port than the one that you want the web server on.  I put the webcam on port 8081 and the web server on port 8080.  You can do this by editing the following file:

 /etc/config/mjpg-streamer
Config mjpg-streamer core
    option device "/dev/video0"
    option resolution "640x480"
    option fps "5"
    option port "8080"
    option enabled "true"

Web Server

Next we will install a web server,  I used lighttpd.

opkg update
opkg install lighttpd lighttpd-mod-cgi

Now lets edit the configuration file:

/etc/lighttpd/lighttpd.conf

Enable the mod_cgi module:

server.modules = (
    [..]
    "mod_cgi"
    [..]
}

Allow index.php as index:

index-file.names = ("index.html","default.html","index.htm","default.htm","index.php")

Set the server root directory:

server.document-root = "/www/pub_www/"

Set the server port:

server.port = 8080

Under CGI Module add:

cgi.assign = (".php => "/usr/bin/php")

Create a new folder for the document root:

mkdir /www/pub_www

Start lighttpd:

/etc/init.d/lighttpd start

Or have lighttpd start on startup:

/etc/init.d/lighttpd enable

PHP Server

Now lets setup the php server.  I installed PHP4 because it is a much smaller package.

opkg update
opkg install php4 php4-cgi

Edit the config file:

/etc/php.ini

Setup the root folder:

doc_root = /www/pub_www

To test the php setup we will create the file /www/pub_www/phpinfo.php with the following content:

<?php php phpinfo() ?>;

Now restart the server:

/etc/init.d/lighttpd restart

Browse to the phpinfo.php page (http://192.168.1.1:8080/phpinfo.php)

PHP Webcam Page

Now we will create a php page that includes the webcam stream.  Check out this post to see how I setup the webcam.

<?php
/* Usage: <img src="thisfile.php"> */
$server = "localhost"; // camera server
$port = 8081; // camera server port
$url = "/?action=stream"; // url on camera server
$fp = fsockopen($server, $port, $errno, $errstr, 30);
if( !$fp ){
    echo "$errstr ($errno)<br />\n";
}else{
    $urlstring = "GET ".$url." HTTP/1.0\r\n\r\n";
    fwrite( $fp, $urlstring );
    while( $str = trim( fgets( $fp, 4096 ) ) )header( $str );
    fpassthru( $fp );
    fclose( $fp );
}
?>

Firewall Setup

Once the server is all setup we will configure the firewall so that it is accessible from the wan.

config redirect
    option src wan
    option src_dport 80
    option dest lan
    option dest_ip 192.168.1.1
    option dest_port 8000
    option proto tcp
config rule
    option src wan
    option dest_port 8000
    option target ACCEPT
    option proto tcp

Now restart the firewall:

/etc/init.d/firewall restart

Write a comment