Mojolicious has already written a number of articles from which you can get general impressions about this web framework. Reading publications you can understand how easily the application is deployed, including PSGI, it scales, interacts with web servers and withstands high loads. All this is good, of course, but for some reason there is no article on how to run the application on an ordinary virtual hosting. Although maybe it is not by the fact that to do it indecently simply?
Requirements.
Anyway, but shared hosting has to satisfy some requirements.
- Perl version 5.10 or higher is installed;
- allowed to use cgi-scripts;
- supported by mod_rewrite;
In general, that's all. Of course, it’s good if you can use the space outside the web directory to store all the necessary modules there. We assume that you yourself have already installed Mojolicious on your computer, for example, using any of the articles with reviews of this web framework.
To run a PSGI application on a hosting Plack must be installed and supported by mod_perl.
')
Mojolicious application
If you don’t have one yet, then I suggest you have some fun and check Habrahabr for tolerance. To do this, we will need Mojolicious himself, your favorite IDE and a little patience.
Application framework
The first step is to generate the application framework. Since we want to get out of the single-line application and put an end to the opposition of Dancer vs Mojolicious, we abandon the Lite version:
habra@cynovg-notebook:~$ mojo generate app Habra [mkdir] /home/habra/habra/script [write] /home/habra/habra/script/habra [chmod] habra/script/habra 744 [mkdir] /home/habra/habra/lib [write] /home/habra/habra/lib/Habra.pm [mkdir] /home/habra/habra/lib/Habra [write] /home/habra/habra/lib/Habra/Example.pm [mkdir] /home/habra/habra/t [write] /home/habra/habra/t/basic.t [mkdir] /home/habra/habra/log [mkdir] /home/habra/habra/public [write] /home/habra/habra/public/index.html [mkdir] /home/habra/habra/templates/layouts [write] /home/habra/habra/templates/layouts/default.html.ep [mkdir] /home/habra/habra/templates/example [write] /home/habra/habra/templates/example/welcome.html.ep
We delete directories with examples, if they interfere and proceed to entertainment.
Application Design
In order for the application to be at least a little more serious than “Hello, World!”, I suggest using the Habrakhabra search to develop skills. Namely, query the user for the search string, give it to Habra, get an answer and display it to the user.
Let's start small, learn to generate a search query entry form and output a stub template as a reaction to receiving data from the user. To do this, you need a route to the input form, the form template itself and the answer-stub.
All the routes we have are stored in the Habra.pm module located in the lib directory, the contents of which after generation will look like this.
package Habra; use Mojo::Base 'Mojolicious';
Since we are engaged in the search, it is logical to call our Search controller, and the routes to it, respectively, search # default and search # result. By default, we will display the input form:
$r->route( '/' )->via('get')->to( 'search#default' );
and create a template for the input form, placing it in /templates/search/deafult.html.ep:
% layout 'default'; % title ' '; <form action='<% url_for %>' method='post'> <input type='text' name='q' placeholder=' ?'> <input type='submit' value='!'> </form>
Now, after removing all unnecessary from the Habra.pm module and changing the default route, our module should look like this:
package Habra; use Mojo::Base 'Mojolicious'; sub startup { my $self = shift; my $r = $self->routes; $r->route( '/' )->via('get')->to( 'search#default' ); } 1;
Check how it works by running a script from the same directory from the command line:
morbo script/habra Server available at http://127.0.0.1:3000.
Turning to it from the browser at 127.0.0.1:35000 our input form should be displayed, which, of course, leads to the collapse of the application as soon as we are going to go to the thread. And this happens because we have no route to the control for the post method, which is used to transfer data from the input form. Add a route in the main module and a temporary template stub in the templates. To do this, add the line in the main module:
$r->route( '/' )->via('post')->to( 'search#result' )
and create an output template, templates / search / result.html.ep
% layout 'default'; % title ' ';
If we now check how our application works, we will see that as a reaction to our desire to go to the thread, there will be a blank page with the title “Search result”.
In order to achieve our goal, we will need the implementation of the result method in the control, which would accept the request, send it to Habr and convert the response from Habr to a convenient form. To do this, create a module lib / Habra / Search.pm with the following contents:
package Habra::Search; use Mojo::Base 'Mojolicious::Controller'; sub result { my $self = shift; my $str = $self->param( 'q' ); my $ua = Mojo::UserAgent->new; my $dom = $ua->get( 'http://habrahabr.ru/search/?q='.$str )->res->dom; my $pull = (); for my $raw ( $dom->find( 'div[id^="post"]' )->each ) { my $header= $raw->find( 'a[class="post_title"]' )->first; push @$pull, { title => $header->text, url => $header->{href}, content => $raw->find( 'div[class^="content"]' )->first->text }; } $self->render( result => $pull ); } 1;
and supplement the stub template to display the result of the query:
% layout 'default'; % title ' '; <ul> % for my $row ( @$result ) { <li> <h2><%= $row->{title} %></h2> <p><%= $row->{content} %></p> <p> : <a href="<%= $row->{url} %>"><%= $row->{url} %></a></p> </li> %} </ul>
If there is any simpler or, in your opinion, more "correct" way to extract data, then I would love to know about it.
We assume that for the first acquaintance this will be enough, but we still have not achieved the main goal, since our application is still on the local machine. It's time to run it on a shared hosting.
Application Publishing
At this stage we have hosting that meets the requirements and a working application. It remains the case for small, publish it. Oh yes, let's assume that we have a domain name mojoex.am.pl, and the path to the site /home/mojoex.am.pl/www
To run the application, you need to download the Mojolicious source codes, for example, from GitHub
git clone https://github.com/kraih/mojo.git
after that, remove all unnecessary from the distribution, leaving only the lib directory, since that is where all the modules are stored. Not to be confused between the modules of the application and the modules of Mojolisches itself, you can rename the lib directory as an example, to mojo.
The next step is to slightly modify the script that launches the application by adding a pointer to the directory with the Modzholisys modules and the application itself in its title and rename it to habra.cgi
use lib ( '/home/mojoex.am.pl/mojo' ); use lib ( '/home/mojoex.am.pl/lib' );
The next step is to put the modules Modzholishes beyond the site, along the path /home/mojoex.am.pl/mojo. Put the application modules and templates along the same path, /home/mojoex.am.pl/lib and /home/mojoex.am.pl/templates, respectively. Also create a directory for logs, /home/mojoex.am.pl/log. But the script that starts the application should be placed in the /home/mojoex.am.pl/www/cgi-bin directory, with the right to execute.
The final step is a bit of magic with mod_rewrite. It is necessary to create rules, for this we will create a .htaccess file, which should be located in the /home/mojoex.am.pl/www directory, with the following contents:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-l RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /cgi-bin/habra.cgi/$1 [L] RewriteRule ^$ /cgi-bin/habra.cgi [L]
frenzytechnix Tells that if hosting is installed mod_perl, then we can run a PSGI application. To do this, you need to specify the following in .htaccess:
<Perl> $ENV{PLACK_ENV} = 'production'; $ENV{MOJO_HOME} = '/home/mojoex.am.pl'; $ENV{MOJO_TEMPLATE_CACHE} = 0; $ENV{PERL5LIB} .= '/home/mojoex.am.pl/lib;/home/mojoex.am.pl/mojo' </Perl> SetHandler perl-script PerlHandler Plack::Handler::Apache2 PerlSetVar psgi_app /home/mojoex.am.pl/www/cgi-bin/habra
Everything, on this our ordeals should end, as can be seen by going to the main page of the site. If everything is done correctly, you should be prompted to enter search data. If this does not happen, then you should look at the reason in the server error logs, most likely the problem in accessing the application launch script.
Summary
As a result, the hosting structure should look similar:
/home/mojoex.am.pl/
Static files, such as styles, images, and so on, are stored in the usual way, thanks to the rules in .htaccess, they are ignored during redirection. When accessing non-existent resources, the application is redirected to the application, which independently decides how to proceed. If the call goes through the specified paths, then the control is transferred to the control, otherwise an error message is generated, which can be configured independently.
If a piece of code may seem cumbersome to someone, or there are some incomprehensible points, comments or suggestions, leave comments.