-
Notifications
You must be signed in to change notification settings - Fork 0
Mojolicious with FastCGI on Dreamhost
The Dreamhost is a web hosting company that provides various services related to hosting sites, either with private servers or shared hosts.
In this text we address the installation of an Mojolicious application (Mojolicious::Lite to be more precise) using FastCGI on a Dreamhost shared host, in which we have no access to administrator password nor any special privilege.
The first step need to install a web application on Dreamhost is create a user account with shell access and a domain pointing to some directory of that account.
Multiple applications and multiple domains can be configured under the same user account, but in this example we will use both an new account and a new domain to show all steps.
A new user can be created through the administrative panel of Dreamhost.
In the option Type of User Account we tick Shell: Allows SFTP/FTP plus shell access. Thus we allow that the new user can access the server via ssh.
It's optional, but we also check the option Enhanced security to leave this account completely isolated from others. Then even our another users cannot access the files of this account.
A domain or subdomain can also be configured via the control panel of Dreamhost.
In this case we use a sub-domain, but could be any other domain.
In the option Domain to host we add the name of the sub-domain test.blabos.org in this case.
In the option Do you want the www in your URL? we check Remove WWW (personal choice. Can be any one).
In the option PHP mode we tick PHP 5 FastCGI. This will enable FastCGI support into the directory that our sub-domain points to.
NOTE: In the Dreamhost wiki's they warn that unfortunately they only support FastCGI with PHP. In our case, we use FastCGI with Perl, then we're on our own risk.
In the Web directory option you may configure the directory that suits better for your needs. This directory is where the sub-domain will point to and where we will install the Mojolicious application later.
The DNS update should take a few hours, then set both the user and the domain in advance (a day or two, for example).
After the updates to take effect, you will be able to log into the sub-domain via ssh and access it (the directory configured above) via a browser.
When configuring correctly the local::lib we will able to install additional Perl modules from http://cpan.org directly into the home directory of our regular user. For that log into the server and run the steps described on http://blog.blabos.org/2011/01/perl-cada-vez-mais-facil/.
NOTE: The extra detail here is that unlike the tutorial, the Dreamhost file where we configure the environment variables is .bash_profile rather than .bashrc.
After configuring the local::lib module we install the module Mojolicious::Lite with the command:
[server]$ cpanm Mojolicious::LiteThe Mojolicious just depends on the Perl CORE modules, so this step should be pretty fast.
After this we will create our own little test application within the directory which the sub-domain points to, which in our case is test.blabos.org:
[server]$ cd test.blabos.org
[server]$ ls
favicon.gif quickstart.html favicon.ico
[server]$ mkdir TestApp
[server]$ cd TestApp /
[server]$ vi app.pl Let's create our test application containing only the lines:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => sub {
my $ self = shift;
$self->stash({
'Message' => 'Hello World !!!',
});
} => 'index';
app->start;
__DATA__
@@ index.html.ep
<center><h1><% = $ message%></h1></center>Then we need to give permission to execute it:
[server]$ chmod +x app.plThen we can run it using its internal web server:
[server]$ ./app.pl daemon
Sat Sep 11 19:56:19 2010 info Mojo::Server::Daemon:363 [17141]: Server
listening (http://*:3000)
Server available at http://*:3000.We still cannot access it by sub-domain setup, but we can do a test by pointing to the server within the Dreamhost. This can be done with a browser or by using the techniques shown in the text about HTTP:
user@host:~$ nc server.dreamhost.com 3000
GET / HTTP/1.1
Host: server.dreamhost.com:3000
HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Type: text/html
X-Powered-By: Mojolicious (Perl)
Date: Sun, 12 Sep 2010 03:04:56 GMT
Content-Length: 41
Server: Mojolicious (Perl)
<center><h1>Hello World!!!</h1></center>At this point we only guarantee that our application does not contain syntax errors and it is fully functional, we still need to configure the FastCGI environment.
To configure the FastCGI environment, we need to create one .htaccess file into the directory where the sub-domain points to. This file allows us to pass settings to the web server (Apache) that apply only to that directory tree and without requiring administrative access. For details about the file .htaccess files, see the Apache documentation.
Now begins the tricky part :)
According to the FastCGI documentation in the Dreamhost wiki the .htaccess file must have the content below:
Options +FollowSymLinks +ExecCGI
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteRule ^(.*)$ TestApp/dispatch.fcgi [QSA,L]Although this configuration allows the application startup, beware because it is insufficient to ensure the correct functioning of the Mojolicious routes. This may lead to a preliminar false sense of correct configuration since the application starts. As the complete configuration of mod_rewrite is also beyond the scope of this text (see the book X for details), let me just say that the configuration that best behaved for me was:
Options +FollowSymLinks +ExecCGI
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteRule ^(TestApp/dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ TestApp/dispatch.fcgi/$1 [QSA,PT,L]You may explore all other options of configuration, but remember to kill your application whenever you update the .htaccess file. Again, has a trick. After kill the process you MUST erase the session files at /tmp directory:
[server]$ killall -9 dispatch.fcgi && rm /tmp/sess_* -Rf > /dev/null 2>&1Missing this you will be thrown into a deep pit of mess. All session files that not owned by current user will reject (I hope so :) ) deletion.
Moreover, because of the process management policy on the shared server, the script executable application MUST be renamed to dispatch.fcgi
[server]$ mv app.pl dispatch.fcgiThe application also needs to be tuned properly in order to use FastCGI and need to get explicit paths to the modules that were installed in the user directory. Thus the application code is:
#!/usr/bin/env perl
use lib '/home/user/perl5/lib/perl5';
use local::lib;
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
$self->stash({
'message' => 'Hello World!!!',
});
} => 'index';
app->start;
__DATA__
@@ index.html.ep
<center><h1><%= $message %></h1></center>Another detail also documented in the Dreamhost wiki is that the executable and the directory where it is need to belong to the same user and group account you created. In addition, both must have write permission enabled for the group.
In our tests this configuration was irrelevant, but as is documented, let's keep it:
[server]$ chmod -R g+w TestApp/Our directory structure from the root directory of the sub-domain should be something like:
[server]$ tree -a
.
|-- .htaccess
|-- TestApp
| `-- dispatch.fcgi
|-- favicon.gif
|-- favicon.ico
`-- quickstart.htmlThe files favicon.gif, favicon.ico and quickstart.html are installed by Dreamhost when creating the sub domain and they has nothing to do with our application.
Now we can test the application directly to the sub-domain:
user@host:~$ nc test.blabos.org 80
GET / HTTP/1.1
Host: test.blabos.org
HTTP/1.1 200 OK
Date: Sun, 12 Sep 2010 03:27:27 GMT
Server: Apache
X-Powered-By: Mojolicious (Perl)
Content-Length: 41
Vary: Accept-Encoding
Content-Type: text/html
<center><h1>Hello World!!!</h1></center>I thank to Breno G. de Oliveira for patience, availability and support when we need to deploy the application. Thank you!
AUTHOR
Blabos de Blebe
License
This text is licensed under Creative Commons by-sa