Skip to content
yuki-kimoto edited this page Apr 1, 2011 · 34 revisions

Apache deployment

Apache/mod_proxy (Host name)

If you want to deploy multiple applications, it is easy to use virtual-host and mod_proxy in Apache.

<VirtualHost *:80>

   ServerName app1.somehost.com
   ProxyPass / http://localhost:3000/
   ProxyPassReverse / http://localhost:3000/

    </VirtualHost>

    <VirtualHost *:80>

   ServerName app2.somehost.com
   ProxyPass / http://localhost:3001/
   ProxyPassReverse / http://localhost:3001/

    </VirtualHost>

Note that you must create subdomains for each applications in above way.

If you see apache "Service Temporarily Unavailable" error, SELinux mode is maybe set to "enforcing". Try to change mode or do setting properly.

vi /etc/selinux/config

# Setting
SELINUX=disabled

Apache/mod_proxy (Path)

you can also do the following way using path.

<VirtualHost *:80>

   ServerName somehost.com
   ProxyPass /app1 http://localhost:3000
   ProxyPassReverse /app1 http://localhost:3000

    </VirtualHost>

    <VirtualHost *:80>

   ServerName somehost.com
   ProxyPass /app2 http://localhost:3001
   ProxyPassReverse /app2 http://localhost:3001

    </VirtualHost>

In this case not like using host name, you must set request base URL by yourself if the request is from proxy.

app->hook('before_dispatch' => sub {
    my $self = shift;
    
    if (my $hosts = $self->req->headers->header('X-Forwarded-Host')) {
        my $host = (split(/,/, $hosts))[0];
        $host =~ s/^\s+//;
        $host =~ s/\s+$//;
        
        ### Proxy Path setting ###
        my $path = '/app1';
        
        $self->req->url->base->host($host)->port(80)->path->parse($path);
    }
});

Apache/CGI

To make Mojolicious application run in CGI mode under Apache make sure your .htaccess file looks like following:

AddHandler cgi-script .cgi
Options +ExecCGI

IndexIgnore *

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
RewriteRule ^(.*) public/$1 [L]

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteRule ^(.*) myapp.cgi [L] 

In this way you make Apache render static files from /public folder for you.

Pretty "Web 2.0" URLs

Nobody really likes URLs like www.mycompany.com/myapp.cgi/foo/bar. Instead we want prefer seeing: www.mycompany.com/foo/bar, they are much nicer to use. The following configuration will hide the fact that the application is running from a cgi. Particularly useful if your hosting environment forbids the use of ScriptAlias.

## .htaccess
RewriteEngine on
# Rewrite only if the request isn't for a real file, directory, or symlink.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myapp.cgi/$1 [L]

## lib/MyApp.pm
sub startup {
     my $self = shift;
     # ... your start up and routes.
     $self->hook( before_dispatch => sub {
          my $self = shift;
          # notice: url must be fully-qualified or absolute, ending in '/' matters.
          $self->req->url->base(Mojo::URL->new(q{http://www.mycompany.com/});
     });
}

Apache/Shared Hosting

We have a discussion on the mailinglist about setting up mojo in shared environments.

https://groups.google.com/group/mojolicious/browse_thread/thread/6f17653fe30ab884

And here is the actual example working equally well with mod_fastcgi and mod_cgi: http://github.com/kberov/MYDLjE/blob/master/.htaccess

Clone this wiki locally