Web App With Mojolicious and Nginx
Web App With Mojolicious and Nginx
Web App With Mojolicious and Nginx
Mojolicious
Introduction
This document explains how to create and setup a Web App implementing a Markdown presenter
at Linode using Debian, Nginx, daemontools and Mojolicious.
Please change all references to localhost to the domain your server has.
Final result
I use the Vim editor. If you prefer another editor, like emacs or nano, use it. For me Vim is the best
because it's light, smart and works very well. At the end of this document you can find the link to a
good tutorial about Vim. Enjoy!
After creating your new Linode, or if you already have one, select it and then click on Deploy a
Linux Distribution. You'll be prompted to deploy a Linux distribution, as shown below. This is the
operating system that will be installed on your Linode. You can choose from Arch Linux, CentOS,
Debian, Fedora, Gentoo, openSUSE, Slackware, and Ubuntu. My choice is Debian.
Always I create a normal user for my logins because isn't good login with root account. You could
do it later, now lets install the packages we must use in this job.
1. Login with your credentials and install some software we need with apt-get:apt-get install
links2 nginx mojolicious libtext-markdown-perl daemontools daemontools-run
If you want to use php with nginx you must install it:
I don't reccomend but if you prefer it's possible to install daemontools compiling it by hand. See at
the end of this document.
Mojolicious
use Mojolicious::Lite;
use Text::Markdown qw{ markdown };
use Path::Class;
use Encode qw{ decode_utf8 };
app->config(hypnotoad => {listen => ['http://*:8080']});
app->start;
Creating the directories where we can put the markdown files to be read by web:
mkdir /opt/www
Copying the file of this document to be presented at web. Please, put there the document you want!
cp weapp-server.md /opt/www/
morbo mdreader.pl
You will see the app in action pointing your web browser to http://localhost:8080/weapp-server.md
Seting up nginx
1. With nginx you will publish your app at web with reverse proxy. Add this configuration at
available sites for Nginx:vi /etc/nginx/sites-available/mdreader
2. Write the comands above: upstream mdreader {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://mdreader;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "http";
}
}
Now we must automate the Web App startup when we boot the server and guarantize it will be
running under control and logging messages uninterruptly. The choice is put it under daemontools
service control. Lets do:
Colophon
● The animal sleeps now. It's cool!
A dragon is a legendary creature, typically with serpentine or reptilian traits, that features in the
myths of many cultures.
See also:
I strongly recomend you read the Getting Start tutorial for Linode:
https://www.linode.com/docs/getting-started
daemontools by hand
Install daemontools
1. Create a /daemontools directory: mkdir daemontools
cd daemontools
2. Download daemontools-0.76.tar.gz into the directory daemontools. Unpack the daemontools
package: wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
tar -xzf daemontools-0.76.tar.gz
rm daemontools-0.76.tar.gz
cd admin/daemontools-0.76
3. Compile and set up the daemontools programs: package/install
4. Reboot your system to start svscan. reboot
5. Report your succesful install to cr.yp.to (optional): mail djb-sysdeps@cr.yp.to <
/package/admin/daemontools/compile/sysdeps
Credits