Installing Mako Server as a Service on Linux

Installing Mako Server as a service (aka daemon) on Linux is straightforward. The following example shows how to create a dedicated user for the Mako Server and create a boot script that starts the Mako Server when Linux starts. This script also lets you start/stop the service from the command line.

If you want to install the Mako Server as a Linux service quickly, use the Ansible Mako Server Installation Script.

Running the Mako Server in the background is super easy; all you have to do is to add the -d switch:

mako -d additional-commands

As shown above, Starting the Mako Server in the background makes the server run until Linux reboots. We can automate the Mako Server startup when Linux powers on by either creating an init.d script or a systemd script.

Getting Started

Note, all commands below must be run as user root. If you are not root, you can become root by using the command su or sudo sh.

Installing the Mako Server:

First, we must download and install the Mako Server. You can skip this step if you have already copied Mako Server to your /usr/local/bin directory. The following commands download Mako Server for x86 (32 or 64) bit. Download the appropriate version for other platforms or cross-compile the source for your target platform.

Copy the following commands and paste into your terminal window.

cd /tmp; export XBIT=`getconf LONG_BIT`; wget http://makoserver.net/download/mako.linux-x$XBIT.tar.gz; tar xvzf mako.linux-x$XBIT.tar.gz; cp mako mako.zip /usr/local/bin;

Create a user for the server:

The next step is creating a dedicated Mako Server user. We will use the name 'mako' as the Linux user. You can choose any username or simply run the server under your existing user account. However, you must modify the service script if you use a username other than 'mako'.

useradd -G daemon -m -s /bin/bash mako

You should now have a new user with the home directory /home/mako. We are now ready to install the Mako Server's boot script. This script starts the Mako Server as a Linux service (aka daemon) -- that is, the server runs in the background.

Option 1; init.d script:

Open the following file in an editor, such as nano:

nano /etc/init.d/mako.sh

Copy the following and paste the content into mako.sh:

#!/bin/sh ### BEGIN INIT INFO # Provides: mako # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 6 # Short-Description: Mako Server daemon script ### END INIT INFO # # description: Mako Server # processname: mako start() { echo "Starting Mako Server" export HOME=/home/mako cd /home/mako ulimit -n 200000 echo "File (socket) descriptor limit:" ulimit -n /usr/local/bin/mako -u mako -d RETVAL=$? return $RETVAL } stop() { start-stop-daemon -K -x /usr/local/bin/mako return 0 } case "$1" in start) start ;; boot) start ;; stop) stop ;; restart) stop sleep 1 start ;; *) echo "Usage: /etc/init.d/mako.sh {start|stop|restart}" exit 1 esac exit 0

You should now be able to start and stop the Mako Server as a background process from the command line:

/etc/init.d/mako.sh start /etc/init.d/mako.sh stop

Although you can start and stop the server from the command line, the server will not start if you reboot your Linux computer. For this to work, the following command must be run:

update-rc.d mako.sh defaults

In addition to the start and stop command shown above, you should now also be able to start and stop the service as follows:

service mako.sh start service mako.sh stop

Option 2; systemd service unit file

Create a service unit file called mako.service in the /etc/systemd/system/ directory using the following command:

nano /etc/systemd/system/mako.service

Copy the following and paste the content into mako.service:

[Unit] Description=Mako Server After=network.target [Service] Type=simple User=mako WorkingDirectory=/home/mako/ ExecStart=/usr/local/bin/mako -s Restart=always RestartSec=3 LimitNOFILE=200000 SyslogIdentifier=mako AmbientCapabilities=CAP_NET_BIND_SERVICE [Install] WantedBy=multi-user.target

Restart systemd:

systemctl daemon-reload

Start, check status, and stop the Mako Server:

systemctl start mako.service systemctl status mako.service systemctl stop mako.service

Configuring Your First Application

If you started Mako Server as instructed above, you will get a 404 not found response if you visit the server using a browser. We need to install at least one LSP application in the server. The following example shows how to create one app that simulates a standard web server's www directory.

Run the following command in your console Window:

su mako; cd; whoami;

You should now be running as the user 'mako' and be in the /home/mako directory. The next step is to create a mako configuration file that loads one app. You can use any editor to create the configuration file. We are using the 'nano' editor in the following example. If you get an error when running nano, install nano as follows: apt-get install nano.

nano mako.conf

When nano is running, copy the following and paste into the nano editor.

apps = { { name="", path="www", dav=true, auth=true }, } users = { mako="123456" } tracelogger = { auth=true }

Using the nano editor, modify the username "mako" to your username and the password "123456" to a more suitable password. Press Ctrl-X and save the file (Y). You should now be back at the command prompt.

The above configuration file sets up one root application using the path 'www' (that is /home/mako/www). We must create this directory before restarting the server. Type the following command at the Linux command prompt.

mkdir www

We can now restart the server. The two following commands exit user mako (back to root) and restart the server.

exit; /etc/init.d/mako.sh restart;

You will still get a 404 page if you visit the server using a browser. What we need is an index page in the new www directory.

Create index.lsp in the www directory.

Map/mount the server using WebDAV if the Mako Server runs on an online VPS (cloud server)

To map/mount the online server as a file system, use the URL: http://server-address/dav/. In Windows, you can go to Map Network Drive and enter the URL to map the online server as a drive in Windows. See the WebDAV information page for the BarracudaDrive product for more information on how to map/mount a WebDAV drive. The above Mako Server configuration script (mako.conf) enabled WebDAV for your application. The WebDAV username/password (default mako/123456) is what you put into the modified Mako Server configuration script.

When you have mapped/mounted WebDAV, use any editor on your host computer (PC) and open/create a new file on the mapped/mounted drive.

The following example shows how to do this on Windows using Notepad. The online server is mapped as 'Z:'.

notepad z:\index.lsp

Copy the following content into the editor (index.lsp) and save the file.

<h1><?lsp print"Hello Browser" trace"Hello Trace" ?></h1>

You should now see "Hello Browser" if you visit your home page using your browser.

Function trace normally prints to the console, but you have no console when the server runs as a background service. When the server runs as a background service, the Mako Server inserts the information printed to the console into the file mako.log. This log can also be viewed in real time if TraceLogger is enabled. The TraceLogger was enabled in the above Mako Server configuration script. Navigate to http://servername/rtl/tracelogger/ and log in using the credentials you entered when you modified the mako configuration file. You should see the following:

image

Notice how the message "Hello Trace" and the line number are printed each time you navigate (using a different browser window) to your main index page.

Hash Encrypted Passwords

We stored the passwords in clear text in the above Mako Server configuration file (mako.conf). You can also store the passwords as an MD5 hash. The MD5 hash must be computed as explained in the Barracuda App Server manual: Storing passwords as a hash value.

You may simplify the calculations using the following LSP script. Copy the following content and paste it into an LSP page that you can run on your server instance.

<?lsp local d = request:data() if d.u and d.p then local realm="Barracuda Server" -- For Mako Server DAV and TraceLogger local ha1=ba.crypto.hash"md5"(d.u)":"(realm)":"(d.p)(true,"hex") response:write(d.u,'={"',ha1,'"}') else print"Username and password required" end ?>

When you run the page, enter the following:

http://servername/pagename.lsp?u=mako&p=123456

'mako' and '123456' must be substituted with your own credentials.

Executing the page with the above credentials produces the following result:

mako={"072fa416feb977c8c2d7464e50a32122"}

Copy the produced result and paste it into mako.conf:

users = { mako={"072fa416feb977c8c2d7464e50a32122"} }

Xedge and Mako Server when running as a service: A perfect combination

Xedge is a powerful IDE designed for developing web and embedded applications with the Mako Server. When the Mako Server runs as a background service, using Xedge becomes highly convenient because it allows for:

  • Live Development: Edit and test Lua scripts in real-time without restarting the server.
  • Remote Management: Seamlessly manage and update the server from any location.
  • Centralized Control: Monitor logs, control processes, and handle file transfers all within the IDE.
  • Streamlined Deployment: Quickly deploy updates without interrupting the service.
  • Error Tracking: View detailed logs directly in the IDE to simplify debugging.

The following mako.conf file should be used when using Xedge:

apps = { { name="", path="Xedge.zip"} }

Xedge turns Mako Server into a robust, efficient platform for fast, continuous development.


Installing Mako Server on an Online VPS

The tutorial Setting up an IoT Broker explains how to install the Mako Server on a low cost online VPS.


Posted in Tutorials by bd


OSZAR »