Difference between revisions of "MythWeb on Lighttpd"

From MythTV Official Wiki
Jump to: navigation, search
 
m (Page needed info about variable translation issue. MythTV 25.2 MythWeb 25.2 Lightttpd 1.4.31)
 
(22 intermediate revisions by 11 users not shown)
Line 1: Line 1:
Ok, this will describe a way to get MythWeb working on lighttpd. This is not going to be an elegant solution, but at least it should work.
+
{{outdated}}
 +
== MythWeb on Lighttpd ==
  
Ok, lets start.
+
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.
  
Your MythWeb installation wont work on lighttpd because lighttpd doesnt read/understands the .htaccess file which is needed to set the database settings in the webservers environment as well as specifing the rewrite rules which are needed for MythWeb.
+
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).
  
Lighttpd does in fact have support for rewriting and as well for enviroment settings, but the latter didnt work for me.
 
  
You can try yourself, just put the following into your lighttpd.conf
+
== Installing ==
  
# first you need to load the setenv module
+
Before we get anywhere we need to install the programs needed for this. Use your package manager or compile from source.
server.modules += ( mod_setenv )
 
  
# second you specify the the needed variables
+
=== PHP ===
setenv.add-environment = (
+
 
   "db_server" => "localhost",
+
PHP just needs the following options compiled as a minimum:
   "db_name" => "mythconverg",
+
 
  "db_login" => "mythtv_user",
+
<pre>
  "db_password" => "password"
+
./configure --enable-fastcgi --enable-discard-path --enable-force-redirect --with-mysql
 +
</pre>
 +
 
 +
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:
 +
 
 +
<pre>
 +
allow_url_fopen = On
 +
</pre>
 +
 
 +
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
 
)
 
)
  
That should work, but doesn't for me, so here is how I did it:
+
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:
 +
 
 +
<pre>
 +
fastcgi.server            = ( ".php" =>
 +
                              ( "localhost" =>
 +
                                (
 +
                                  "socket" => "/tmp/php-fastcgi.socket",
 +
                                  "bin-path" => "/usr/local/bin/php",
 +
                                  "broken-scriptfilename" => "enable"
 +
                                )
 +
                              )
 +
                          )
 +
</pre>
 +
 
 +
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:
 +
 
 +
<pre>
 +
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"
 +
                                                        )
 +
                                )
 +
                              )
 +
                              )
 +
</pre>
  
1. Open the file includes/init.php in your MythWeb directory with the editor of your choice.
+
Changing the values to match you install, e.g. if my login was Pompey the line would look like this
  
2. find the part of the file where the env variables are checked for the first time:
+
<pre>
 +
"db_login" => "Pompey",
 +
</pre>
  
// No database connection info defined?
+
MythWeb can now connect to the database.
if (empty($_SERVER['db_server']) || empty($_SERVER['db_name']) || empty($_SERVER['db_login'])) {
 
    require_once 'templates/_db_vars_error.php';
 
    exit;
 
}
 
...
 
  
3. enter the following ABOVE the part you found in step 2:
+
==== Mod_Rewrite ====
  
$_SERVER["db_server"] = "localhost";
+
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["db_name"] = "mythconverg";
 
$_SERVER["db_login"] = "mythtv_user";
 
$_SERVER["db_password"] = "password";
 
  
After that, mythweb should find and be able to connect to the database.
+
<pre>
 +
server.modules = (
 +
mod_rewrite
 +
)
 +
</pre>
  
But things will be still screwed up, cause there are no rewrite rules.
+
We now need to add the rule in the same config file that does the rewriting:
  
So, now we are going to specify those in the file lighttpd.conf.
+
<pre>
 +
url.rewrite = (
 +
  "^/mythweb(/tv.*|/music.*|/video.*|/weather.*|/settings.*|/status.*|/backend_log.*)$" =>  "/mythweb/mythweb.php/$1"
 +
)
 +
</pre>
  
# make sure the rewrite module is loaded
+
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:
server.modules += ( "mod_rewrite" )
 
  
#the actual rewrite rule
+
<pre>
 
url.rewrite = (
 
url.rewrite = (
"^(/tv.*|/video.*|/weather.*|/settings.*|/status.*)$" => "mythweb.php/$1"
+
  "^(/tv.*|/music.*|/video.*|/weather.*|/settings.*|/status.*|/backend_log.*)$" =>   "/mythweb.php/$1"
 +
)
 +
</pre>
 +
 
 +
 
 +
==== Default Page ====
 +
 
 +
As most of you know when you type in an address such as [http://www.immolo.net 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:
 +
 
 +
<pre>
 +
server.indexfiles    = ("index.php", "index.html", "index.htm", "default.htm")
 +
</pre>
 +
 
 +
And change to:
 +
 
 +
<pre>
 +
server.indexfiles    = ("mythweb.php", "index.php", "index.html", "index.htm", "default.htm")
 +
</pre>
 +
 
 +
Note : on my version (1.4.13), server.indexfiles should be replaced by index-file.names so it becomes :
 +
 
 +
<pre>
 +
index-file.names          = ("mythweb.php", "index.php", "index.html", "index.htm", "default.htm")
 +
</pre>
 +
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)
 +
 
 +
<pre>
 +
Fatal error: Call to a member function query() on a non-object in /var/www/localhost/htdocs/mythweb/includes/session.php on line 60
 +
</pre>
 +
 
 +
To fix this we need to apply a few lines of code which I found on the [http://forums.gentoo.org/viewtopic-p-4138309.html?sid=14fba9b62046cf3c4ad8c7027402bc62 Gentoo fourms]
 +
 
 +
<pre>
 +
 
 +
/**
 +
+ * 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();
 +
    }
 +
</pre>
 +
 
 +
The above code needs to go into mythtv/include/objects/Database.php around line 90 which is just under the following section:
 +
 
 +
<pre>
 +
/**
 +
* 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);
 +
    }
 +
</pre>
 +
 
 +
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 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
 +
<pre>
 +
// 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"];
 +
</pre>
 +
== 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:
 +
 
 +
<pre>
 +
server.modules = (
 +
mod_auth
 
)
 
)
 +
</pre>
 +
 +
We can then add the following rule to create the protected directory:
 +
 +
<pre>
 +
auth.backend              = "htpasswd"
 +
auth.backend.htpasswd.userfile = "/var/www/localhost/lighttpd.user.htpasswd"
 +
auth.require              = ( "/mythweb/" =>
 +
                              (
 +
                                "method"  => "basic",
 +
                                "realm"  => "mythweb",
 +
                                "require" => "valid-user"
 +
                              )
 +
                        )
 +
</pre>
 +
 +
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:
 +
 +
<pre>
 +
htpassword -c /var/www/localhost/lighttpd.user.htpassword <user>
 +
</pre>
 +
 +
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
 +
 +
<pre>
 +
auth.backend              = "htdigest"
 +
auth.backend.htdigest.userfile = "/var/www/localhost/lighttpd.user.htdigest"
 +
auth.require              = ( "/mythweb/" =>
 +
                              (
 +
                                "method"  => "digest",
 +
                                "realm"  => "mythweb",
 +
                                "require" => "valid-user"
 +
                              )
 +
                        )
 +
</pre>
 +
 +
then htdigest command instead of htpasswd
 +
 +
<pre>
 +
htdigest -c /var/www/localhost/lighttpd.user.htdigest <realm> <user>
 +
</pre>
 +
 +
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.
  
This rule assumes that the mythweb dir is the actual server root.
+
After you restart Lighttpd when you point your browser to MythWeb you should be asked for your username and password.
If it isn't you may want to create a virtual host like this:
 
  
$HTTP["host"] == "mythweb.some.domain" {
+
== ToDo ==
  server.document-root = "/var/www/mythweb"
 
  index-file.names = ("mythweb.php")
 
  url.rewrite-once = (
 
    "^(/tv.*|/video.*|/weather.*|/settings.*|/status.*)$" =>  "mythweb.php/$1"
 
  )
 
}
 
  
You may wonder if you could just take the rewrite rules from the .htaccess file and use those. Well, i thought so too, and the guys in #lighttpd said in should work, but it didnt...
+
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.
  
Anyways, here is what it should look like if you want to try it out anyway:
+
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.
  
url.rewrite-once = (
+
The layout of this guide maybe change if you have any ideas on a better feel please let me know.
  "^(css|data|images|js|themes|skins|[a-z_]+\.php)(/|$)" =>  "$0",
 
  "^(.+)$" => "mythweb.php/$1",
 
  "^(.*)$" => "mythweb.php"
 
)
 
  
The $0 is needed instead of the "-" according to #lighttpd... and thats the only difference.
+
[[Category:HOWTO]] [[Category:MythWeb]]

Latest revision as of 18:20, 20 September 2012

Time.png Outdated: The information on this page may no longer be relevant to the current release of MythTV, 30.0. 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.