MythWeb on Lighttpd

From MythTV Official Wiki
Revision as of 18:20, 20 September 2012 by Reubencrane (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Time.png Outdated: The information on this page may no longer be relevant to the current release of MythTV, 0.27.4. Please consider helping to update it.

MythWeb on Lighttpd

MythWeb provides a frontend for scheduling and managing recordings on your MythBox from a web browser located on any machine. In addition, it allows for changing key bindings and other settings.

This guide will provide you with a way to run MythWeb on Lighttpd rather then apache. Lighttpd has the benefit of faster page loading times for MythWeb, but comes at the expense of a longer setup time (which should be helped by this guide).


Installing

Before we get anywhere we need to install the programs needed for this. Use your package manager or compile from source.

PHP

PHP just needs the following options compiled as a minimum:

./configure --enable-fastcgi --enable-discard-path --enable-force-redirect --with-mysql

Gentoo would use the following USE Flags: cgi cli discard-path force-cgi-redirect mysql ssl pcre


Lighttpd

A default compile should pull everything needed in and the USE Flags for Gentoo would be: fastcgi mysql pcre php ssl


MythWeb

You need to copy the MythWeb files over to your web server's path. On some systems, that is /var/www/localhost/htdocs/ I also put MythWeb into a directory called mythweb and the rest of the guide will follow the layout of the path being /var/www/localhost/htdocs/mythweb/ and the address being http://localhost/mythweb, change to suit your needs.


Configuring

With everything installed we next need to configure our software for use.

PHP

Only one option here needs changing else you will be seeing the following error quite alot:

 Error at /var/www/localhost/htdocs/modules/weather/handler.php, line 96:
 file() [function.file]: URL file-access is disabled in the server configuration

You need to open your php.ini which in Gentoo's case is /etc/php/cgi-php5/php.ini and change the following:

allow_url_fopen = On

The above command allows URLs to be treated as files.


Lighttpd

There are many options in here that need changing to allow MythWeb to work.

fastcgi.server

MythWeb needs a php in cgi mode so we need to enable the module mod_fastcgi in the lighttpd.conf file as so:

server.modules = ( mod_fastcgi )

We need to configure fastcgi for PHP use so open the config file for it and leaving the parts which are already there as it may break you install we need to add the following line:

fastcgi.server             = ( ".php" =>
                              ( "localhost" =>
                                (
                                  "socket" => "/tmp/php-fastcgi.socket",
                                  "bin-path" => "/usr/local/bin/php",
                                  "broken-scriptfilename" => "enable"
                                )
                              )
                           )

Which should just be the broken-scriptfilename line

Important NOTE: except the last line, every line in the array must end with a comma, or else lighttpd won't start!

Lighttpd doesn't use .htaccess like Apache does so we need a workaround for access to the MythTV database.

Once again editing the fastcgi.server section place the following:

fastcgi.server             = ( ".php" =>
                              ( "localhost" =>
                                (
                                  "socket" => "/tmp/php-fastcgi.socket",
                                  "bin-path" => "/usr/local/bin/php",
                                  "broken-scriptfilename" => "enable",
                                  "bin-environment" => ( "db_server" => "localhost",
                                                         "db_name" => "mythconverg",
                                                         "db_login" => "mythtv",
                                                         "db_password" => "mythtv"
                                                        )
                                 )
                               )
                              )

Changing the values to match you install, e.g. if my login was Pompey the line would look like this

"db_login" => "Pompey",

MythWeb can now connect to the database.

Mod_Rewrite

MythWeb needs a rewrite rule to function properly so we need to enable the module mod_rewrite in the lighttpd.conf file as so:

server.modules = (
mod_rewrite
)

We now need to add the rule in the same config file that does the rewriting:

url.rewrite = (
   "^/mythweb(/tv.*|/music.*|/video.*|/weather.*|/settings.*|/status.*|/backend_log.*)$" =>   "/mythweb/mythweb.php/$1"
 )

The above rule is following my layout of being in the mythweb directory, If MythWeb was in the root of the web server '/' then we would use the following rule:

url.rewrite = (
   "^(/tv.*|/music.*|/video.*|/weather.*|/settings.*|/status.*|/backend_log.*)$" =>   "/mythweb.php/$1"
 )


Default Page

As most of you know when you type in an address such as http://www.immolo.net it really loads up the page http://www.immolo.net/index.php but MythWeb's main page is called mythweb.php so lets edit lighttpd.conf to also open mythweb.php as a default page so we can just type http://localhost/mythweb/ to access MythWeb

Find the following line:

server.indexfiles    = ("index.php", "index.html", "index.htm", "default.htm")

And change to:

server.indexfiles    = ("mythweb.php", "index.php", "index.html", "index.htm", "default.htm")

Note : on my version (1.4.13), server.indexfiles should be replaced by index-file.names so it becomes :

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

Lighttpd is now configure for MythWeb usage but we need to restart Lighttpd for all the changes to take place.

MythWeb

When using MythWeb on Lighttpd you always get the following error: (Note: this is also applicable if using MythWeb on Apache)

Fatal error: Call to a member function query() on a non-object in /var/www/localhost/htdocs/mythweb/includes/session.php on line 60

To fix this we need to apply a few lines of code which I found on the Gentoo fourms


/**
+ * Destructor to save the session  before the database connection dies
/**/
    function __destruct() {
        // Restore global db object reference for session write
        global $db;
        $db = $this;
        // Ask session to write and close now, before we lose the db object forever
        session_write_close();
    } 

The above code needs to go into mythtv/include/objects/Database.php around line 90 which is just under the following section:

/**
 * Legacy constructor to catch things that the abstract classification won't
/**/
    function Database() {
        trigger_error('The Database class should never be created as an object.  Use Database::connect() instead.', E_USER_ERROR);
    }

After saving you should notice the error has gone from all the pages and now MythWeb fully works as it would with Apache.

If Mythweb doesn't display correctly

Symptom:

Page loads without error and just shows two "MythTV" links to ~/mythweb/root_url.

Cause:

I found at http://en.gentoo-wiki.com/wiki/MythWeb there is a known variable translation issue.

Solution:

Edit mythweb/includes/init.php and add these lines near the top

// Define db access for Lighttpd.
$_SERVER["db_server"] = $_SERVER["DB_SERVER"];
$_SERVER["db_name"] = $_SERVER["DB_NAME"];
$_SERVER["db_login"] = $_SERVER["DB_LOGIN"];
$_SERVER["db_password"] = $_SERVER["DB_PASSWORD"];

Secure MythWeb on Lighttpd

You sometimes hear stories of people gaining access to other peoples MythTV setup via the Internet so lets look into ways of securing MythWeb but still leaving access to MythWeb from the outside in case we ever need to access it from work or from a friends (or show off :D).

Auth (Password)

Nice and simple, lets put an username and password on the directory so then only we can access it.

In the lighttpd.conf uncomment the mod_auth so it looks like this:

server.modules = (
mod_auth
)

We can then add the following rule to create the protected directory:

auth.backend               = "htpasswd"
auth.backend.htpasswd.userfile = "/var/www/localhost/lighttpd.user.htpasswd"
auth.require               = ( "/mythweb/" =>
                               (
                                 "method"  => "basic",
                                 "realm"   => "mythweb",
                                 "require" => "valid-user"
                               )
                        )

As you can see, we are using htpasswd (an Apache based tool) which on Gentoo can be installed with the app-admin/apache-tools package but other distributions will need to check the package they need.

with htpasswd installed you now need to run the following command:

htpassword -c /var/www/localhost/lighttpd.user.htpassword <user>

The more secure digest method can also be used which doesn't send plain text passwords. Uncomment mod_auth like above and use this in mod_auth.conf

auth.backend               = "htdigest"
auth.backend.htdigest.userfile = "/var/www/localhost/lighttpd.user.htdigest"
auth.require               = ( "/mythweb/" =>
                               (
                                 "method"  => "digest",
                                 "realm"   => "mythweb",
                                 "require" => "valid-user"
                               )
                        )

then htdigest command instead of htpasswd

htdigest -c /var/www/localhost/lighttpd.user.htdigest <realm> <user>

Chaning <user> to the username you desire. You will next be asked to type a password which I'm sure you don't need me to say needs to be secure.

After you restart Lighttpd when you point your browser to MythWeb you should be asked for your username and password.

ToDo

I still need to add a section on using SSL with MythWeb on Lighttpd and any ideas are welcome place them in the Talk page or just add it yourself.

I also wrote this guide late at night and 3 days after the birth of my son (hence the lack of sleep) so if you find spelling or grammer mistakes please correct them.

The layout of this guide maybe change if you have any ideas on a better feel please let me know.