You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Valerio <va...@gmail.com> on 2008/03/20 18:50:55 UTC

Virtual host with mod_perl

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,
<VirtualHost *:80>
ServerName %0
VirtualDocumentRoot /srv/www/vhosts/%0/
HostnameLookups Off
UseCanonicalName Off
VirtualScriptAlias "/srv/www/vhosts/%0/cgi-bin/"
    <Directory ">
	    Options None
	    AllowOverride None
    	Order allow,deny
	    Allow from all
    </Directory>
</VirtualHost>

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

Posted by Valerio <va...@gmail.com>.
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 <to...@gmx.net>:
> 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

Posted by Torsten Foertsch <to...@gmx.net>.
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

Posted by Valerio <va...@gmail.com>.
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:

<Perl>
	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;	
	}
</Perl>

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 <pe...@elem.com>:
> On Fri, Mar 28, 2008 at 11:36 AM, Ryan Gies <ry...@livesite.net> 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

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, Mar 28, 2008 at 11:36 AM, Ryan Gies <ry...@livesite.net> 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)

Posted by Ryan Gies <ry...@livesite.net>.
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: <VirtualHost not allowed here
> 
> 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([
>     '<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:
>> On Fri, Mar 28, 2008 at 8:30 AM, Valerio <va...@gmail.com> 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

Posted by Perrin Harkins <pe...@elem.com>.
On Fri, Mar 28, 2008 at 8:30 AM, Valerio <va...@gmail.com> 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

Posted by Valerio <va...@gmail.com>.
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é Warnier <aw...@ice-sa.com>:
> 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

Posted by Valerio <va...@gmail.com>.
I will check the books you have suggested.
:-)

Thanks a lot

Valerio

2008/3/21, André Warnier <aw...@ice-sa.com>:
> 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

Posted by André Warnier <aw...@ice-sa.com>.
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

Posted by Valerio <va...@gmail.com>.
2008/3/20, Roberto C. Sánchez <ro...@connexer.com>:
> 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ánchez
>  http://people.connexer.com/~roberto
>  http://www.connexer.com
>
> -----BEGIN PGP SIGNATURE-----
>  Version: GnuPG v1.4.6 (GNU/Linux)
>
>  iD8DBQFH4rLa5SXWIKfIlGQRApHoAJ9R8uq3r7q/LytbnM8vuNo6HEg1xACffDp6
>  UN/6ED5mGa7ap4hiCUtH1L4=
>  =JN2S
>  -----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

Posted by "Roberto C. Sánchez" <ro...@connexer.com>.
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ánchez
http://people.connexer.com/~roberto
http://www.connexer.com