Skip to content
dekimsey edited this page Jan 21, 2011 · 34 revisions

Apache deployment

Apache/mod_proxy

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.

And also you can do the following way using URL 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>

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

Clone this wiki locally