Sails is a real-time MVC framework for building production-ready enterprise Node.js applications. It is an excellent tool for helping you quickly create and prototype websites. It is built on top of Node JS and express JS, so all applicable in NodeJS and Express will work with Sails JS. It supports scalable WebSockets, service-oriented architecture, and multiple data stores in the same project, and it provides basic security and role-based access control.

This post will show you how to install Sails.js with Apache as a reverse proxy on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Getting Started

Before starting, updating your packages to the latest version is recommended. You can update all of them by running the following command:

apt update -y

apt upgrade -y

Once all the packages are updated, install other required dependencies using the following command:

apt-get install curl wget gnupg2 -y

Once you are done, you can proceed to the next step.

Install Node.js

Next, you will need to install Node.js and NPM to create a Sails.js application. First, add the Node.js repository using the following command:

curl -sL https://deb.nodesource.com/setup_16.x | bash -

Once the repository is added, install the Node.js package with the following command:

apt-get install nodejs -y

After the installation, you can verify the Node.js version using the following command:

node --version

You will get the Node.js version in the following output:

v16.17.1

Install Sails.js

Next, you will need to install Sails.js on your server. You can install the Sails using the Node Package Manager.

npm -g install sails

Once the Sails is installed, you can verify it with the following command:

sails --version

You will get the Sails version in the following output:

1.5.3

Create a Sails.js Application

After installing Sails, let’s create a simple Sails application using the following command:

sails new sails-app

You should get the following output:

 Choose a template for your new Sails app:
 1. Web App  ·  Extensible project with auth, login, & password recovery
 2. Empty    ·  An empty Sails app, yours to configure
 (type "?" for help, or  to cancel)
? 2
 info: Installing dependencies...
Press CTRL C to cancel.
(to skip this step in the future, use --fast)
 info: Created a new Sails app `sails-app`!

You can verify all files created by Sails using the following command:

ls sails-app

You should see the following output:

api  app.js  assets  config  Gruntfile.js  node_modules  package.json  package-lock.json  README.md  tasks  views

Next, change the directory to your Sails app and start the application with the following command:

cd sails-app

sails lift

You should see the following output:

 info: Starting app...

 info: 
 info:                .-..-.
 info: 
 info:    Sails              <|    .-..-.
 info:    v1.5.3              |
 info:                       /|.
 info:                      / || 
 info:                    ,'  |'  
 info:                 .-'.-==|/_--'
 info:                 `--'-------' 
 info:    __---___--___---___--___---___--___
 info:  ____---___--___---___--___---___--___-__
 info: 
 info: Server lifted in `/root/sails-app`
 info: To shut down Sails, press    C at any time.
 info: Read more at https://sailsjs.com/support.

debug: -------------------------------------------------------
debug: :: Sun Sep 25 2022 05:43:59 GMT 0000 (Coordinated Universal Time)

debug: Environment : development
debug: Port        : 1337
debug: -------------------------------------------------------

Press the CTRL C to stop the application. We will create a systemd service file to start and manage the Sails application.

Create a Systemd Service File for Sails.js

Next, you will need to create a systemd service file to manage the Sails.js application. You can create it with the following command:

nano /etc/systemd/system/sails-app.service

Add the following lines:

[Unit]
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/root/sails-app
ExecStart=/usr/bin/sails lift
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save and close the file, then reload the systemd daemon to apply the changes:

systemctl daemon-reload

Next, start the Sails.js service and enable it to start at system reboot:

systemctl start sails-app

systemctl enable sails-app

You can now check the status of the Sails application with the following command:

systemctl status sails-app

You should get the following output:

? sails-app.service
     Loaded: loaded (/etc/systemd/system/sails-app.service; disabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-09-25 05:44:42 UTC; 6s ago
   Main PID: 2896 (node)
      Tasks: 22 (limit: 4579)
     Memory: 159.2M
        CPU: 3.676s
     CGroup: /system.slice/sails-app.service
             ??2896 node /usr/bin/sails lift
             ??2903 grunt "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ">

Sep 25 05:44:44 ubuntu2204 sails[2896]:  info:  ____---___--___---___--___---___--___-__
Sep 25 05:44:44 ubuntu2204 sails[2896]:  info:
Sep 25 05:44:44 ubuntu2204 sails[2896]:  info: Server lifted in `/root/sails-app`
Sep 25 05:44:44 ubuntu2204 sails[2896]:  info: To shut down Sails, press    C at any time.
Sep 25 05:44:44 ubuntu2204 sails[2896]:  info: Read more at https://sailsjs.com/support.
Sep 25 05:44:44 ubuntu2204 sails[2896]: debug: -------------------------------------------------------
Sep 25 05:44:44 ubuntu2204 sails[2896]: debug: :: Sun Sep 25 2022 05:44:44 GMT 0000 (Coordinated Universal Time)
Sep 25 05:44:44 ubuntu2204 sails[2896]: debug: Environment : development
Sep 25 05:44:44 ubuntu2204 sails[2896]: debug: Port        : 1337
Sep 25 05:44:44 ubuntu2204 sails[2896]: debug: -------------------------------------------------------

At this point, the Sails app is started and listens on port 1337. You can now proceed to the next step.

Configure Apache as a Reverse Proxy for Sails.js

Configuring Apache as a reverse proxy to access the Sails application is a good idea. First, install the Apache server with the following command:

apt install apache2 -y

Once the Apache package is installed, create an Apache virtual host configuration file with the following command:

nano /etc/apache2/sites-available/sails.conf

Add the following lines:

    ServerName sailsapp.example.com

    ServerAdmin [email protected]
    DocumentRoot /root/sails-app

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    
            ProxyPass   http://127.0.0.1:1337
            ProxyPassReverse   http://127.0.0.1:1337
    

    
            ProxyPass !
    

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
    RewriteCond %{QUERY_STRING} transport=websocket    [NC]
    RewriteRule /(.*)           ws://localhost:1337/$1 [P,L]

Save and close the file then activate the Sails virtual host with the following command:

a2ensite sails.conf

Next, enable other Apache modules with the following command:

a2enmod headers proxy_http xml2enc proxy ssl proxy_wstunnel rewrite proxy_ajp deflate proxy_balancer proxy_connect proxy_html

Next, restart the Apache service to apply the changes:

systemctl restart apache2

You can now verify the Apache service status with the following command:

systemctl status apache2

You should get the following output:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-09-25 05:46:54 UTC; 2s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 3986 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 3990 (apache2)
      Tasks: 55 (limit: 4579)
     Memory: 6.8M
        CPU: 90ms
     CGroup: /system.slice/apache2.service
             ??3990 /usr/sbin/apache2 -k start
             ??3991 /usr/sbin/apache2 -k start
             ??3992 /usr/sbin/apache2 -k start

Sep 25 05:46:54 ubuntu2204 systemd[1]: Starting The Apache HTTP Server...

Currently, the Apache web server is installed and configured as a reverse proxy for the Sails.js application. You can now proceed to the next step.

Access Sails.js Web UI

You can now open your web browser and access the Sails.js web interface using the URL http://sailsapp.example.com. You should see the Sails.js web UI on the following screen:

<img alt="" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/04/echo/p1.png6627f52120ac4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="376" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! You have successfully installed Sails.js with Apache as a reverse proxy on Ubuntu 22.04. You can now host a scalable and production-ready web application using the Sails.js framework. Feel free to ask me if you have any questions.