Virtual host with mod_perl

Virtual host with mod_perl

am 20.03.2008 18:50:55 von Valerio

Hi all,

I am new of this mailing list.
I spent the last 3 days studying mod_perl and apache: I am trying to
configure dynamic virtual hosts on apache.
I need to query a db to get the real filesystem location of a virtual
server hosted.
(I query the db using the requested hostname)

I have configured correctly (I suppose) apache conf,

ServerName %0
VirtualDocumentRoot /srv/www/vhosts/%0/
HostnameLookups Off
UseCanonicalName Off
VirtualScriptAlias "/srv/www/vhosts/%0/cgi-bin/"

Options None
AllowOverride None
Order allow,deny
Allow from all



but now I need to change the path served by apache with the real path
where the requested page is hosted.
(for example /srv/www/vhosts/www.server1.com/index.html to
/mnt/user1/www/index.html)

Can someone give me some guidelines about the handlers to use in
mod_perl and some ideas about the way to do it?

Thank you a lot.

Valerio Bontempi

Re: Virtual host with mod_perl

am 20.03.2008 19:54:18 von roberto

--EuxKj2iCbKjpUGkD
Content-Type: text/plain; charset=iso-8859-1
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Thu, Mar 20, 2008 at 06:50:55PM +0100, Valerio wrote:
> Hi all,
>=20
> I am new of this mailing list.
> I spent the last 3 days studying mod_perl and apache: I am trying to
> configure dynamic virtual hosts on apache.
> I need to query a db to get the real filesystem location of a virtual
> server hosted.
> (I query the db using the requested hostname)
>=20
Why not just configure your system such that the requested hostname
corresponds to its location on the filesystem. It seems like you are
doing to much work if you need to query a database.

Regards,

-Roberto

--=20
Roberto C. S=E1nchez
http://people.connexer.com/~roberto
http://www.connexer.com

--EuxKj2iCbKjpUGkD
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFH4rLa5SXWIKfIlGQRApHoAJ9R8uq3r7q/LytbnM8vuNo6HEg1xACf fDp6
UN/6ED5mGa7ap4hiCUtH1L4=
=JN2S
-----END PGP SIGNATURE-----

--EuxKj2iCbKjpUGkD--

Re: Virtual host with mod_perl

am 21.03.2008 09:14:48 von Valerio

2008/3/20, Roberto C. S=E1nchez :
> On Thu, Mar 20, 2008 at 06:50:55PM +0100, Valerio wrote:
> > Hi all,
> >
> > I am new of this mailing list.
> > I spent the last 3 days studying mod_perl and apache: I am trying to
> > configure dynamic virtual hosts on apache.
> > I need to query a db to get the real filesystem location of a virtual
> > server hosted.
> > (I query the db using the requested hostname)
> >
>
> Why not just configure your system such that the requested hostname
> corresponds to its location on the filesystem. It seems like you are
> doing to much work if you need to query a database.
>
> Regards,
>
> -Roberto
>
>
> --
> Roberto C. S=E1nchez
> http://people.connexer.com/~roberto
> http://www.connexer.com
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.6 (GNU/Linux)
>
> iD8DBQFH4rLa5SXWIKfIlGQRApHoAJ9R8uq3r7q/LytbnM8vuNo6HEg1xACf fDp6
> UN/6ED5mGa7ap4hiCUtH1L4=3D
> =3DJN2S
> -----END PGP SIGNATURE-----
>
>


We need a database backend because we need to develop a big hosting
platform, where each user have one or more domains registered and
linked to his account and we may also need different configurations
for each user.
So we think to create these configurations on the fly, using mod_perl
and a database backend.

Re: Virtual host with mod_perl

am 21.03.2008 14:41:04 von aw

I believe I saw an example of just that kind of stuff in one of the
books about Apache and/or mod_perl.
Maybe you want to check "the Mod_perl Developers Cookbook" or "Practical
Mod_perl". I don't have them with me right now, so I cannot tell you
which one for sure. They are excellent books anyway.
If I remember correctly, the title of the section was something like
"mass hosting with mod_perl".
Google is your friend. Try searching for : modperl +"mass hosting"

André

Valerio wrote:
[...]

>> > I spent the last 3 days studying mod_perl and apache: I am trying to
>> > configure dynamic virtual hosts on apache.
[...]

Re: Virtual host with mod_perl

am 21.03.2008 17:59:57 von Valerio

I will check the books you have suggested.
:-)

Thanks a lot

Valerio

2008/3/21, Andr=E9 Warnier :
> I believe I saw an example of just that kind of stuff in one of the
> books about Apache and/or mod_perl.
> Maybe you want to check "the Mod_perl Developers Cookbook" or "Practical
> Mod_perl". I don't have them with me right now, so I cannot tell you
> which one for sure. They are excellent books anyway.
> If I remember correctly, the title of the section was something like
> "mass hosting with mod_perl".
> Google is your friend. Try searching for : modperl +"mass hosting"
>
> Andr=E9
>
> Valerio wrote:
> [...]
>
>
> >> > I spent the last 3 days studying mod_perl and apache: I am trying =
to
> >> > configure dynamic virtual hosts on apache.
>
> [...]
>

Re: Virtual host with mod_perl

am 28.03.2008 13:30:49 von Valerio

Hi all,

reading along internet the huge amount of docs found about mod_perl
and mass hosting, I didn't find examples or solutions for what I am
trying to do: I found the way to realize a dynamic vhost conf file,
but I understood that this conf will be generated during startup.
Instead I need a solution that allow adding a vhost to the database
without need of restarting apache.
Does anyone know how I can do it?
Can I modify vhost configuration using mod_perl handlesr? If so, which
kind of handler can I use?

thanks a lot

Valerio


2008/3/21, Andr=E9 Warnier :
> I believe I saw an example of just that kind of stuff in one of the
> books about Apache and/or mod_perl.
> Maybe you want to check "the Mod_perl Developers Cookbook" or "Practical
> Mod_perl". I don't have them with me right now, so I cannot tell you
> which one for sure. They are excellent books anyway.
> If I remember correctly, the title of the section was something like
> "mass hosting with mod_perl".
> Google is your friend. Try searching for : modperl +"mass hosting"
>
> Andr=E9
>
> Valerio wrote:
> [...]
>
>
> >> > I spent the last 3 days studying mod_perl and apache: I am trying =
to
> >> > configure dynamic virtual hosts on apache.
>
> [...]
>

Re: Virtual host with mod_perl

am 28.03.2008 16:00:23 von Perrin Harkins

On Fri, Mar 28, 2008 at 8:30 AM, Valerio wrote:
> Instead I need a solution that allow adding a vhost to the database
> without need of restarting apache.

This is not usually done with mod_perl because there are
lighter-weight solutions available. There's some info on vhosts with
mod_rewrite and mod_vhost_alias here:
http://httpd.apache.org/docs/2.0/vhosts/mass.html

You can find apache modules built specifically for dynamically pulling
vhosts from a database here:
http://modules.apache.org/

You certainly could do this from mod_perl, but I wouldn't.

- Perrin

Re: Virtual host with mod_perl

am 28.03.2008 16:36:50 von Ryan Gies








I'm curious, how would this be
possible with mod_perl?  The below will fail as:


$r->add_config() has failed: <VirtualHost not allowed here

and when size="-1">$r->add_config face="sans-serif"> is changed to $r->server->add_config size="-1"> it will fail as:

Can't run '$s->add_config' after server startup



In /etc/hosts


127.0.0.1	local.example.com

In httpd.conf


PerlInitHandler Apache::Handlers::MyInitHandler

In
Apache/Handlers/MyInitHandler.pm:


package Apache::Handlers::MyInitHandler;
use strict;
our $VERSION = 0;

use Apache2::RequestUtil ();
use Apache2::Const -compile => qw(DECLINED);

sub handler {
my $r = shift;
$r->add_config([
'<VirtualHost *:80>',
'ServerName local.example.com',
'DocumentRoot /var/www/example.com/htdocs',
'<Directory /var/www/example.com/htdocs>',
'Order allow,deny',
'Allow from all',
'AllowOverride None',
'Options None',
'</Directory>',
'</VirtualHost>',
]);
Apache2::Const::DECLINED;
}

1;



Perrin Harkins wrote:
cite="mid66887a3d0803280800h183ecerd5c2fcb921921bed@mail.gma il.com"
type="cite">
On Fri, Mar 28, 2008 at 8:30 AM, Valerio  wrote:


 Instead I need a solution that allow adding a vhost to the database
without need of restarting apache.



This is not usually done with mod_perl because there are
lighter-weight solutions available. There's some info on vhosts with
mod_rewrite and mod_vhost_alias here:


You can find apache modules built specifically for dynamically pulling
vhosts from a database here:


You certainly could do this from mod_perl, but I wouldn't.

- Perrin



Re: Virtual host with mod_perl

am 28.03.2008 16:46:37 von Perrin Harkins

On Fri, Mar 28, 2008 at 11:36 AM, Ryan Gies wrote:
> I'm curious, how would this be possible with mod_perl?

I'd probably use mod_vhost_alias and make a mod_perl handler that just
checks whether the current IP or hostname is legit.

- Perrin

Re: Virtual host with mod_perl (off-topic)

am 28.03.2008 16:47:10 von Ryan Gies

Ahem, sorry folks about the HTML email

Ryan Gies wrote:
> I'm curious, how would this be possible with mod_perl? The below will
> fail as:
>
> $r->add_config() has failed: >
> and when $r->add_config is changed to $r->server->add_config it will
> fail as:
>
> Can't run '$s->add_config' after server startup
>
>
> In /etc/hosts
>
> 127.0.0.1 local.example.com
>
> In httpd.conf
>
> PerlInitHandler Apache::Handlers::MyInitHandler
>
> In Apache/Handlers/MyInitHandler.pm:
>
> package Apache::Handlers::MyInitHandler;
> use strict;
> our $VERSION = 0;
>
> use Apache2::RequestUtil ();
> use Apache2::Const -compile => qw(DECLINED);
>
> sub handler {
> my $r = shift;
> $r->add_config([
> '',
> 'ServerName local.example.com',
> 'DocumentRoot /var/www/example.com/htdocs',
> '',
> 'Order allow,deny',
> 'Allow from all',
> 'AllowOverride None',
> 'Options None',
> '
',
> '
',
> ]);
> Apache2::Const::DECLINED;
> }
>
> 1;
>
>
> Perrin Harkins wrote:
>> On Fri, Mar 28, 2008 at 8:30 AM, Valerio wrote:
>>
>>> Instead I need a solution that allow adding a vhost to the database
>>> without need of restarting apache.
>>>
>>
>> This is not usually done with mod_perl because there are
>> lighter-weight solutions available. There's some info on vhosts with
>> mod_rewrite and mod_vhost_alias here:
>> http://httpd.apache.org/docs/2.0/vhosts/mass.html
>>
>> You can find apache modules built specifically for dynamically pulling
>> vhosts from a database here:
>> http://modules.apache.org/
>>
>> You certainly could do this from mod_perl, but I wouldn't.
>>
>> - Perrin
>>

Re: Virtual host with mod_perl

am 28.03.2008 17:14:46 von Valerio

Hi all,

thank you so much for you help :)

I will try to use other solutions a part from mod_perl
(mod_vhost_alias for example)

but I managed to do quite what I want by this way:


package My::Trans;
use Apache2::Const -compile => qw(OK DECLINED);
use DBI;
use Data::Dumper;
sub handler {
my $r = shift;
my $url = $r->uri();
my $rlog = $r->log;
my $db_name = 'xxx';
my $db_host = 'xxx';
my $db_port = 'xxx';
my $db_user = 'xxx';
my $db_pw = 'xxx';
my $dsn = "DBI:mysql:database=$db_name;host=$db_host;port=$db_port";
our $dbh = DBI->connect($dsn, $db_user, $db_pw);
my $hostname = $r->hostname();
my $sql = "select vpath from vhosts where vhost = '$hostname'";
my $sth = $dbh->prepare($sql);
$sth->execute;
$row=$sth->fetchrow_hashref;
$sth->finish;
$r->document_root($row->{vpath});
$r->filename($row->{vpath}.'index.html');
return Apache2::Const::OK;
}


PerlModule ModPerl::Registry
PerlTransHandler My::Trans


It seems to work fine, Apache is serving me the right page.
I'm on the right way?
Another question:
I set both document_root and filename: can I set just only the
filename or the document-root or I have to set both them?

Valerio

2008/3/28, Perrin Harkins :
> On Fri, Mar 28, 2008 at 11:36 AM, Ryan Gies wrote:
> > I'm curious, how would this be possible with mod_perl?
>
>
> I'd probably use mod_vhost_alias and make a mod_perl handler that just
> checks whether the current IP or hostname is legit.
>
>
> - Perrin
>

Re: Virtual host with mod_perl

am 28.03.2008 18:07:30 von torsten.foertsch

On Fri 28 Mar 2008, Valerio wrote:
> I set both document_root and filename: can I set just only the
> filename or the document-root or I have to set both them?

filename is just enough. You need to set docroot only if you have CGI scripts
or similar that use it. You can also set only docroot and return DECLINED.
The default trans handler then does the rest.

You may also want to cache your DB lookups at least for the duration of the
request. There are subrequests (when a directory index is created or mod_cgi
computes PATH_TRANSLATED or similar). Each one of those would repeat the DB
lookup otherwise.

sub handler {
my $r=shift;

my $vpath;
if( $r->prev ) { # handle subrequests
$vpath=$r->prev->notes->{vpath};
} elsif( $r->main ) { # handle internal redirects (ErrorDocument)
$vpath=$r->main->notes->{vpath};
} else {
$vpath=db_lookup(...);
$r->notes->{vpath}=$vpath;
}

$r->filename($vpath.$r->uri);
return Apache2::Const::OK;
}

Torsten

Re: Virtual host with mod_perl

am 28.03.2008 18:41:35 von Valerio

Hi Torsten,

if my page contains relative paths (for example for images), I suppose
that I have to set document root and return DECLINED. It is correct?
It seems to work using this solution

thank you

Valerio



2008/3/28, Torsten Foertsch :
> On Fri 28 Mar 2008, Valerio wrote:
> > I set both document_root and filename: can I set just only the
> > filename or the document-root or I have to set both them?
>
>
> filename is just enough. You need to set docroot only if you have CGI scripts
> or similar that use it. You can also set only docroot and return DECLINED.
> The default trans handler then does the rest.
>
> You may also want to cache your DB lookups at least for the duration of the
> request. There are subrequests (when a directory index is created or mod_cgi
> computes PATH_TRANSLATED or similar). Each one of those would repeat the DB
> lookup otherwise.
>
>
> sub handler {
> my $r=shift;
>
>
> my $vpath;
> if( $r->prev ) { # handle subrequests
> $vpath=$r->prev->notes->{vpath};
> } elsif( $r->main ) { # handle internal redirects (ErrorDocument)
> $vpath=$r->main->notes->{vpath};
> } else {
> $vpath=db_lookup(...);
> $r->notes->{vpath}=$vpath;
> }
>
> $r->filename($vpath.$r->uri);
> return Apache2::Const::OK;
> }
>
>
> Torsten
>